diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/FlatfileProvider.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/FlatfileProvider.java index d5140c0b..08f1720b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/FlatfileProvider.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/FlatfileProvider.java @@ -30,13 +30,13 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.concurrent.locks.ReentrantLock; -public abstract class FlatfileProvider extends SQLProvider { +abstract class FlatfileProvider extends SQLProvider { private final File file; private final ReentrantLock lock = new ReentrantLock(); private Connection connection; - public FlatfileProvider(String name, File file) { + FlatfileProvider(String name, File file) { super(name); this.file = file; } @@ -77,53 +77,41 @@ public abstract class FlatfileProvider extends SQLProvider { @Override public boolean runQuery(String query, QueryPS queryPS) { - boolean success = false; - - PreparedStatement preparedStatement = null; - - try { - Connection connection = getConnection(); + try (Connection connection = getConnection()) { if (connection == null || connection.isClosed()) { throw new IllegalStateException("SQL connection is null"); } - preparedStatement = connection.prepareStatement(query); - queryPS.onRun(preparedStatement); + try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { + queryPS.onRun(preparedStatement); - preparedStatement.execute(); - success = true; + preparedStatement.execute(); + return true; + } } catch (SQLException e) { e.printStackTrace(); - } finally { - close(preparedStatement); } - return success; + return false; } @Override public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) { - boolean success = false; - - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; - try { Connection connection = getConnection(); if (connection == null || connection.isClosed()) { throw new IllegalStateException("SQL connection is null"); } - preparedStatement = connection.prepareStatement(query); - queryPS.onRun(preparedStatement); + try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { + queryPS.onRun(preparedStatement); - resultSet = preparedStatement.executeQuery(); - success = queryRS.onResult(resultSet); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + return queryRS.onResult(resultSet); + } + } } catch (SQLException e) { e.printStackTrace(); - } finally { - close(resultSet); - close(preparedStatement); } - return success; + return false; } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/MySQLProvider.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/MySQLProvider.java index b37f98ce..279d089c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/MySQLProvider.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/MySQLProvider.java @@ -97,57 +97,44 @@ public class MySQLProvider extends SQLProvider { @Override public boolean runQuery(String query, QueryPS queryPS) { - boolean success = false; - - Connection connection = null; - PreparedStatement preparedStatement = null; - try { - connection = getConnection(); - if (connection == null || connection.isClosed()) { - throw new IllegalStateException("SQL connection is null"); + try (Connection connection = getConnection()) { + if (connection == null || connection.isClosed()) { + throw new IllegalStateException("SQL connection is null"); + } + + try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { + queryPS.onRun(preparedStatement); + + preparedStatement.execute(); + return true; + } } - - preparedStatement = connection.prepareStatement(query); - queryPS.onRun(preparedStatement); - - preparedStatement.execute(); - success = true; } catch (SQLException e) { e.printStackTrace(); - } finally { - close(preparedStatement); - close(connection); } - return success; + return false; } @Override public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) { - boolean success = false; - - Connection connection = null; - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; - try { - connection = getConnection(); - if (connection == null || connection.isClosed()) { - throw new IllegalStateException("SQL connection is null"); + try (Connection connection = getConnection()) { + if (connection == null || connection.isClosed()) { + throw new IllegalStateException("SQL connection is null"); + } + + try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { + queryPS.onRun(preparedStatement); + + try (ResultSet resultSet = preparedStatement.executeQuery()) { + return queryRS.onResult(resultSet); + } + } } - - preparedStatement = connection.prepareStatement(query); - queryPS.onRun(preparedStatement); - - resultSet = preparedStatement.executeQuery(); - success = queryRS.onResult(resultSet); } catch (SQLException e) { e.printStackTrace(); - } finally { - close(resultSet); - close(preparedStatement); - close(connection); } - return success; + return false; } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/PostgreSQLProvider.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/PostgreSQLProvider.java index 6fbcf9cc..fd4d83bd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/PostgreSQLProvider.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/PostgreSQLProvider.java @@ -50,7 +50,7 @@ public class PostgreSQLProvider extends SQLProvider { String address = configuration.getAddress(); String[] addressSplit = address.split(":"); address = addressSplit[0]; - String port = addressSplit.length > 1 ? addressSplit[1] : "3306"; + String port = addressSplit.length > 1 ? addressSplit[1] : "5432"; String database = configuration.getDatabase(); String username = configuration.getUsername(); @@ -88,57 +88,40 @@ public class PostgreSQLProvider extends SQLProvider { @Override public boolean runQuery(String query, QueryPS queryPS) { - boolean success = false; - - Connection connection = null; - PreparedStatement preparedStatement = null; - - try { - connection = getConnection(); + try (Connection connection = getConnection()) { if (connection == null || connection.isClosed()) { throw new IllegalStateException("SQL connection is null"); } - preparedStatement = connection.prepareStatement(query); - queryPS.onRun(preparedStatement); + try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { + queryPS.onRun(preparedStatement); - preparedStatement.execute(); - success = true; + preparedStatement.execute(); + return true; + } } catch (SQLException e) { e.printStackTrace(); - } finally { - close(preparedStatement); - close(connection); } - return success; + return false; } @Override public boolean runQuery(String query, QueryPS queryPS, QueryRS queryRS) { - boolean success = false; - - Connection connection = null; - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; - - try { - connection = getConnection(); + try (Connection connection = getConnection()){ if (connection == null || connection.isClosed()) { throw new IllegalStateException("SQL connection is null"); } - preparedStatement = connection.prepareStatement(query); - queryPS.onRun(preparedStatement); + try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { + queryPS.onRun(preparedStatement); - resultSet = preparedStatement.executeQuery(); - success = queryRS.onResult(resultSet); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + return queryRS.onResult(resultSet); + } + } } catch (SQLException e) { e.printStackTrace(); - } finally { - close(resultSet); - close(preparedStatement); - close(connection); } - return success; + return false; } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/SQLProvider.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/SQLProvider.java index a214ba43..b5a85dc2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/SQLProvider.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/sqlprovider/SQLProvider.java @@ -34,15 +34,6 @@ import java.sql.SQLException; public abstract class SQLProvider { private static final QueryPS EMPTY_PS = preparedStatement -> {}; - static void close(AutoCloseable closeable) { - if (closeable != null) { - try { - closeable.close(); - } catch (Exception ignored) { - } - } - } - @Getter private final String name;