You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2006/02/02 12:29:01 UTC

svn commit: r374363 - /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java

Author: arminw
Date: Thu Feb  2 03:28:56 2006
New Revision: 374363

URL: http://svn.apache.org/viewcvs?rev=374363&view=rev
Log:
fix issue, if OJB's batch statement mode is enabled and identity columns are used, OJB will always execute batch on insert
fix issue, if OJB's batch statement mode is enabled and optimistic locking is used, OJB will always execute batch on update and delete

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java?rev=374363&r1=374362&r2=374363&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java Thu Feb  2 03:28:56 2006
@@ -99,6 +99,8 @@
         PreparedStatement stmt = null;
         try
         {
+            // workaround for issue with optimistic locking with enabled batch mode
+            boolean batchSetting = preHandleBatchProcess(cld);
             stmt = sm.getDeleteStatement(cld);
             if (stmt == null)
             {
@@ -107,8 +109,7 @@
             }
 
             sm.bindDelete(stmt, cld, obj);
-            if (logger.isDebugEnabled())
-                logger.debug("executeDelete: " + stmt);
+            if (logger.isDebugEnabled()) logger.debug("executeDelete: " + stmt);
 
             // @todo: clearify semantics
             // thma: the following check is not secure. The object could be deleted *or* changed.
@@ -116,8 +117,10 @@
             // does is make sense to throw an OL exception if the object was changed?
             if (stmt.executeUpdate() == 0 && cld.isLocking()) //BRJ
             {
+                postHandleBatchProcess(batchSetting);
                 throw new OptimisticLockException("Object has been modified or deleted by someone else", obj);
             }
+            postHandleBatchProcess(batchSetting);
 
             // Harvest any return values.
             harvestReturnValues(cld.getDeleteProcedure(), obj, stmt);
@@ -493,22 +496,24 @@
         oldLockingValues = cld.getCurrentLockingValues(obj);
         try
         {
+            // workaround for issue with optimistic locking with enabled batch mode
+            boolean batchSetting = preHandleBatchProcess(cld);
             stmt = sm.getUpdateStatement(cld);
             if (stmt == null)
             {
                 logger.error("getUpdateStatement returned a null statement");
                 throw new PersistenceBrokerException("getUpdateStatement returned a null statement");
             }
-
             sm.bindUpdate(stmt, cld, obj);
-            if (logger.isDebugEnabled())
-                logger.debug("executeUpdate: " + stmt);
+
+            if (logger.isDebugEnabled()) logger.debug("executeUpdate: " + stmt);
 
             if ((stmt.executeUpdate() == 0) && cld.isLocking()) //BRJ
             {
+                postHandleBatchProcess(batchSetting);
                 throw new OptimisticLockException("Object has been modified by someone else", obj);
             }
-
+            postHandleBatchProcess(batchSetting);
             // Harvest any return values.
             harvestReturnValues(cld.getUpdateProcedure(), obj, stmt);
         }
@@ -762,7 +767,7 @@
         return true;
     }
 
-    protected void preSequenceProcess(ClassDescriptor cld, Object target) throws SequenceManagerException
+    protected void preSequenceProcess(final ClassDescriptor cld, final Object target) throws SequenceManagerException
     {
         // TODO: refactor auto-increment handling, auto-increment should only be supported by PK fields?
         // FieldDescriptor[] fields = cld.getPkFields();
@@ -783,7 +788,7 @@
         }
     }
 
-    protected void postSequenceProcess(ClassDescriptor cld, Object target) throws SequenceManagerException
+    protected void postSequenceProcess(final ClassDescriptor cld, final Object target) throws SequenceManagerException
     {
         // if database Identity Columns are used, query the id from database
         // other SequenceManager implementations will ignore this call
@@ -793,6 +798,41 @@
             broker.serviceConnectionManager().executeBatch();
             // lookup identity column PK value from DB and set PK in persistent object
             broker.serviceSequenceManager().afterStore(this, cld, target);
+        }
+    }
+
+    /**
+     * Workaround workaround to avoid problems with locking fields in batch mode.
+     * Only use in combination with method {@link #postHandleBatchProcess(boolean)}.
+     * @return The 'old' state of the batch mode setting. This value have to be used
+     * in method {@link #postHandleBatchProcess(boolean)} to restore the old batch mode state.
+     */
+    protected boolean preHandleBatchProcess(final ClassDescriptor cld)
+    {
+        // TODO: this is a workaround to avoid problems with locking fields in batch mode
+        if(cld.isLocking())
+        {
+            ConnectionManagerIF cm = broker.serviceConnectionManager();
+            if(cm.isBatchMode())
+            {
+                cm.executeBatch();
+                cm.setBatchMode(false);
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Workaround workaround to avoid problems with locking fields in batch mode.
+     * Only use in combination with method
+     * {@link #preHandleBatchProcess(org.apache.ojb.broker.metadata.ClassDescriptor)}.
+     */
+    protected void postHandleBatchProcess(final boolean batchModeSetting)
+    {
+        if(batchModeSetting)
+        {
+            broker.serviceConnectionManager().setBatchMode(true);
         }
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org