[Jboss-cvs] JBossAS SVN: r56311 - branches/Branch_4_0/messaging/src/main/org/jboss/mq/pm/jdbc2

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Aug 27 06:26:42 EDT 2006


Author: luc.texier at jboss.com
Date: 2006-08-27 06:26:38 -0400 (Sun, 27 Aug 2006)
New Revision: 56311

Modified:
   branches/Branch_4_0/messaging/src/main/org/jboss/mq/pm/jdbc2/PersistenceManager.java
Log:
JBAS-3466  added retry logic

Modified: branches/Branch_4_0/messaging/src/main/org/jboss/mq/pm/jdbc2/PersistenceManager.java
===================================================================
--- branches/Branch_4_0/messaging/src/main/org/jboss/mq/pm/jdbc2/PersistenceManager.java	2006-08-27 10:19:28 UTC (rev 56310)
+++ branches/Branch_4_0/messaging/src/main/org/jboss/mq/pm/jdbc2/PersistenceManager.java	2006-08-27 10:26:38 UTC (rev 56311)
@@ -1353,6 +1353,14 @@
       Connection c = null;
       PreparedStatement stmt = null;
       boolean threadWasInterrupted = Thread.interrupted();
+
+      // How many times we retry to execute the statement
+      // in case of a SQLException e.g when deadlock was detected
+      // Feedback from the field shows that 1 retry is usually enough.
+      // TODO: should be a configurable attribute
+      final int MAX_TRIES = 5;
+
+
       try
       {
          c = this.getConnection();
@@ -1368,10 +1376,6 @@
                stmt = c.prepareStatement(DELETE_MESSAGE);
                stmt.setLong(1, messageRef.messageId);
                stmt.setString(2, messageRef.getPersistentKey());
-               int rc = stmt.executeUpdate();
-               if (rc != 1)
-                  throw new SpyJMSException(
-                     "Could not delete the message from the database: delete affected " + rc + " rows");
 
                // Adrian Brock:
                // Remove the message from the cache, but don't 
@@ -1392,16 +1396,45 @@
                stmt.setString(2, "D");
                stmt.setLong(3, messageRef.messageId);
                stmt.setString(4, messageRef.getPersistentKey());
-               int rc = stmt.executeUpdate();
-               if (rc != 1)
-                  throw new SpyJMSException(
-                     "Could not mark the message as deleted in the database: update affected " + rc + " rows");
             }
+
+             int tries = 0;
+             while (true)
+             {
+                try
+                {
+                   int rc = stmt.executeUpdate();
+
+                   if (tries > 0)
+                   {
+                      if (rc != 1)
+                         throw new SpyJMSException(
+                           "Could not mark the message as deleted in the database: update affected " + rc + " rows");
+
+                      log.warn("Remove operation worked after " +tries +" retries");
+                   }
+                   break;
+                }
+                catch (SQLException e)
+                {
+                   log.warn("SQLException caught - assuming deadlock detected, try:" + (tries + 1), e);
+                   tries++;
+                   if (tries == MAX_TRIES)
+                   {
+                      log.error("Retried " + tries + " times, now giving up");
+                      throw new IllegalStateException("Could not remove message after " +tries + "attempts");
+                   }
+                   log.warn("Trying again after a pause");
+                   //Now we wait for a random amount of time to minimise risk of deadlock
+                   Thread.sleep((long)(Math.random() * 500));
+                }
+             }
+
             if (trace)
                log.trace("Removed message " + messageRef + " transaction=" + txId);
          }
       }
-      catch (SQLException e)
+      catch (Exception e)
       {
          tms.setRollbackOnly();
          throw new SpyJMSException("Could not remove message: " + messageRef, e);




More information about the jboss-cvs-commits mailing list