diff --git a/activemq-jdbc-store/src/main/java/org/apache/activemq/store/jdbc/DataSourceServiceSupport.java b/activemq-jdbc-store/src/main/java/org/apache/activemq/store/jdbc/DataSourceServiceSupport.java index 9790d3d8c6..cf7cb0d932 100644 --- a/activemq-jdbc-store/src/main/java/org/apache/activemq/store/jdbc/DataSourceServiceSupport.java +++ b/activemq-jdbc-store/src/main/java/org/apache/activemq/store/jdbc/DataSourceServiceSupport.java @@ -89,6 +89,10 @@ abstract public class DataSourceServiceSupport extends LockableServiceSupport { } public static DataSource createDataSource(String homeDir) throws IOException { + return createDataSource(homeDir, "derbydb"); + } + + public static DataSource createDataSource(String homeDir, String dbName) throws IOException { // Setup the Derby datasource. System.setProperty("derby.system.home", homeDir); @@ -96,7 +100,7 @@ abstract public class DataSourceServiceSupport extends LockableServiceSupport { System.setProperty("derby.storage.pageCacheSize", "100"); final EmbeddedDataSource ds = new EmbeddedDataSource(); - ds.setDatabaseName("derbydb"); + ds.setDatabaseName(dbName); ds.setCreateDatabase("create"); return ds; } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/network/NetworkBrokerDetachTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/network/NetworkBrokerDetachTest.java index ceef43c8cb..02f9a44c72 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/network/NetworkBrokerDetachTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/network/NetworkBrokerDetachTest.java @@ -108,11 +108,15 @@ public class NetworkBrokerDetachTest { @After public void cleanup() throws Exception { - networkedBroker.stop(); - networkedBroker.waitUntilStopped(); + if (networkedBroker != null) { + networkedBroker.stop(); + networkedBroker.waitUntilStopped(); + } - broker.stop(); - broker.waitUntilStopped(); + if (broker != null) { + broker.stop(); + broker.waitUntilStopped(); + } } @Test diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/store/jdbc/JDBCNetworkBrokerDetachTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/store/jdbc/JDBCNetworkBrokerDetachTest.java index 4881b2f252..aa56862bf1 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/store/jdbc/JDBCNetworkBrokerDetachTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/store/jdbc/JDBCNetworkBrokerDetachTest.java @@ -16,19 +16,43 @@ */ package org.apache.activemq.store.jdbc; +import java.sql.SQLException; +import java.util.LinkedList; import org.apache.activemq.broker.BrokerService; import org.apache.activemq.network.NetworkBrokerDetachTest; import org.apache.derby.jdbc.EmbeddedDataSource; +import org.junit.After; public class JDBCNetworkBrokerDetachTest extends NetworkBrokerDetachTest { + LinkedList dataSources = new LinkedList<>(); protected void configureBroker(BrokerService broker) throws Exception { JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter(); - EmbeddedDataSource dataSource = (EmbeddedDataSource) jdbc.getDataSource(); - dataSource.setDatabaseName(broker.getBrokerName()); - dataSource.getConnection().close(); // ensure derby for brokerName is initialized + try { + EmbeddedDataSource dataSource = (EmbeddedDataSource) DataSourceServiceSupport.createDataSource(jdbc.getDataDirectoryFile().getCanonicalPath(), broker.getBrokerName()); + dataSource.getConnection().close(); // ensure derby for brokerName is initialized + jdbc.setDataSource(dataSource); + dataSources.add(dataSource); + } catch (SQLException e) { + e.printStackTrace(); + Exception n = e.getNextException(); + while (n != null) { + n.printStackTrace(); + if (n instanceof SQLException) { + n = ((SQLException) n).getNextException(); + } + } + throw e; + } broker.setPersistenceAdapter(jdbc); broker.setUseVirtualTopics(false); } - + + @After + public void shutdownDataSources() throws Exception { + for (EmbeddedDataSource ds: dataSources) { + DataSourceServiceSupport.shutdownDefaultDataSource(ds); + } + dataSources.clear(); + } }