From 38b840a61f4e909f6196fd3cf36e884f586ded58 Mon Sep 17 00:00:00 2001 From: Gary Tully Date: Wed, 10 Jun 2009 14:59:02 +0000 Subject: [PATCH] resolve https://issues.apache.org/activemq/browse/AMQ-2285 - pull file locking outside setting opened state so that an open does not ocurr unless the lock is available, sorts out Npe on shutdown git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@783384 13f79535-47bb-0310-9956-ffa450edef68 --- .../store/kahadb/MessageDatabase.java | 39 +++++++++---------- .../store/kahadb/KahaDBStoreBrokerTest.java | 2 - 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java b/activemq-core/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java index 95b3f5e33a..0c497f6438 100644 --- a/activemq-core/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java +++ b/activemq-core/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java @@ -218,26 +218,25 @@ public class MessageDatabase { * @throws IOException */ public void open() throws IOException { + File lockFileName = new File(directory, "lock"); + lockFile = new LockFile(lockFileName, true); + if (failIfDatabaseIsLocked) { + lockFile.lock(); + } else { + while (true) { + try { + lockFile.lock(); + break; + } catch (IOException e) { + LOG.info("Database "+lockFileName+" is locked... waiting " + (DATABASE_LOCKED_WAIT_DELAY / 1000) + " seconds for the database to be unlocked. Reason: " + e); + try { + Thread.sleep(DATABASE_LOCKED_WAIT_DELAY); + } catch (InterruptedException e1) { + } + } + } + } if( opened.compareAndSet(false, true) ) { - File lockFileName = new File(directory, "lock"); - lockFile = new LockFile(lockFileName, true); - if (failIfDatabaseIsLocked) { - lockFile.lock(); - } else { - while (true) { - try { - lockFile.lock(); - break; - } catch (IOException e) { - LOG.info("Database "+lockFileName+" is locked... waiting " + (DATABASE_LOCKED_WAIT_DELAY / 1000) + " seconds for the database to be unlocked. Reason: " + e); - try { - Thread.sleep(DATABASE_LOCKED_WAIT_DELAY); - } catch (InterruptedException e1) { - } - } - } - } - getJournal().start(); loadPageFile(); @@ -312,7 +311,7 @@ public class MessageDatabase { public void unload() throws IOException, InterruptedException { synchronized (indexMutex) { - if( pageFile.isLoaded() ) { + if( pageFile != null && pageFile.isLoaded() ) { metadata.state = CLOSED_STATE; metadata.firstInProgressTransactionLocation = getFirstInProgressTxLocation(); diff --git a/activemq-core/src/test/java/org/apache/activemq/store/kahadb/KahaDBStoreBrokerTest.java b/activemq-core/src/test/java/org/apache/activemq/store/kahadb/KahaDBStoreBrokerTest.java index aa8793681f..3051764c74 100644 --- a/activemq-core/src/test/java/org/apache/activemq/store/kahadb/KahaDBStoreBrokerTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/store/kahadb/KahaDBStoreBrokerTest.java @@ -17,11 +17,9 @@ package org.apache.activemq.store.kahadb; import java.io.File; -import java.net.URI; import junit.framework.Test; -import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerTest;