2727import java .util .List ;
2828import java .util .Map ;
2929import java .util .TimeZone ;
30- import java .util .concurrent .Executors ;
31- import java .util .concurrent .ScheduledExecutorService ;
32- import java .util .concurrent .TimeUnit ;
3330
3431import javax .management .StandardMBean ;
3532
3633import org .apache .log4j .Logger ;
3734
3835import com .cloud .utils .DateUtil ;
39- import com .cloud .utils .concurrency .NamedThreadFactory ;
4036import com .cloud .utils .exception .CloudRuntimeException ;
4137import com .cloud .utils .mgmt .JmxUtil ;
4238import com .cloud .utils .time .InaccurateClock ;
@@ -62,15 +58,18 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
6258 private long _msId ;
6359
6460 private static Merovingian2 s_instance = null ;
65- ScheduledExecutorService _executor = null ;
66- Connection _conn = null ;
61+ ConnectionConcierge _concierge = null ;
6762
6863 private Merovingian2 (long msId ) {
6964 super (MerovingianMBean .class , false );
7065 _msId = msId ;
71- String result = resetDbConnection ();
72- if (!result .equalsIgnoreCase ("Success" )) {
73- throw new CloudRuntimeException ("Unable to initialize a connection to the database for locking purposes due to " + result );
66+ Connection conn = null ;
67+ try {
68+ conn = Transaction .getStandaloneConnectionWithException ();
69+ _concierge = new ConnectionConcierge ("LockMaster" , conn , true , true );
70+ } catch (SQLException e ) {
71+ s_logger .error ("Unable to get a new db connection" , e );
72+ throw new CloudRuntimeException ("Unable to initialize a connection to the database for locking purposes: " , e );
7473 }
7574 }
7675
@@ -90,42 +89,6 @@ public static Merovingian2 getLockMaster() {
9089 return s_instance ;
9190 }
9291
93- @ Override
94- public String resetDbConnection () {
95- if (_conn != null ) {
96- try {
97- _conn .close ();
98- } catch (Throwable th ) {
99- s_logger .error ("Unable to close connection" , th );
100- }
101- }
102-
103- try {
104- _conn = Transaction .getStandaloneConnectionWithException ();
105- _conn .setAutoCommit (true );
106- } catch (SQLException e ) {
107- s_logger .error ("Unable to get a new db connection" , e );
108- return "Unable to initialize a connection to the database for locking purposes: " + e ;
109- }
110-
111- if (_conn == null ) {
112- return "Unable to initialize a connection to the database for locking purposes, shutdown this server!" ;
113- }
114-
115- if (_executor != null ) {
116- try {
117- _executor .shutdown ();
118- } catch (Throwable th ) {
119- s_logger .error ("Unable to shutdown the executor" , th );
120- }
121- }
122-
123- _executor = Executors .newScheduledThreadPool (1 , new NamedThreadFactory ("LockMasterConnectionKeepAlive" ));
124- _executor .schedule (new KeepAliveTask (), 10 , TimeUnit .SECONDS );
125-
126- return "Success" ;
127- }
128-
12992 public boolean acquire (String key , int timeInSeconds ) {
13093 Thread th = Thread .currentThread ();
13194 String threadName = th .getName ();
@@ -160,7 +123,7 @@ public boolean acquire(String key, int timeInSeconds) {
160123 protected boolean increment (String key , String threadName , int threadId ) {
161124 PreparedStatement pstmt = null ;
162125 try {
163- pstmt = _conn .prepareStatement (INCREMENT_SQL );
126+ pstmt = _concierge . conn () .prepareStatement (INCREMENT_SQL );
164127 pstmt .setString (1 , key );
165128 pstmt .setLong (2 , _msId );
166129 pstmt .setString (3 , threadName );
@@ -188,7 +151,7 @@ protected boolean doAcquire(String key, String threadName, int threadId) {
188151
189152 long startTime = InaccurateClock .getTime ();
190153 try {
191- pstmt = _conn .prepareStatement (ACQUIRE_SQL );
154+ pstmt = _concierge . conn () .prepareStatement (ACQUIRE_SQL );
192155 pstmt .setString (1 , key );
193156 pstmt .setLong (2 , _msId );
194157 pstmt .setString (3 , threadName );
@@ -226,7 +189,7 @@ protected Map<String, String> isLocked(String key) {
226189 PreparedStatement pstmt = null ;
227190 ResultSet rs = null ;
228191 try {
229- pstmt = _conn .prepareStatement (INQUIRE_SQL );
192+ pstmt = _concierge . conn () .prepareStatement (INQUIRE_SQL );
230193 pstmt .setString (1 , key );
231194 rs = pstmt .executeQuery ();
232195 if (!rs .next ()) {
@@ -258,7 +221,7 @@ public void cleanupThisServer() {
258221 public void cleanupForServer (long msId ) {
259222 PreparedStatement pstmt = null ;
260223 try {
261- pstmt = _conn .prepareStatement (CLEANUP_MGMT_LOCKS_SQL );
224+ pstmt = _concierge . conn () .prepareStatement (CLEANUP_MGMT_LOCKS_SQL );
262225 pstmt .setLong (1 , _msId );
263226 pstmt .executeUpdate ();
264227 } catch (SQLException e ) {
@@ -279,7 +242,7 @@ public boolean release(String key) {
279242 String threadName = th .getName ();
280243 int threadId = System .identityHashCode (th );
281244 try {
282- pstmt = _conn .prepareStatement (DECREMENT_SQL );
245+ pstmt = _concierge . conn () .prepareStatement (DECREMENT_SQL );
283246 pstmt .setString (1 , key );
284247 pstmt .setLong (2 , _msId );
285248 pstmt .setString (3 , threadName );
@@ -291,7 +254,7 @@ public boolean release(String key) {
291254 }
292255 if (rows == 1 ) {
293256 pstmt .close ();
294- pstmt = _conn .prepareStatement (RELEASE_SQL );
257+ pstmt = _concierge . conn () .prepareStatement (RELEASE_SQL );
295258 pstmt .setString (1 , key );
296259 pstmt .setLong (2 , _msId );
297260 int result = pstmt .executeUpdate ();
@@ -336,7 +299,7 @@ protected List<Map<String, String>> getLocks(String sql, Long msId) {
336299 PreparedStatement pstmt = null ;
337300 ResultSet rs = null ;
338301 try {
339- pstmt = _conn .prepareStatement (sql );
302+ pstmt = _concierge . conn () .prepareStatement (sql );
340303 if (msId != null ) {
341304 pstmt .setLong (1 , msId );
342305 }
@@ -384,7 +347,7 @@ public List<Map<String, String>> getLocksAcquiredBy(long msId, String threadName
384347 PreparedStatement pstmt = null ;
385348 ResultSet rs = null ;
386349 try {
387- pstmt = _conn .prepareStatement (SELECT_THREAD_LOCKS_SQL );
350+ pstmt = _concierge . conn () .prepareStatement (SELECT_THREAD_LOCKS_SQL );
388351 pstmt .setLong (1 , msId );
389352 pstmt .setString (2 , threadName );
390353 rs = pstmt .executeQuery ();
@@ -411,7 +374,7 @@ public void cleanupThread() {
411374
412375 PreparedStatement pstmt = null ;
413376 try {
414- pstmt = _conn .prepareStatement (CLEANUP_THREAD_LOCKS_SQL );
377+ pstmt = _concierge . conn () .prepareStatement (CLEANUP_THREAD_LOCKS_SQL );
415378 pstmt .setLong (1 , _msId );
416379 pstmt .setString (2 , threadName );
417380 pstmt .setInt (3 , threadId );
@@ -433,7 +396,7 @@ public void cleanupThread() {
433396 public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing (String key ) {
434397 PreparedStatement pstmt = null ;
435398 try {
436- pstmt = _conn .prepareStatement (RELEASE_LOCK_SQL );
399+ pstmt = _concierge . conn () .prepareStatement (RELEASE_LOCK_SQL );
437400 pstmt .setString (1 , key );
438401 int rows = pstmt .executeUpdate ();
439402 return rows > 0 ;
@@ -442,25 +405,4 @@ public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) {
442405 return false ;
443406 }
444407 }
445-
446- protected class KeepAliveTask implements Runnable {
447- @ Override
448- public void run () {
449- PreparedStatement pstmt = null ; // Should this even be prepared everytime?
450- try {
451- pstmt = _conn .prepareStatement ("SELECT 1" );
452- pstmt .executeQuery ();
453- } catch (Throwable th ) {
454- s_logger .error ("Unable to keep the db connection alive for locking purposes!" , th );
455- } finally {
456- if (pstmt != null ) {
457- try {
458- pstmt .close ();
459- } catch (SQLException e ) {
460- }
461- }
462- }
463- }
464- }
465-
466408}
0 commit comments