001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.activemq.store.jdbc.adapter;
018
019 import java.io.IOException;
020 import java.sql.PreparedStatement;
021 import java.sql.ResultSet;
022 import java.sql.SQLException;
023
024 import org.apache.activemq.store.jdbc.DefaultDatabaseLocker;
025 import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029 /**
030 * Represents an exclusive lock on a database to avoid multiple brokers running
031 * against the same logical database.
032 *
033 * @org.apache.xbean.XBean element="transact-database-locker"
034 *
035 */
036 public class TransactDatabaseLocker extends DefaultDatabaseLocker {
037 private static final Logger LOG = LoggerFactory.getLogger(TransactDatabaseLocker.class);
038
039 @Override
040 public void doStart() throws Exception {
041 stopping = false;
042
043 LOG.info("Attempting to acquire the exclusive lock to become the Master broker");
044 PreparedStatement statement = null;
045 while (true) {
046 try {
047 connection = dataSource.getConnection();
048 connection.setAutoCommit(false);
049 String sql = statements.getLockCreateStatement();
050 statement = connection.prepareStatement(sql);
051 if (statement.getMetaData() != null) {
052 ResultSet rs = statement.executeQuery();
053 // if not already locked the statement below blocks until lock acquired
054 rs.next();
055 } else {
056 statement.execute();
057 }
058 break;
059 } catch (Exception e) {
060 if (stopping) {
061 throw new Exception("Cannot start broker as being asked to shut down. Interrupted attempt to acquire lock: " + e, e);
062 }
063
064 if (exceptionHandler != null) {
065 try {
066 exceptionHandler.handle(e);
067 } catch (Throwable handlerException) {
068 LOG.error("The exception handler " + exceptionHandler.getClass().getCanonicalName() + " threw this exception: " + handlerException
069 + " while trying to handle this excpetion: " + e, handlerException);
070 }
071
072 } else {
073 LOG.error("Failed to acquire lock: " + e, e);
074 }
075 } finally {
076
077 if (null != statement) {
078 try {
079 statement.close();
080 } catch (SQLException e1) {
081 LOG.warn("Caught while closing statement: " + e1, e1);
082 }
083 statement = null;
084 }
085 }
086
087 LOG.debug("Sleeping for " + lockAcquireSleepInterval + " milli(s) before trying again to get the lock...");
088 try {
089 Thread.sleep(lockAcquireSleepInterval);
090 } catch (InterruptedException ie) {
091 LOG.warn("Master lock retry sleep interrupted", ie);
092 }
093 }
094
095 LOG.info("Becoming the master on dataSource: " + dataSource);
096 }
097
098 }