[Mulgara-svn] r1019 - branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver

ronald at mulgara.org ronald at mulgara.org
Mon Jun 23 23:06:45 UTC 2008


Author: ronald
Date: 2008-06-23 16:06:43 -0700 (Mon, 23 Jun 2008)
New Revision: 1019

Modified:
   branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver/AdvDatabaseSessionUnitTest.java
   branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver/MulgaraInternalTransactionFactory.java
Log:
Fix bug that was probably introduced in r1010 and created tests cases for it:
after a failed operation the session should be reset automatically when in
auto-commit mode (i.e. not marked as failed).


Modified: branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver/AdvDatabaseSessionUnitTest.java
===================================================================
--- branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver/AdvDatabaseSessionUnitTest.java	2008-06-23 11:33:16 UTC (rev 1018)
+++ branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver/AdvDatabaseSessionUnitTest.java	2008-06-23 23:06:43 UTC (rev 1019)
@@ -131,7 +131,8 @@
     suite.addTest(new AdvDatabaseSessionUnitTest("testCreateModel"));
     suite.addTest(new AdvDatabaseSessionUnitTest("testInsertionBlankNodes"));
     suite.addTest(new AdvDatabaseSessionUnitTest("testAutoCommitTransactionOps"));
-    suite.addTest(new AdvDatabaseSessionUnitTest("testTransactionFailure"));
+    suite.addTest(new AdvDatabaseSessionUnitTest("testExplicitTransactionFailure"));
+    suite.addTest(new AdvDatabaseSessionUnitTest("testAutoCommitTransactionFailure"));
 
     return suite;
   }
@@ -1223,21 +1224,21 @@
 
 
   /**
-   * Test various operations after a transaction fails.
+   * Test various operations after an explicit transaction fails.
    */
-  public void testTransactionFailure() {
-    logger.info("Testing transactionFailure");
+  public void testExplicitTransactionFailure() {
+    logger.info("Testing transactionExplicitFailure");
 
     try {
       // query after failure should fail
-      shouldFail(new TestOp() {
+      shouldFailExplicit(new TestOp() {
         public void run(Session session) throws Exception {
           session.query(createQuery(modelURI)).close();
         }
       }, "Query in failed transaction did not fail");
 
       // insert after failure should fail
-      shouldFail(new TestOp() {
+      shouldFailExplicit(new TestOp() {
         public void run(Session session) throws Exception {
           session.insert(modelURI, Collections.singleton((Triple)new TripleImpl(
                                                     new URIReferenceImpl(URI.create("test:a")),
@@ -1247,28 +1248,28 @@
       }, "Insert in failed transaction did not fail");
 
       // commit after failure should fail
-      shouldFail(new TestOp() {
+      shouldFailExplicit(new TestOp() {
         public void run(Session session) throws Exception {
           session.commit();
         }
       }, "Commit in failed transaction did not fail");
 
       // rollback after failure should succeed
-      shouldSucceed(new TestOp() {
+      shouldSucceedExplicit(new TestOp() {
         public void run(Session session) throws Exception {
           session.rollback();
         }
       });
 
       // setAutoCommit(false) after failure should fail
-      shouldFail(new TestOp() {
+      shouldFailExplicit(new TestOp() {
         public void run(Session session) throws Exception {
           session.setAutoCommit(false);
         }
       }, "setAutoCommit(false) in failed transaction did not fail");
 
       // setAutoCommit(true) after failure should succeed
-      shouldSucceed(new TestOp() {
+      shouldSucceedExplicit(new TestOp() {
         public void run(Session session) throws Exception {
           session.setAutoCommit(true);
         }
@@ -1278,15 +1279,15 @@
     }
   }
 
-  private void shouldFail(TestOp op, String msg) throws Exception {
-    testTransactionFailureOp(op, true, msg);
+  private void shouldFailExplicit(TestOp op, String msg) throws Exception {
+    testExplicitTransactionFailureOp(op, true, msg);
   }
 
-  private void shouldSucceed(TestOp op) throws Exception {
-    testTransactionFailureOp(op, false, null);
+  private void shouldSucceedExplicit(TestOp op) throws Exception {
+    testExplicitTransactionFailureOp(op, false, null);
   }
 
-  private void testTransactionFailureOp(TestOp op, boolean shouldFail, String msg) throws Exception {
+  private void testExplicitTransactionFailureOp(TestOp op, boolean shouldFail, String msg) throws Exception {
     Session session = database.newSession();
     try {
       // start tx
@@ -1322,6 +1323,104 @@
     }
   }
 
+  /**
+   * Test various operations after an operation in auto-commit fails.
+   */
+  public void testAutoCommitTransactionFailure() {
+    logger.info("Testing transactionAutoCommitFailure");
+
+    try {
+      // query after failure should succeed
+      shouldSucceedImplicit(new TestOp() {
+        public void run(Session session) throws Exception {
+          session.query(createQuery(modelURI)).close();
+        }
+      });
+
+      // insert after failure should succeed
+      shouldSucceedImplicit(new TestOp() {
+        public void run(Session session) throws Exception {
+          session.insert(modelURI, Collections.singleton((Triple)new TripleImpl(
+                                                    new URIReferenceImpl(URI.create("test:a")),
+                                                    new URIReferenceImpl(URI.create("test:b")),
+                                                    new URIReferenceImpl(URI.create("test:c")))));
+        }
+      });
+
+      // commit should fail
+      shouldFailImplicit(new TestOp() {
+        public void run(Session session) throws Exception {
+          session.commit();
+        }
+      }, "Commit after failed query did not fail");
+
+      // rollback should fail
+      shouldFailImplicit(new TestOp() {
+        public void run(Session session) throws Exception {
+          session.rollback();
+        }
+      }, "Rollback after failed query did not fail");
+
+      // setAutoCommit(false) after failure should succeed
+      shouldSucceedImplicit(new TestOp() {
+        public void run(Session session) throws Exception {
+          session.setAutoCommit(false);
+          session.setAutoCommit(true);
+        }
+      });
+
+      // setAutoCommit(true) after failure should succeed
+      shouldSucceedImplicit(new TestOp() {
+        public void run(Session session) throws Exception {
+          session.setAutoCommit(true);
+        }
+      });
+    } catch (Exception e) {
+      fail(e);
+    }
+  }
+
+  private void shouldFailImplicit(TestOp op, String msg) throws Exception {
+    testImplicitTransactionFailureOp(op, true, msg);
+  }
+
+  private void shouldSucceedImplicit(TestOp op) throws Exception {
+    testImplicitTransactionFailureOp(op, false, null);
+  }
+
+  private void testImplicitTransactionFailureOp(TestOp op, boolean shouldFail, String msg) throws Exception {
+    Session session = database.newSession();
+    try {
+      // run bad query -> failed tx
+      try {
+        session.query(createQuery(URI.create("urn:no:such:model")));
+        fail("Bad query failed to throw exception");
+      } catch (QueryException qe) {
+      }
+
+      // run test op, verify it succeeds/fails
+      if (shouldFail) {
+        try {
+          op.run(session);
+          fail(msg);
+        } catch (QueryException qe) {
+        }
+      } else {
+        op.run(session);
+      }
+
+      // verify we're good to go
+      session.query(createQuery(modelURI)).close();
+
+      session.setAutoCommit(false);
+      session.query(createQuery(modelURI)).close();
+      session.setAutoCommit(true);
+    } finally {
+      session.close();
+    }
+  }
+
+
   private static interface TestOp {
     public void run(Session session) throws Exception;
   }

Modified: branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver/MulgaraInternalTransactionFactory.java
===================================================================
--- branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver/MulgaraInternalTransactionFactory.java	2008-06-23 11:33:16 UTC (rev 1018)
+++ branches/mgr-121-lockrecovery/src/jar/resolver/java/org/mulgara/resolver/MulgaraInternalTransactionFactory.java	2008-06-23 23:06:43 UTC (rev 1019)
@@ -418,7 +418,8 @@
     try {
       try {
         // Make sure this cleans up the transaction metadata - this transaction is DEAD!
-        if (transaction == writeTransaction) {
+        SessionInfo sessInfo = sessionInfoMap.get(xaSessionMap.get(transaction));
+        if (!sessInfo.autoCommit && transaction == writeTransaction) {
           failedSessions.add(xaSessionMap.get(transaction));
         }
         transactionComplete(transaction);




More information about the Mulgara-svn mailing list