geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r468214 - /geronimo/server/trunk/modules/geronimo-timer/src/main/java/org/apache/geronimo/timer/TransactionalExecutorTask.java
Date Fri, 27 Oct 2006 00:51:49 GMT
Author: dain
Date: Thu Oct 26 17:51:48 2006
New Revision: 468214

URL: http://svn.apache.org/viewvc?view=rev&rev=468214
Log:
When tx is rollback only, don't try to commit and don't update the threadPooledTimer

Modified:
    geronimo/server/trunk/modules/geronimo-timer/src/main/java/org/apache/geronimo/timer/TransactionalExecutorTask.java

Modified: geronimo/server/trunk/modules/geronimo-timer/src/main/java/org/apache/geronimo/timer/TransactionalExecutorTask.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/modules/geronimo-timer/src/main/java/org/apache/geronimo/timer/TransactionalExecutorTask.java?view=diff&rev=468214&r1=468213&r2=468214
==============================================================================
--- geronimo/server/trunk/modules/geronimo-timer/src/main/java/org/apache/geronimo/timer/TransactionalExecutorTask.java
(original)
+++ geronimo/server/trunk/modules/geronimo-timer/src/main/java/org/apache/geronimo/timer/TransactionalExecutorTask.java
Thu Oct 26 17:51:48 2006
@@ -18,6 +18,7 @@
 package org.apache.geronimo.timer;
 
 import javax.transaction.TransactionManager;
+import javax.transaction.Status;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -44,41 +45,81 @@
     }
 
     public void run() {
-        for (int tries = 0; tries < repeatCount; tries++) {
-            try {
-                transactionManager.begin();
-            } catch (Exception e) {
-                log.warn("Exception occured while starting container transaction", e);
-                break;
-            }
-            try {
+        try {
+            // try to do the work until it succeeded or we reach the repeat count
+            boolean succeeded = false;
+            for (int tries = 0; !succeeded && tries < repeatCount; tries++) {
                 try {
-                    userTask.run();
-                } catch (Exception e) {
-                    log.warn("Exception occured while running user task", e);
+                    if (!beginWork()) {
+                        break;
+                    }
+
+                    work();
+                } finally {
+                    succeeded = completeWork();
                 }
+            }
+
+            // if this was a one time thing, remove the job
+            if (workInfo.isOneTime()) {
+                threadPooledTimer.removeWorkInfo(workInfo);
+            }
+
+            // if we didn't succeed, log it
+            if (!succeeded) {
+                log.warn("Failed to execute work successfully");
+            }
+        } catch (RuntimeException e) {
+            log.warn("RuntimeException occured while running user task", e);
+            throw e;
+        } catch (Error e) {
+            log.warn("Error occured while running user task", e);
+            throw e;
+        }
+    }
+
+    private boolean beginWork() {
+        try {
+            transactionManager.begin();
+        } catch (Exception e) {
+            log.warn("Exception occured while starting container transaction", e);
+            return false;
+        }
+        return true;
+    }
+
+    private void work() {
+        try {
+            userTask.run();
+        } catch (Exception e) {
+            log.warn("Exception occured while running user task", e);
+        }
+    }
+
+    private boolean completeWork() {
+        try {
+            if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
+                // clean up the work persistent data
                 try {
                     threadPooledTimer.workPerformed(workInfo);
                 } catch (PersistenceException e) {
                     log.warn("Exception occured while updating timer persistent state", e);
                 }
-            } finally {
-                try {
-                    transactionManager.commit();
-                    if (workInfo.isOneTime()) {
-                        threadPooledTimer.removeWorkInfo(workInfo);
-                    }
-                    // todo this is a very weird code structure.... returning from a finally
is very confusing
-                    return;
-                } catch (Exception e) {
-                    log.warn("Exception occured while completing container transaction",
e);
-                }
+
+                // commit the tx
+                transactionManager.commit();
+
+                // all is cool
+                return true;
+            } else {
+                // tx was marked rollback, so roll it back
+                transactionManager.rollback();
             }
+        } catch (Exception e) {
+            log.warn("Exception occured while completing container transaction", e);
         }
-        if (workInfo.isOneTime()) {
-            threadPooledTimer.removeWorkInfo(workInfo);
-        }
-        log.warn("Failed to execute work successfully");
+        // something bad happened
+        return false;
     }
 
 }



Mime
View raw message