From 2932a2a711f49a89aa1c6f61e2d28ffe003e92bf Mon Sep 17 00:00:00 2001 From: Martyn Taylor Date: Fri, 3 Jun 2016 16:30:14 +0100 Subject: [PATCH] Ensure ResultSet is closed on table operation --- .../artemis/jdbc/store/JDBCUtils.java | 19 ++++++++++++++----- .../artemis/tests/util/ActiveMQTestBase.java | 13 ++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java index c63e3233e8..61293af59f 100644 --- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java +++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/JDBCUtils.java @@ -65,12 +65,21 @@ public class JDBCUtils { public static void createTableIfNotExists(Connection connection, String tableName, String sql) throws SQLException { logger.tracef("Validating if table %s didn't exist before creating", tableName); - ResultSet rs = connection.getMetaData().getTables(null, null, tableName, null); - if (!rs.next()) { - logger.tracef("Table %s did not exist, creating it with SQL=%s", tableName, sql); - Statement statement = connection.createStatement(); - statement.executeUpdate(sql); + try { + connection.setAutoCommit(false); + try (ResultSet rs = connection.getMetaData().getTables(null, null, tableName, null)) { + if (rs != null && !rs.next()) { + logger.tracef("Table %s did not exist, creating it with SQL=%s", tableName, sql); + Statement statement = connection.createStatement(); + statement.executeUpdate(sql); + } + } + connection.commit(); } + catch (SQLException e) { + connection.rollback(); + } + } public static SQLProvider getSQLProvider(String driverClass, String tableName) { diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java index 31ad1d6504..064091d395 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java @@ -43,6 +43,7 @@ import java.net.ServerSocket; import java.sql.Connection; import java.sql.Driver; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; @@ -467,10 +468,16 @@ public abstract class ActiveMQTestBase extends Assert { Statement statement = connection.createStatement(); try { for (String tableName : tableNames) { + connection.setAutoCommit(false); SQLProvider sqlProvider = JDBCUtils.getSQLProvider(getJDBCClassName(), tableName); - ResultSet rs = connection.getMetaData().getTables(null, null, sqlProvider.getTableName(), null); - if (rs.next()) { - statement.execute("DROP TABLE " + sqlProvider.getTableName()); + try (ResultSet rs = connection.getMetaData().getTables(null, null, sqlProvider.getTableName(), null)) { + if (rs.next()) { + statement.execute("DROP TABLE " + sqlProvider.getTableName()); + } + connection.commit(); + } + catch (SQLException e) { + connection.rollback(); } } }