Fix connection leaking on batch inserts; fix IDEA analyser warning

This commit is contained in:
bakatrouble 2016-12-25 20:07:02 +03:00 committed by Luck
parent a8fea0fc49
commit 4c93bf7cf0

View File

@ -43,6 +43,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
@ -103,7 +104,7 @@ public class SQLBacking extends AbstractBacking {
return provider.runQuery(query, queryRS);
}
public boolean tableExists(String table) throws SQLException {
private boolean tableExists(String table) throws SQLException {
return provider.getConnection().getMetaData().getTables(null, null, table.toUpperCase(), null).next();
}
@ -292,7 +293,8 @@ public class SQLBacking extends AbstractBacking {
}
List<NodeDataHolder> data = user.getNodes().stream().map(NodeDataHolder::fromNode).collect(Collectors.toList());
try (PreparedStatement ps = provider.getConnection().prepareStatement(prefix.apply(USER_PERMISSIONS_INSERT))) {
try (Connection connection = provider.getConnection()) {
try (PreparedStatement ps = connection.prepareStatement(prefix.apply(USER_PERMISSIONS_INSERT))) {
for (NodeDataHolder nd : data) {
ps.setString(1, user.getUuid().toString());
ps.setString(2, nd.getPermission());
@ -304,6 +306,7 @@ public class SQLBacking extends AbstractBacking {
ps.addBatch();
}
ps.executeBatch();
}
} catch (SQLException e) {
e.printStackTrace();
}