]> granicus.if.org Git - postgresql/commitdiff
Attached is a patch that fixes
authorBruce Momjian <bruce@momjian.us>
Mon, 10 Sep 2001 14:54:22 +0000 (14:54 +0000)
committerBruce Momjian <bruce@momjian.us>
Mon, 10 Sep 2001 14:54:22 +0000 (14:54 +0000)
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

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

index b9c2444c79c72d90b7db461f9b78dbe320b8a9e0..8d52bccce2671a24b8bd3b5cbca0822f76bc6aa5 100644 (file)
@@ -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