pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedPGJDBC 8 transaction problem
3+ messages / 3 participants
[nested] [flat]
* PGJDBC 8 transaction problem
@ 2006-06-05 13:01 Rodrigo Willian Bonatto <bonatto@diuno.com.br>
2006-06-05 13:14 ` Re: PGJDBC 8 transaction problem Dave Cramer <pg@fastcrypt.com>
2006-06-05 13:25 ` Re: PGJDBC 8 transaction problem Oliver Jowett <oliver@opencloud.com>
0 siblings, 2 replies; 3+ messages in thread
From: Rodrigo Willian Bonatto @ 2006-06-05 13:01 UTC (permalink / raw)
To: pgsql-jdbc
Hi,
I have a problem using transactions with PGJDBC 8.0 or greater.
For example:
In the database I have the following record in the "employee" table:
name age
John 27
If I use the PGJDBC 7.4 to update and list the same record in a single
statement like this:
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(url, "postgres",
"postgres");
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
StringBuffer query = new StringBuffer();
query.append("BEGIN;");
query.append("update employee set age = 28 where age = 27;");
query.append("COMMIT;");
query.append("select name from employee where age = 28");
ResultSet rs = stmt.executeQuery(query.toString());
Here the resultset will return "John", but if I use PGJDBC driver
version 8 or greater, the statement return any result.
Any ideas?
Thanks in advanced!
Rodrigo
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: PGJDBC 8 transaction problem
2006-06-05 13:01 PGJDBC 8 transaction problem Rodrigo Willian Bonatto <bonatto@diuno.com.br>
@ 2006-06-05 13:14 ` Dave Cramer <pg@fastcrypt.com>
1 sibling, 0 replies; 3+ messages in thread
From: Dave Cramer @ 2006-06-05 13:14 UTC (permalink / raw)
To: Rodrigo Willian Bonatto <bonatto@diuno.com.br>; +Cc: pgsql-jdbc
Rodrigo,
You don't need the begin, and end
you use connection.commit() instead
Dave
On 5-Jun-06, at 9:01 AM, Rodrigo Willian Bonatto wrote:
> Hi,
>
>
>
> I have a problem using transactions with PGJDBC 8.0 or greater.
>
>
>
> For example:
>
>
>
> In the database I have the following record in the “employee” table:
>
>
>
> name age
>
> John 27
>
>
>
> If I use the PGJDBC 7.4 to update and list the same record in a
> single statement like this:
>
>
>
> Class.forName("org.postgresql.Driver");
>
> Connection conn = DriverManager.getConnection(url, "postgres",
> "postgres");
>
> conn.setAutoCommit(false);
>
> Statement stmt = conn.createStatement();
>
> StringBuffer query = new StringBuffer();
>
> query.append("BEGIN;");
>
> query.append("update employee set age = 28 where age = 27;");
>
> query.append("COMMIT;");
>
> query.append("select name from employee where age = 28");
>
> ResultSet rs = stmt.executeQuery(query.toString());
>
>
>
> Here the resultset will return “John”, but if I use PGJDBC driver
> version 8 or greater, the statement return any result.
>
>
>
> Any ideas?
>
>
>
> Thanks in advanced!
>
>
>
> Rodrigo
>
>
>
>
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: PGJDBC 8 transaction problem
2006-06-05 13:01 PGJDBC 8 transaction problem Rodrigo Willian Bonatto <bonatto@diuno.com.br>
@ 2006-06-05 13:25 ` Oliver Jowett <oliver@opencloud.com>
1 sibling, 0 replies; 3+ messages in thread
From: Oliver Jowett @ 2006-06-05 13:25 UTC (permalink / raw)
To: Rodrigo Willian Bonatto <bonatto@diuno.com.br>; +Cc: pgsql-jdbc
Rodrigo Willian Bonatto wrote:
> query.append("BEGIN;");
> query.append("update employee set age = 28 where age = 27;");
> query.append("COMMIT;");
> query.append("select name from employee where age = 28");
> ResultSet rs = stmt.executeQuery(query.toString());
>
> Here the resultset will return “John”, but if I use PGJDBC driver
> version 8 or greater, the statement return any result.
The more recent drivers implement support for returning multiple
resultsets from a query correctly, which older drivers didn't do. Your
Statement will actually have four results associated with it -- one for
each of BEGIN, UPDATE, COMMIT, SELECT. I would expect executeQuery() to
throw an exception because the query returned something other than a
single resultset.
You will need to use Statement.execute() / Statement.getMoreResults() /
Statement.getResultSet() to step to the 4th result and retrieve the
SELECT's results (you could also get at the UPDATE's update count in a
similar way).
Also, you should avoid explicit BEGIN/COMMIT statements if you can --
Connection.setAutocommit() / Connection.commit() is the recommended way
to manage transaction boundaries.
-O
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-06-05 13:25 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-06-05 13:01 PGJDBC 8 transaction problem Rodrigo Willian Bonatto <bonatto@diuno.com.br>
2006-06-05 13:14 ` Dave Cramer <pg@fastcrypt.com>
2006-06-05 13:25 ` Oliver Jowett <oliver@opencloud.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