Fixed a lot of unclosed prepared statements, connections and resultset

This commit is contained in:
tastybento 2018-02-06 13:52:25 -08:00
parent 84d36a1a24
commit 06e61d858c

View File

@ -164,11 +164,12 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
//plugin.getLogger().info(setSql);
// Execute the statement
PreparedStatement collections = connection.prepareStatement(setSql);
try (PreparedStatement collections = connection.prepareStatement(setSql)) {
if (DEBUG)
plugin.getLogger().info("DEBUG: collections prepared statement = " + collections.toString());
collections.executeUpdate();
}
}
} else {
// The Java type is not in the hashmap, so we'll just guess that it can be stored in a string
// This should NOT be used in general because every type should be in the hashmap
@ -191,7 +192,6 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
} finally {
// Close the database properly
MySQLDatabaseResourceCloser.close(pstmt);
MySQLDatabaseResourceCloser.close(pstmt);
}
}
@ -525,7 +525,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
} finally {
// Close properly
MySQLDatabaseResourceCloser.close(preparedStatement);
MySQLDatabaseResourceCloser.close(connection);
MySQLDatabaseResourceCloser.close(preparedStatement);
}
}
@ -626,7 +626,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
try {
connection = databaseConnecter.createConnection();
String query = "SELECT " + getColumns(false) + " FROM `" + dataObject.getCanonicalName() + "` WHERE uniqueId = ? LIMIT 1";
PreparedStatement preparedStatement = connection.prepareStatement(query);
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, uniqueId);
if (DEBUG)
plugin.getLogger().info("DEBUG: load Object query = " + preparedStatement.toString());
@ -636,8 +636,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (!result.isEmpty()) {
return result.get(0);
}
}
return null;
} finally {
MySQLDatabaseResourceCloser.close(resultSet);
MySQLDatabaseResourceCloser.close(statement);
@ -708,12 +708,13 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// We will need to fill in the ? later with the unique id of the class from the database
setSql += "WHERE uniqueId = ?";
// Prepare the statement
PreparedStatement collStatement = connection.prepareStatement(setSql);
try (PreparedStatement collStatement = connection.prepareStatement(setSql)) {
// Set the unique ID
collStatement.setObject(1, uniqueId);
if (DEBUG)
plugin.getLogger().info("DEBUG: collStatement = " + collStatement.toString());
ResultSet collectionResultSet = collStatement.executeQuery();
try (ResultSet collectionResultSet = collStatement.executeQuery()) {
//plugin.getLogger().info("DEBUG: collectionResultSet = " + collectionResultSet.toString());
// Do single dimension types (set and list)
if (Set.class.isAssignableFrom(propertyDescriptor.getPropertyType())) {
@ -779,6 +780,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// additional table.
value = true;
}
}
}
} else {
if (DEBUG)
plugin.getLogger().info("DEBUG: regular type");
@ -905,19 +908,21 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
propertyDescriptor.getPropertyType().equals(HashMap.class) ||
propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
// First substitution is the table name
preparedStatement = connection.prepareStatement(deleteQuery.replace("[table_name]", "`" + dataObject.getCanonicalName() + "." + field.getName() + "`"));
try (PreparedStatement preparedStatement2 = connection.prepareStatement(deleteQuery.replace("[table_name]", "`" + dataObject.getCanonicalName() + "." + field.getName() + "`"))) {
// Second is the unique ID
preparedStatement.setString(1, uniqueId);
preparedStatement.addBatch();
preparedStatement2.setString(1, uniqueId);
preparedStatement2.addBatch();
// Execute
if (DEBUG)
plugin.getLogger().info("DEBUG: " + preparedStatement.toString());
preparedStatement.executeBatch();
plugin.getLogger().info("DEBUG: " + preparedStatement2.toString());
preparedStatement2.executeBatch();
}
}
}
} finally {
// Close properly
MySQLDatabaseResourceCloser.close(preparedStatement);
MySQLDatabaseResourceCloser.close(connection);
}
}