From f43b9c395f2b80b86cd7d55b084b6db3627667f1 Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Mon, 9 Dec 2019 16:32:05 -0500 Subject: [PATCH] NO-JIRA Fixing memory leak on testsuite AbstractJDBCDriver would hold an instance to AbstractJDBCDriver through an innner class, that would hold an ActiveMQServerImpl. That means Servers would be leaking for the entire duration of the testsuite when using JDBC. --- .../store/drivers/AbstractJDBCDriver.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java index 9629587b5f..7168cf025a 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java @@ -27,6 +27,7 @@ import java.sql.Statement; import java.util.Arrays; import java.util.Properties; import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Stream; import org.apache.activemq.artemis.jdbc.store.logging.LoggingConnection; @@ -293,21 +294,28 @@ public abstract class AbstractJDBCDriver { } } + private static AtomicBoolean shutAdded = new AtomicBoolean(false); + + private static class ShutdownDerby extends Thread { + @Override + public void run() { + try { + DriverManager.getConnection("jdbc:derby:;shutdown=true"); + } catch (Exception e) { + } + } + + } + private Driver getDriver(String className) { try { Driver driver = (Driver) Class.forName(className).newInstance(); // Shutdown the derby if using the derby embedded driver. if (className.equals("org.apache.derby.jdbc.EmbeddedDriver")) { - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - try { - DriverManager.getConnection("jdbc:derby:;shutdown=true"); - } catch (Exception e) { - } - } - }); + if (shutAdded.compareAndSet(false, true)) { + Runtime.getRuntime().addShutdownHook(new ShutdownDerby()); + } } return driver; } catch (ClassNotFoundException cnfe) {