Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c3e7b3c

Browse files
author
Barry Lind
committed
Fixed support in jdbc for 7.3 server autocommit. With these changes the
jdbc regression tests pass for both autocommit on and autocommit off Modified Files: jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java jdbc/org/postgresql/test/jdbc2/ConnectionTest.java
1 parent f789bd3 commit c3e7b3c

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.postgresql.util.*;
1515

1616

17-
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v 1.10 2002/10/01 00:39:01 davec Exp $
17+
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v 1.11 2002/10/17 05:33:52 barry Exp $
1818
* This class defines methods of the jdbc1 specification. This class is
1919
* extended by org.postgresql.jdbc2.AbstractJdbc2Connection which adds the jdbc2
2020
* methods. The real Connection class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Connection
@@ -362,6 +362,29 @@ public void openConnection(String host, int port, Properties info, String databa
362362

363363
String dbEncoding = resultSet.getString(2);
364364
encoding = Encoding.getEncoding(dbEncoding, info.getProperty("charSet"));
365+
//In 7.3 we are forced to do a second roundtrip to handle the case
366+
//where a database may not be running in autocommit mode
367+
//jdbc by default assumes autocommit is on until setAutoCommit(false)
368+
//is called. Therefore we need to ensure a new connection is
369+
//initialized to autocommit on.
370+
if (haveMinimumServerVersion("7.3"))
371+
{
372+
java.sql.ResultSet acRset =
373+
ExecSQL("show autocommit");
374+
375+
if (!acRset.next())
376+
{
377+
throw new PSQLException("postgresql.con.failed", "failed getting autocommit status");
378+
}
379+
//if autocommit is currently off we need to turn it on
380+
//note that we will be in a transaction because the select above
381+
//will have initiated the transaction so we need a commit
382+
//to make the setting permanent
383+
if (acRset.getString(1).equals("off"))
384+
{
385+
ExecSQL("set autocommit = on; commit;");
386+
}
387+
}
365388

366389
// Initialise object handling
367390
initObjectTypes();
@@ -896,10 +919,26 @@ public void setAutoCommit(boolean autoCommit) throws SQLException
896919
if (this.autoCommit == autoCommit)
897920
return ;
898921
if (autoCommit)
899-
ExecSQL("end");
922+
{
923+
if (haveMinimumServerVersion("7.3"))
924+
{
925+
//We do the select to ensure a transaction is in process
926+
//before we do the commit to avoid warning messages
927+
//from issuing a commit without a transaction in process
928+
ExecSQL("select 1; commit; set autocommit = on;");
929+
}
930+
else
931+
{
932+
ExecSQL("end");
933+
}
934+
}
900935
else
901936
{
902-
if (haveMinimumServerVersion("7.1"))
937+
if (haveMinimumServerVersion("7.3"))
938+
{
939+
ExecSQL("set autocommit = off; " + getIsolationLevelSQL());
940+
}
941+
else if (haveMinimumServerVersion("7.1"))
903942
{
904943
ExecSQL("begin;" + getIsolationLevelSQL());
905944
}
@@ -938,7 +977,11 @@ public void commit() throws SQLException
938977
{
939978
if (autoCommit)
940979
return ;
941-
if (haveMinimumServerVersion("7.1"))
980+
if (haveMinimumServerVersion("7.3"))
981+
{
982+
ExecSQL("commit; " + getIsolationLevelSQL());
983+
}
984+
else if (haveMinimumServerVersion("7.1"))
942985
{
943986
ExecSQL("commit;begin;" + getIsolationLevelSQL());
944987
}
@@ -962,7 +1005,14 @@ public void rollback() throws SQLException
9621005
{
9631006
if (autoCommit)
9641007
return ;
965-
if (haveMinimumServerVersion("7.1"))
1008+
if (haveMinimumServerVersion("7.3"))
1009+
{
1010+
//we don't automatically start a transaction
1011+
//but let the server functionality automatically start
1012+
//one when the first statement is executed
1013+
ExecSQL("rollback; " + getIsolationLevelSQL());
1014+
}
1015+
else if (haveMinimumServerVersion("7.1"))
9661016
{
9671017
ExecSQL("rollback; begin;" + getIsolationLevelSQL());
9681018
}

src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* PS: Do you know how difficult it is to type on a train? ;-)
1212
*
13-
* $Id: ConnectionTest.java,v 1.9 2002/08/14 20:35:40 barry Exp $
13+
* $Id: ConnectionTest.java,v 1.10 2002/10/17 05:33:52 barry Exp $
1414
*/
1515

1616
public class ConnectionTest extends TestCase
@@ -244,18 +244,28 @@ public void testTransactionIsolation()
244244
assertEquals(Connection.TRANSACTION_READ_COMMITTED,
245245
con.getTransactionIsolation());
246246

247+
248+
// Note the behavior on when a transaction starts is different
249+
// under 7.3 than previous versions. In 7.3 a transaction
250+
// starts with the first sql command, whereas previously
251+
// you were always in a transaction in autocommit=false
252+
// so we issue a select to ensure we are in a transaction
253+
Statement stmt = con.createStatement();
254+
stmt.executeQuery("select 1");
255+
247256
// Now change the default for future transactions
248257
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
249258

250259
// Since the call to setTransactionIsolation() above was made
251-
// inside the transaction, the isolation level of the current
252-
// transaction did not change. It affects only future transactions.
253-
// This behaviour is recommended by the JDBC spec.
254-
assertEquals(Connection.TRANSACTION_READ_COMMITTED,
260+
// inside the transaction, the isolation level of the current
261+
// transaction did not change. It affects only future ones.
262+
// This behaviour is recommended by the JDBC spec.
263+
assertEquals(Connection.TRANSACTION_READ_COMMITTED,
255264
con.getTransactionIsolation());
256265

257-
// Begin a new transaction
258-
con.commit();
266+
// Begin a new transaction
267+
con.commit();
268+
stmt.executeQuery("select 1");
259269

260270
// Now we should see the new isolation level
261271
assertEquals(Connection.TRANSACTION_SERIALIZABLE,

0 commit comments

Comments
 (0)