KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > store > jdbc > DefaultDatabaseLocker


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.store.jdbc;
18
19 import org.apache.activemq.Service;
20 import org.apache.activemq.broker.BrokerService;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.sql.DataSource JavaDoc;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 /**
31  * Represents an exclusive lock on a database to avoid multiple brokers
32  * running against the same logical database.
33  *
34  * @version $Revision: $
35  */

36 public class DefaultDatabaseLocker implements DatabaseLocker {
37     private static final Log log = LogFactory.getLog(DefaultDatabaseLocker.class);
38     
39     private final DataSource JavaDoc dataSource;
40     private final Statements statements;
41     private long sleepTime = 1000;
42     private Connection JavaDoc connection;
43     private boolean stopping;
44
45     public DefaultDatabaseLocker(DataSource JavaDoc dataSource, Statements statements) {
46         this.dataSource = dataSource;
47         this.statements = statements;
48     }
49
50     public void start() throws Exception JavaDoc {
51         stopping = false;
52         connection = dataSource.getConnection();
53         connection.setAutoCommit(false);
54         
55         log.info("Attempting to acquire the exclusive lock to become the Master broker");
56         String JavaDoc sql = statements.getLockCreateStatement();
57         PreparedStatement JavaDoc statement = connection.prepareStatement(sql);
58         while (true) {
59             try {
60                 statement.execute();
61                 break;
62             }
63             catch (Exception JavaDoc e) {
64                 if (stopping) {
65                     throw new Exception JavaDoc("Cannot start broker as being asked to shut down. Interupted attempt to acquire lock: " + e, e);
66                 }
67                 log.error("Failed to acquire lock: " + e, e);
68             }
69             log.debug("Sleeping for " + sleepTime + " milli(s) before trying again to get the lock...");
70             Thread.sleep(sleepTime);
71         }
72         
73         log.info("Becoming the master on dataSource: " + dataSource);
74     }
75
76     public void stop() throws Exception JavaDoc {
77         stopping = true;
78         if (connection != null) {
79             connection.rollback();
80             connection.close();
81         }
82     }
83
84     public boolean keepAlive() {
85         try {
86             PreparedStatement JavaDoc statement = connection.prepareStatement(statements.getLockUpdateStatement());
87             statement.setLong(1, System.currentTimeMillis());
88             int rows = statement.executeUpdate();
89             if (rows == 1) {
90                 return true;
91             }
92         }
93         catch (Exception JavaDoc e) {
94             log.error("Failed to update database lock: " + e, e);
95         }
96         return false;
97     }
98 }
99
Popular Tags