This closes #558

This commit is contained in:
Clebert Suconic 2016-06-03 12:03:00 -04:00
commit 14516af7c7
2 changed files with 24 additions and 8 deletions

View File

@ -65,13 +65,22 @@ 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()) {
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) {
if (driverClass.contains("derby")) {

View File

@ -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,11 +468,17 @@ 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);
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();
}
}
}
catch (Throwable e) {