Author: jstrachan
Date: Mon Jun 11 05:21:11 2007
New Revision: 546120

URL: http://svn.apache.org/viewvc?view=rev&rev=546120
Log:
applied patch for AMQ-1263 to create a new connection each time in case the 
connection goes stale

Modified:
    
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DefaultDatabaseLocker.java

Modified: 
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DefaultDatabaseLocker.java
URL: 
http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DefaultDatabaseLocker.java?view=diff&rev=546120&r1=546119&r2=546120
==============================================================================
--- 
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DefaultDatabaseLocker.java
 (original)
+++ 
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DefaultDatabaseLocker.java
 Mon Jun 11 05:21:11 2007
@@ -16,13 +16,10 @@
  */
 package org.apache.activemq.store.jdbc;
 
-import org.apache.activemq.Service;
-import org.apache.activemq.broker.BrokerService;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import javax.sql.DataSource;
-
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
@@ -30,16 +27,16 @@
 /**
  * Represents an exclusive lock on a database to avoid multiple brokers
  * running against the same logical database.
- * 
+ *
  * @version $Revision: $
  */
 public class DefaultDatabaseLocker implements DatabaseLocker {
     private static final Log log = 
LogFactory.getLog(DefaultDatabaseLocker.class);
-    
     private final DataSource dataSource;
     private final Statements statements;
     private long sleepTime = 1000;
     private Connection connection;
+    private PreparedStatement statement;
     private boolean stopping;
 
     public DefaultDatabaseLocker(DataSource dataSource, Statements statements) 
{
@@ -49,27 +46,47 @@
 
     public void start() throws Exception {
         stopping = false;
-        connection = dataSource.getConnection();
-        connection.setAutoCommit(false);
-        
+
         log.info("Attempting to acquire the exclusive lock to become the 
Master broker");
-               String sql = statements.getLockCreateStatement();
-               PreparedStatement statement = connection.prepareStatement(sql);
+
         while (true) {
             try {
+                connection = dataSource.getConnection();
+                connection.setAutoCommit(false);
+                String sql = statements.getLockCreateStatement();
+                statement = connection.prepareStatement(sql);
                 statement.execute();
-                               break;
+                break;
             }
             catch (Exception e) {
-                if (stopping) { 
-                    throw new Exception("Cannot start broker as being asked to 
shut down. Interupted attempt to acquire lock: " + e, e);
+                if (stopping) {
+                    throw new Exception("Cannot start broker as being asked to 
shut down. Interrupted attempt to acquire lock: " + e, e);
                 }
                 log.error("Failed to acquire lock: " + e, e);
+                if (null != statement) {
+                    try {
+                        statement.close();
+                    }
+                    catch (SQLException e1) {
+                        log.warn("Caught while closing statement: " + e1, e1);
+                    }
+                    statement = null;
+                }
+                if (null != connection) {
+                    try {
+                        connection.close();
+                    }
+                    catch (SQLException e1) {
+                        log.warn("Caught while closing connection: " + e1, e1);
+                    }
+                    connection = null;
+                }
             }
+
             log.debug("Sleeping for " + sleepTime + " milli(s) before trying 
again to get the lock...");
             Thread.sleep(sleepTime);
         }
-        
+
         log.info("Becoming the master on dataSource: " + dataSource);
     }
 


Reply via email to