pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
JDBC and transactions
8+ messages / 4 participants
[nested] [flat]

* JDBC and transactions
@ 2002-04-23 20:23 Ryan Chambers <ryan@squaretrade.com>
  2002-04-23 20:48 ` Re: JDBC and transactions Dave Cramer <Dave@micro-automation.net>
  0 siblings, 1 reply; 8+ messages in thread

From: Ryan Chambers @ 2002-04-23 20:23 UTC (permalink / raw)
  To: pgsql-jdbc


Hi

I'm trying to figure out how to use transactions with JDBC and PostGresql,
without much success. In particular, I can't figure out how to mark the
start of a transaction and how to do a rollback.

I'm iterating through a list of objects and I insert them into the DB one by
one. However, it is possible to get a NoSuchElement runtime exception while
going through the list. If this happens, I want to rollback all the previous
inserts.

The code/pseudo-code looks like this.

Connection conn = // get the connection
conn.setAutoCommit(false);

boolean rollBack = false;

while(myList.hasNext()) {
	MyObject obj = null;
	try {
		obj = (MyObject)myList.next();
	}
	catch(NoSuchElementException e) {
		rollBack = true;
		break;
	}

	try {
		persistToDB(conn, obj);
	}
	catch(SQLException e) {
		rollBack = true;
		break;
	}
}

if(rollBack) {
	try {
		conn.rollback();
	}
	catch(SQLException e) {}
}
else {
	try {
		conn.commit();
	}
	catch(SQLException e) {}
}

conn.close();


I run this code and, in order to test it, deliberately throw a
NoSuchElementException. My code correctly detects this and calls the
rollback method on the connection. But when I look in the database, the rows
before the exception have been inserted, and the rollback hasn't done
anything. So how do I specify the start of the transaction? I've even tried
executing Statements and PreparedStatements with the query "begin", trying
to emulate the PostGresql "begin" syntax, but this doesn't work either. I
know the syntax of the statements I use to persist the object are good, and
I'm not seeing any exceptions when I do any of the SQL commands. (I ignore
some of the exceptions because these haven't caused my any problems).

Has anybody successfully used the rollback method for a group of SQL
commands using the PostGresql JDBC driver?

Thanks

Ryan




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: JDBC and transactions
  2002-04-23 20:23 JDBC and transactions Ryan Chambers <ryan@squaretrade.com>
@ 2002-04-23 20:48 ` Dave Cramer <Dave@micro-automation.net>
  2002-04-23 21:09   ` Re: JDBC and transactions Ryan Chambers <ryan@squaretrade.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Dave Cramer @ 2002-04-23 20:48 UTC (permalink / raw)
  To: Ryan Chambers <ryan@squaretrade.com>; +Cc: pgsql-jdbc

Ryan,

This certainly should work as you have it. The only thing I can think of
is the persistToDb code is doing a commit? What does the postgres
backend log report?

Dave

On Tue, 2002-04-23 at 16:23, Ryan Chambers wrote:
> 
> Hi
> 
> I'm trying to figure out how to use transactions with JDBC and PostGresql,
> without much success. In particular, I can't figure out how to mark the
> start of a transaction and how to do a rollback.
> 
> I'm iterating through a list of objects and I insert them into the DB one by
> one. However, it is possible to get a NoSuchElement runtime exception while
> going through the list. If this happens, I want to rollback all the previous
> inserts.
> 
> The code/pseudo-code looks like this.
> 
> Connection conn = // get the connection
> conn.setAutoCommit(false);
> 
> boolean rollBack = false;
> 
> while(myList.hasNext()) {
> 	MyObject obj = null;
> 	try {
> 		obj = (MyObject)myList.next();
> 	}
> 	catch(NoSuchElementException e) {
> 		rollBack = true;
> 		break;
> 	}
> 
> 	try {
> 		persistToDB(conn, obj);
> 	}
> 	catch(SQLException e) {
> 		rollBack = true;
> 		break;
> 	}
> }
> 
> if(rollBack) {
> 	try {
> 		conn.rollback();
> 	}
> 	catch(SQLException e) {}
> }
> else {
> 	try {
> 		conn.commit();
> 	}
> 	catch(SQLException e) {}
> }
> 
> conn.close();
> 
> 
> I run this code and, in order to test it, deliberately throw a
> NoSuchElementException. My code correctly detects this and calls the
> rollback method on the connection. But when I look in the database, the rows
> before the exception have been inserted, and the rollback hasn't done
> anything. So how do I specify the start of the transaction? I've even tried
> executing Statements and PreparedStatements with the query "begin", trying
> to emulate the PostGresql "begin" syntax, but this doesn't work either. I
> know the syntax of the statements I use to persist the object are good, and
> I'm not seeing any exceptions when I do any of the SQL commands. (I ignore
> some of the exceptions because these haven't caused my any problems).
> 
> Has anybody successfully used the rollback method for a group of SQL
> commands using the PostGresql JDBC driver?
> 
> Thanks
> 
> Ryan
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
> 
> http://www.postgresql.org/users-lounge/docs/faq.html
> 
> 






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: JDBC and transactions
  2002-04-23 20:23 JDBC and transactions Ryan Chambers <ryan@squaretrade.com>
  2002-04-23 20:48 ` Re: JDBC and transactions Dave Cramer <Dave@micro-automation.net>
@ 2002-04-23 21:09   ` Ryan Chambers <ryan@squaretrade.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Ryan Chambers @ 2002-04-23 21:09 UTC (permalink / raw)
  To: Dave Cramer <Dave@micro-automation.net>; +Cc: pgsql-jdbc


Hi Dave

Arrghh... I _was_ doing a commit in the persistToDb() method. That's my
fault; it was some sloppy cutting-and-pasting of code. Everything is working
okay now.

Thanks for your help.

Ryan


-----Original Message-----
From: pgsql-jdbc-owner@postgresql.org
[mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Dave Cramer
Sent: Tuesday, April 23, 2002 1:48 PM
To: Ryan Chambers
Cc: pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] JDBC and transactions


Ryan,

This certainly should work as you have it. The only thing I can think of
is the persistToDb code is doing a commit? What does the postgres
backend log report?

Dave

On Tue, 2002-04-23 at 16:23, Ryan Chambers wrote:
>
> Hi
>
> I'm trying to figure out how to use transactions with JDBC and PostGresql,
> without much success. In particular, I can't figure out how to mark the
> start of a transaction and how to do a rollback.
>
> I'm iterating through a list of objects and I insert them into the DB one
by
> one. However, it is possible to get a NoSuchElement runtime exception
while
> going through the list. If this happens, I want to rollback all the
previous
> inserts.
>
> The code/pseudo-code looks like this.
>
> Connection conn = // get the connection
> conn.setAutoCommit(false);
>
> boolean rollBack = false;
>
> while(myList.hasNext()) {
> 	MyObject obj = null;
> 	try {
> 		obj = (MyObject)myList.next();
> 	}
> 	catch(NoSuchElementException e) {
> 		rollBack = true;
> 		break;
> 	}
>
> 	try {
> 		persistToDB(conn, obj);
> 	}
> 	catch(SQLException e) {
> 		rollBack = true;
> 		break;
> 	}
> }
>
> if(rollBack) {
> 	try {
> 		conn.rollback();
> 	}
> 	catch(SQLException e) {}
> }
> else {
> 	try {
> 		conn.commit();
> 	}
> 	catch(SQLException e) {}
> }
>
> conn.close();
>
>
> I run this code and, in order to test it, deliberately throw a
> NoSuchElementException. My code correctly detects this and calls the
> rollback method on the connection. But when I look in the database, the
rows
> before the exception have been inserted, and the rollback hasn't done
> anything. So how do I specify the start of the transaction? I've even
tried
> executing Statements and PreparedStatements with the query "begin", trying
> to emulate the PostGresql "begin" syntax, but this doesn't work either. I
> know the syntax of the statements I use to persist the object are good,
and
> I'm not seeing any exceptions when I do any of the SQL commands. (I ignore
> some of the exceptions because these haven't caused my any problems).
>
> Has anybody successfully used the rollback method for a group of SQL
> commands using the PostGresql JDBC driver?
>
> Thanks
>
> Ryan
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>
>




---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* JDBC and transactions
@ 2005-04-01 23:39 Chris White (cjwhite) <cjwhite@cisco.com>
  2005-04-02 07:33 ` Re: JDBC and transactions Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Chris White (cjwhite) @ 2005-04-01 23:39 UTC (permalink / raw)
  To: 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
 

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: JDBC and transactions
  2005-04-01 23:39 JDBC and transactions Chris White (cjwhite) <cjwhite@cisco.com>
@ 2005-04-02 07:33 ` Kris Jurka <books@ejurka.com>
  2005-04-03 20:27   ` Re: JDBC and transactions Chris White (cjwhite) <cjwhite@cisco.com>
  2005-04-03 22:39   ` Re: JDBC and transactions Chris White (cjwhite) <cjwhite@cisco.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Kris Jurka @ 2005-04-02 07:33 UTC (permalink / raw)
  To: Chris White (cjwhite) <cjwhite@cisco.com>; +Cc: 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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: JDBC and transactions
  2005-04-01 23:39 JDBC and transactions Chris White (cjwhite) <cjwhite@cisco.com>
  2005-04-02 07:33 ` Re: JDBC and transactions Kris Jurka <books@ejurka.com>
@ 2005-04-03 20:27   ` Chris White (cjwhite) <cjwhite@cisco.com>
  1 sibling, 0 replies; 8+ messages in thread

From: Chris White (cjwhite) @ 2005-04-03 20:27 UTC (permalink / raw)
  To: 'Kris Jurka' <books@ejurka.com>; +Cc: pgsql-jdbc

Thanks that helps me solve my problem. 

-----Original Message-----
From: Kris Jurka [mailto:books@ejurka.com] 
Sent: Friday, April 01, 2005 11:33 PM
To: Chris White (cjwhite)
Cc: pgsql-jdbc@postgresql.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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: JDBC and transactions
  2005-04-01 23:39 JDBC and transactions Chris White (cjwhite) <cjwhite@cisco.com>
  2005-04-02 07:33 ` Re: JDBC and transactions Kris Jurka <books@ejurka.com>
@ 2005-04-03 22:39   ` Chris White (cjwhite) <cjwhite@cisco.com>
  2005-04-04 17:09     ` Re: JDBC and transactions Kris Jurka <books@ejurka.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Chris White (cjwhite) @ 2005-04-03 22:39 UTC (permalink / raw)
  To: 'Kris Jurka' <books@ejurka.com>; +Cc: 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@ejurka.com] 
Sent: Friday, April 01, 2005 11:33 PM
To: Chris White (cjwhite)
Cc: pgsql-jdbc@postgresql.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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: JDBC and transactions
  2005-04-01 23:39 JDBC and transactions Chris White (cjwhite) <cjwhite@cisco.com>
  2005-04-02 07:33 ` Re: JDBC and transactions Kris Jurka <books@ejurka.com>
  2005-04-03 22:39   ` Re: JDBC and transactions Chris White (cjwhite) <cjwhite@cisco.com>
@ 2005-04-04 17:09     ` Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Kris Jurka @ 2005-04-04 17:09 UTC (permalink / raw)
  To: Chris White (cjwhite) <cjwhite@cisco.com>; +Cc: 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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2005-04-04 17:09 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-04-23 20:23 JDBC and transactions Ryan Chambers <ryan@squaretrade.com>
2002-04-23 20:48 ` Dave Cramer <Dave@micro-automation.net>
2002-04-23 21:09   ` Ryan Chambers <ryan@squaretrade.com>
2005-04-01 23:39 JDBC and transactions Chris White (cjwhite) <cjwhite@cisco.com>
2005-04-02 07:33 ` Kris Jurka <books@ejurka.com>
2005-04-03 20:27   ` Chris White (cjwhite) <cjwhite@cisco.com>
2005-04-03 22:39   ` Chris White (cjwhite) <cjwhite@cisco.com>
2005-04-04 17:09     ` Kris Jurka <books@ejurka.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox