[exo-jcr-commits] exo-jcr SVN: r4549 - jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Jun 20 10:52:17 EDT 2011


Author: nzamosenchuk
Date: 2011-06-20 10:52:16 -0400 (Mon, 20 Jun 2011)
New Revision: 4549

Modified:
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCUtils.java
Log:
EXOJCR-1374: rolling back to stored savePoint in case of failed query, for transaction to operate in a normal way, like no fails existed.

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCUtils.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCUtils.java	2011-06-17 13:27:57 UTC (rev 4548)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCUtils.java	2011-06-20 14:52:16 UTC (rev 4549)
@@ -24,6 +24,7 @@
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Savepoint;
 import java.sql.Statement;
 
 /**
@@ -51,14 +52,36 @@
    {
       Statement stmt = null;
       ResultSet trs = null;
+      Savepoint savePoint = null;
+      Boolean autoCommit = null;
       try
       {
+         // safe get autoCommit value
+         autoCommit = con.getAutoCommit();
+         // set autoCommit to true
+         con.setAutoCommit(false);
+         // make a savepoint (snapshot)
+         savePoint = con.setSavepoint();
          stmt = con.createStatement();
          trs = stmt.executeQuery("SELECT count(*) FROM " + tableName);
          return trs.next();
       }
       catch (SQLException e)
       {
+         if (savePoint != null)
+         {
+            try
+            {
+               // revert state to savePoint after failed query in transaction. This will allow following queries to 
+               // be executed in an ordinary way, like no failed query existed.
+               // Obligatory operation for PostgreSQL.
+               con.rollback(savePoint);
+            }
+            catch (SQLException e1)
+            {
+               LOG.error("Can't rollback to savePoint", e1);
+            }
+         }
          if (LOG.isDebugEnabled())
          {
             LOG.debug("SQLException occurs while checking the table " + tableName, e);
@@ -67,6 +90,17 @@
       }
       finally
       {
+         if (autoCommit != null)
+         {
+            try
+            {
+               con.setAutoCommit(autoCommit);
+            }
+            catch (SQLException e)
+            {
+               LOG.error("Can't set autoCommit value back", e);
+            }
+         }
          if (trs != null)
          {
             try



More information about the exo-jcr-commits mailing list