applied patch for AMQ-1263 to create a new connection each time in case the connection goes stale

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@546120 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2007-06-11 12:21:11 +00:00
parent 8f8ff58f8c
commit b852dc87fc
1 changed files with 31 additions and 14 deletions

View File

@ -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;
@ -35,11 +32,11 @@ import java.sql.SQLException;
*/
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,23 +46,43 @@ public class DefaultDatabaseLocker implements DatabaseLocker {
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;
}
catch (Exception e) {
if (stopping) {
throw new Exception("Cannot start broker as being asked to shut down. Interupted attempt to acquire lock: " + e, e);
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);
}