From commits-return-6249-apmail-activemq-commits-archive=activemq.apache.org@activemq.apache.org Mon Jun 11 12:21:37 2007 Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 31996 invoked from network); 11 Jun 2007 12:21:35 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 11 Jun 2007 12:21:35 -0000 Received: (qmail 4728 invoked by uid 500); 11 Jun 2007 12:21:38 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 4691 invoked by uid 500); 11 Jun 2007 12:21:38 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 4678 invoked by uid 99); 11 Jun 2007 12:21:38 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Jun 2007 05:21:38 -0700 X-ASF-Spam-Status: No, hits=-98.6 required=10.0 tests=ALL_TRUSTED,INFO_TLD,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Jun 2007 05:21:33 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 95A601A981D; Mon, 11 Jun 2007 05:21:12 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r546120 - /activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/DefaultDatabaseLocker.java Date: Mon, 11 Jun 2007 12:21:12 -0000 To: commits@activemq.apache.org From: jstrachan@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070611122112.95A601A981D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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); }