[Mulgara-svn] r1727 - trunk/src/jar/querylang/java/org/mulgara/protocol/http

pag at mulgara.org pag at mulgara.org
Wed Jun 10 03:01:30 UTC 2009


Author: pag
Date: 2009-06-09 20:01:30 -0700 (Tue, 09 Jun 2009)
New Revision: 1727

Modified:
   trunk/src/jar/querylang/java/org/mulgara/protocol/http/ProtocolServlet.java
Log:
Wrapped a transaction around multiple HTTP requests, and ensured that the HTTP session is never left in the middle of a transaction

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/http/ProtocolServlet.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/http/ProtocolServlet.java	2009-06-10 03:00:41 UTC (rev 1726)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/http/ProtocolServlet.java	2009-06-10 03:01:30 UTC (rev 1727)
@@ -53,6 +53,8 @@
 import org.mulgara.query.operation.DropGraph;
 import org.mulgara.query.operation.Insertion;
 import org.mulgara.query.operation.Load;
+import org.mulgara.query.operation.Rollback;
+import org.mulgara.query.operation.SetAutoCommit;
 import org.mulgara.server.ServerInfo;
 import org.mulgara.server.SessionFactoryProvider;
 import org.mulgara.util.functional.C;
@@ -468,32 +470,49 @@
     String queryStr = req.getParameter(QUERY_ARG);
     List<Command> cmds = getCommand(queryStr, req);
 
+    boolean tx = cmds.size() > 1;
+    if (tx) executeCommand(new SetAutoCommit(false), req);
     Object finalResult = null;
     Output finalOutputType = null;
 
     Object tmpResult = null;
     Output tmpOutputType = null;
-    for (Command cmd: cmds) {
-      tmpResult = executeCommand(cmd, req);
-      tmpOutputType = getOutputType(req, cmd);
-      // remember the last answer we see
-      if (finalResult instanceof Answer) {
+    try {
+      try {
+        for (Command cmd: cmds) {
+          tmpResult = executeCommand(cmd, req);
+          tmpOutputType = getOutputType(req, cmd);
+          // remember the last answer we see
+          if (tmpResult instanceof Answer) {
+            finalResult = tmpResult;
+            finalOutputType = tmpOutputType;
+          }
+        }
+      } catch (ServletException e) {
+        if (tx) executeCommand(new Rollback(), req);
+        throw e;
+      }
+
+      // if there is no result, then take the last one
+      if (finalResult == null) {
         finalResult = tmpResult;
         finalOutputType = tmpOutputType;
       }
+  
+      if (finalResult instanceof Answer) {
+        sendAnswer((Answer)finalResult, finalOutputType, resp);
+      } else {
+        sendStatus(finalResult, finalOutputType, resp);
+      }
+    } finally {
+      // always turn on autocommit since we can't leave a transaction running in HTTP
+      try {
+        executeCommand(new SetAutoCommit(true), req);
+      } catch (Exception e) {
+        // throw away
+        logger.error("Unable to close transaction", e);
+      }
     }
-
-    // if there is no result, then take the last one
-    if (finalResult == null) {
-      finalResult = tmpResult;
-      finalOutputType = tmpOutputType;
-    }
-
-    if (finalResult instanceof Answer) {
-      sendAnswer((Answer)finalResult, finalOutputType, resp);
-    } else {
-      sendStatus(finalResult, finalOutputType, resp);
-    }
   }
 
 




More information about the Mulgara-svn mailing list