From c69bb04acc13e0c1aa4e0b5e7910f6850d71173b Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Mon, 10 Sep 2001 14:54:22 +0000 Subject: [PATCH] Attached is a patch that fixes ConnectionTest.testTransactionIsolation() in the JDBC driver's test suite. This reduces the number of failures of the test suite from 7 to 6. The patch fixes the test case itself, rather than the driver. In addition to the change described in my posting below, I fixed the part of the test with autocommit enabled. The author of the test assumed that setting the transaction isolation level would have no effect, but in fact it does. Perhaps the test case worked with pre-7.1 behaviour, when the JDBC driver set the isolation level in every transaction, instead of using "set session characteristics". Anyway, now it works with a backend built from current CVS and the behaviour is JDBC compliant. I also extended the test case by changing the isolation level before beginning a transaction and verifying it inside the transaction. Regards, Ren? Pijlman --- .../postgresql/test/jdbc2/ConnectionTest.java | 120 +++++++++++++----- 1 file changed, 89 insertions(+), 31 deletions(-) diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java index b9c2444c79..8d52bccce2 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java @@ -10,7 +10,7 @@ import java.sql.*; * * PS: Do you know how difficult it is to type on a train? ;-) * - * $Id: ConnectionTest.java,v 1.3 2001/09/07 22:17:48 momjian Exp $ + * $Id: ConnectionTest.java,v 1.4 2001/09/10 14:54:22 momjian Exp $ */ public class ConnectionTest extends TestCase { @@ -203,36 +203,94 @@ public class ConnectionTest extends TestCase { } } - /** - * Transaction Isolation Levels - */ - public void testTransactionIsolation() { - try { - Connection con = JDBC2Tests.openDB(); - - con.setAutoCommit(false); - - // These are the currently available ones - con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); - assert(con.getTransactionIsolation()==Connection.TRANSACTION_SERIALIZABLE); - - con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); - assert(con.getTransactionIsolation()==Connection.TRANSACTION_READ_COMMITTED); - - // Now turn on AutoCommit. Transaction Isolation doesn't work outside of - // a transaction, so they should return READ_COMMITTED at all times! - con.setAutoCommit(true); - con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); - assert(con.getTransactionIsolation()==Connection.TRANSACTION_READ_COMMITTED); - - con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); - assert(con.getTransactionIsolation()==Connection.TRANSACTION_READ_COMMITTED); - - JDBC2Tests.closeDB(con); - } catch(SQLException ex) { - assert(ex.getMessage(),false); - } - } + /** + * Transaction Isolation Levels + */ + public void testTransactionIsolation() + { + try + { + Connection con = JDBC2Tests.openDB(); + + // PostgreSQL defaults to READ COMMITTED + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_READ_COMMITTED ); + + // Begin a transaction + con.setAutoCommit(false); + + // The isolation level should not have changed + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_READ_COMMITTED ); + + // Now change the default for future transactions + con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); + + // Since the call to setTransactionIsolation() above was made + // inside the transaction, the isolation level of the current + // transaction did not change. It affects only future transactions. + // This behaviour is recommended by the JDBC spec. + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_READ_COMMITTED ); + + // Begin a new transaction + con.commit(); + + // Now we should see the new isolation level + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_SERIALIZABLE ); + + // Repeat the steps above with the transition back to + // READ COMMITTED. + con.setTransactionIsolation( + Connection.TRANSACTION_READ_COMMITTED ); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_SERIALIZABLE ); + con.commit(); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_READ_COMMITTED ); + + // Now run some tests with autocommit enabled. + con.setAutoCommit(true); + + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_READ_COMMITTED ); + + con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_SERIALIZABLE ); + + con.setTransactionIsolation( + Connection.TRANSACTION_READ_COMMITTED ); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_READ_COMMITTED ); + + // Test if a change of isolation level before beginning the + // transaction affects the isolation level inside the transaction. + con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_SERIALIZABLE ); + con.setAutoCommit(false); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_SERIALIZABLE ); + con.setAutoCommit(true); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_SERIALIZABLE ); + con.setTransactionIsolation( + Connection.TRANSACTION_READ_COMMITTED ); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_READ_COMMITTED ); + con.setAutoCommit(false); + assertEquals( con.getTransactionIsolation(), + Connection.TRANSACTION_READ_COMMITTED ); + + JDBC2Tests.closeDB(con); + } + catch ( SQLException ex ) + { + fail( ex.getMessage() ); + } + } /** * JDBC2 Type mappings -- 2.40.0