JDBC and transactions

Lists: pgsql-jdbc
From: "Chris White (cjwhite)" <cjwhite(at)cisco(dot)com>
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: JDBC and transactions
Date: 2005-04-01 23:39:41
Message-ID: 200504012339.j31Ndf3S013996@sj-core-4.cisco.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

I am using JDBC to connect to my database. I am using connections with
autocommit turned off, so I can use transactions.

Questions:

1) When does a transaction start? As soon as I do the first insert/update on
the connection or as soon as I set autocommit off?
2) After doing a commit, when does the next transaction start?

The reason I ask these questions is because I have an issue doing DB Vacuum,
where free rows are not being reused because there is still an active
transaction that was started before the rows were deleted, and I am trying
to determine what could be causing the problem.

TIA
Chris White


From: Kris Jurka <books(at)ejurka(dot)com>
To: "Chris White (cjwhite)" <cjwhite(at)cisco(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: JDBC and transactions
Date: 2005-04-02 07:33:13
Message-ID: Pine.BSO.4.56.0504020229300.29873@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

On Fri, 1 Apr 2005, Chris White (cjwhite) wrote:

> 1) When does a transaction start? As soon as I do the first insert/update on
> the connection or as soon as I set autocommit off?
> 2) After doing a commit, when does the next transaction start?

This depends on the driver version you are using. The 7.4 driver starts a
transaction immediately upon setAutoCommit(false) and starts the next
transaction immediately upon commit. The 8.0 driver starts the
transaction on the first execution for both the initial and subsequent
transactions.

Kris Jurka


From: "Chris White (cjwhite)" <cjwhite(at)cisco(dot)com>
To: "'Kris Jurka'" <books(at)ejurka(dot)com>
Cc: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: JDBC and transactions
Date: 2005-04-03 20:27:36
Message-ID: 200504032027.j33KRa3S010688@sj-core-4.cisco.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Thanks that helps me solve my problem.

-----Original Message-----
From: Kris Jurka [mailto:books(at)ejurka(dot)com]
Sent: Friday, April 01, 2005 11:33 PM
To: Chris White (cjwhite)
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: [JDBC] JDBC and transactions

On Fri, 1 Apr 2005, Chris White (cjwhite) wrote:

> 1) When does a transaction start? As soon as I do the first
> insert/update on the connection or as soon as I set autocommit off?
> 2) After doing a commit, when does the next transaction start?

This depends on the driver version you are using. The 7.4 driver starts a
transaction immediately upon setAutoCommit(false) and starts the next
transaction immediately upon commit. The 8.0 driver starts the transaction
on the first execution for both the initial and subsequent transactions.

Kris Jurka


From: "Chris White (cjwhite)" <cjwhite(at)cisco(dot)com>
To: "'Kris Jurka'" <books(at)ejurka(dot)com>
Cc: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: JDBC and transactions
Date: 2005-04-03 22:39:54
Message-ID: 200504032239.j33MdsgS029843@sj-core-3.cisco.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

So I should setAutoCommit(true) after doing the commit to end the
transaction. But won't this waste a transaction, if the next transaction
starts immediately upon the commit?

Chris

-----Original Message-----
From: Kris Jurka [mailto:books(at)ejurka(dot)com]
Sent: Friday, April 01, 2005 11:33 PM
To: Chris White (cjwhite)
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: [JDBC] JDBC and transactions

On Fri, 1 Apr 2005, Chris White (cjwhite) wrote:

> 1) When does a transaction start? As soon as I do the first
> insert/update on the connection or as soon as I set autocommit off?
> 2) After doing a commit, when does the next transaction start?

This depends on the driver version you are using. The 7.4 driver starts a
transaction immediately upon setAutoCommit(false) and starts the next
transaction immediately upon commit. The 8.0 driver starts the transaction
on the first execution for both the initial and subsequent transactions.

Kris Jurka


From: Kris Jurka <books(at)ejurka(dot)com>
To: "Chris White (cjwhite)" <cjwhite(at)cisco(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: JDBC and transactions
Date: 2005-04-04 17:09:18
Message-ID: Pine.BSO.4.56.0504041206280.2383@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

On Sun, 3 Apr 2005, Chris White (cjwhite) wrote:

> So I should setAutoCommit(true) after doing the commit to end the
> transaction. But won't this waste a transaction, if the next transaction
> starts immediately upon the commit?
>

Well, again that depends on what driver version you are using. 8.0 will
not have started a new transaction yet so one will not be used. For 7.4 a
transaction indeed will be wasted. You could try and be clever and use
setAutoCommit(true) instead of commit() because it will commit your
transaction and not start another, but this would be confusing from a
readability point at least.

Kris Jurka