Author: lektran
Date: Thu Oct 22 03:19:21 2009
New Revision: 828289

URL: http://svn.apache.org/viewvc?rev=828289&view=rev
Log:
When scheduling a service to be run do it within it's own transaction.  
This resolves a bug I encountered where a long running (couple of minutes) 
service was scheduling jobs but the poller was unable to get a lock on those 
JobSandbox rows which prevented the poller from queuing any jobs at all for the 
duration of the service.

Modified:
    
ofbiz/trunk/framework/service/src/org/ofbiz/service/GenericAbstractDispatcher.java

Modified: 
ofbiz/trunk/framework/service/src/org/ofbiz/service/GenericAbstractDispatcher.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/GenericAbstractDispatcher.java?rev=828289&r1=828288&r2=828289&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/service/src/org/ofbiz/service/GenericAbstractDispatcher.java
 (original)
+++ 
ofbiz/trunk/framework/service/src/org/ofbiz/service/GenericAbstractDispatcher.java
 Thu Oct 22 03:19:21 2009
@@ -21,10 +21,13 @@
 import java.util.Date;
 import java.util.Map;
 
+import javax.transaction.Transaction;
 import javax.transaction.xa.XAException;
 
 import org.ofbiz.service.calendar.RecurrenceRule;
 import org.ofbiz.entity.Delegator;
+import org.ofbiz.entity.transaction.GenericTransactionException;
+import org.ofbiz.entity.transaction.TransactionUtil;
 import org.ofbiz.security.Security;
 import org.ofbiz.security.authz.Authorization;
 import org.ofbiz.service.jms.JmsListenerFactory;
@@ -60,21 +63,53 @@
      * @see org.ofbiz.service.LocalDispatcher#schedule(java.lang.String, 
java.lang.String, java.lang.String, java.util.Map, long, int, int, int, long, 
int)
      */
     public void schedule(String jobName, String poolName, String serviceName, 
Map<String, ? extends Object> context, long startTime, int frequency, int 
interval, int count, long endTime, int maxRetry) throws GenericServiceException 
{
+        Transaction suspendedTransaction = null;
         try {
-            getJobManager().schedule(jobName, poolName, serviceName, context, 
startTime, frequency, interval, count, endTime, maxRetry);
-
-            if (Debug.verboseOn()) {
-                Debug.logVerbose("[LocalDispatcher.schedule] : Current time : 
" + (new Date()).getTime(), module);
-                Debug.logVerbose("[LocalDispatcher.schedule] : Runtime      : 
" + startTime, module);
-                Debug.logVerbose("[LocalDispatcher.schedule] : Frequency    : 
" + frequency, module);
-                Debug.logVerbose("[LocalDispatcher.schedule] : Interval     : 
" + interval, module);
-                Debug.logVerbose("[LocalDispatcher.schedule] : Count        : 
" + count, module);
-                Debug.logVerbose("[LocalDispatcher.schedule] : EndTime      : 
" + endTime, module);
-                Debug.logVerbose("[LocalDispatcher.schedule] : MazRetry     : 
" + maxRetry, module);
+            boolean beganTransaction = false;
+            suspendedTransaction = TransactionUtil.suspend();
+            try {
+                beganTransaction = TransactionUtil.begin();
+                try {
+                    getJobManager().schedule(jobName, poolName, serviceName, 
context, startTime, frequency, interval, count, endTime, maxRetry);
+
+                    if (Debug.verboseOn()) {
+                        Debug.logVerbose("[LocalDispatcher.schedule] : Current 
time : " + (new Date()).getTime(), module);
+                        Debug.logVerbose("[LocalDispatcher.schedule] : Runtime 
     : " + startTime, module);
+                        Debug.logVerbose("[LocalDispatcher.schedule] : 
Frequency    : " + frequency, module);
+                        Debug.logVerbose("[LocalDispatcher.schedule] : 
Interval     : " + interval, module);
+                        Debug.logVerbose("[LocalDispatcher.schedule] : Count   
     : " + count, module);
+                        Debug.logVerbose("[LocalDispatcher.schedule] : EndTime 
     : " + endTime, module);
+                        Debug.logVerbose("[LocalDispatcher.schedule] : 
MazRetry     : " + maxRetry, module);
+                    }
+
+                } catch (JobManagerException jme) {
+                    throw new GenericServiceException(jme.getMessage(), jme);
+                }
+            } catch (Exception e) {
+                String errMsg = "General error while scheduling job";
+                Debug.logError(e, errMsg, module);
+                try {
+                    TransactionUtil.rollback(beganTransaction, errMsg, e);
+                } catch (GenericTransactionException gte1) {
+                    Debug.logError(gte1, "Unable to rollback transaction", 
module);
+                }
+            } finally {
+                try {
+                    TransactionUtil.commit(beganTransaction);
+                } catch (GenericTransactionException gte2) {
+                    Debug.logError(gte2, "Unable to commit scheduled job", 
module);
+                }
+            }
+        } catch (GenericTransactionException gte) {
+            Debug.logError(gte, "Error suspending transaction while scheduling 
job", module);
+        } finally {
+            if (suspendedTransaction != null) {
+                try {
+                    TransactionUtil.resume(suspendedTransaction);
+                } catch (GenericTransactionException gte3) {
+                    Debug.logError(gte3, "Error resuming suspended transaction 
after scheduling job", module);
+                }
             }
-
-        } catch (JobManagerException e) {
-            throw new GenericServiceException(e.getMessage(), e);
         }
     }
 


Reply via email to