From 768ef9179aa278ebe60cc1aa23717a54c7948ad2 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Mon, 21 May 2018 13:07:13 +0200 Subject: [PATCH] Update datasource columns version - Fixes #1551 Bad closing of resources in case of an exception - Facilitates initialization of SQL handler implementation --- pom.xml | 2 +- .../columnshandler/AuthMeColumnsHandler.java | 13 +++--- .../columnshandler/ConnectionSupplier.java | 20 --------- .../MySqlPreparedStatementGenerator.java | 44 ------------------- 4 files changed, 8 insertions(+), 71 deletions(-) delete mode 100644 src/main/java/fr/xephi/authme/datasource/columnshandler/ConnectionSupplier.java delete mode 100644 src/main/java/fr/xephi/authme/datasource/columnshandler/MySqlPreparedStatementGenerator.java diff --git a/pom.xml b/pom.xml index c4cfe5f8d..535b41b23 100644 --- a/pom.xml +++ b/pom.xml @@ -800,7 +800,7 @@ ch.jalu datasourcecolumns - 0.1-SNAPSHOT + 0.1.1-SNAPSHOT true diff --git a/src/main/java/fr/xephi/authme/datasource/columnshandler/AuthMeColumnsHandler.java b/src/main/java/fr/xephi/authme/datasource/columnshandler/AuthMeColumnsHandler.java index bb0d80b77..50575b4e5 100644 --- a/src/main/java/fr/xephi/authme/datasource/columnshandler/AuthMeColumnsHandler.java +++ b/src/main/java/fr/xephi/authme/datasource/columnshandler/AuthMeColumnsHandler.java @@ -5,9 +5,8 @@ import ch.jalu.datasourcecolumns.data.DataSourceValues; import ch.jalu.datasourcecolumns.data.UpdateValues; import ch.jalu.datasourcecolumns.predicate.Predicate; import ch.jalu.datasourcecolumns.sqlimplementation.PredicateSqlGenerator; -import ch.jalu.datasourcecolumns.sqlimplementation.PreparedStatementGenerator; -import ch.jalu.datasourcecolumns.sqlimplementation.ResultSetValueRetriever; import ch.jalu.datasourcecolumns.sqlimplementation.SqlColumnsHandler; +import ch.jalu.datasourcecolumns.sqlimplementation.statementgenerator.ConnectionSupplier; import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.DatabaseSettings; @@ -16,6 +15,8 @@ import java.sql.Connection; import java.sql.SQLException; import java.util.List; +import static ch.jalu.datasourcecolumns.sqlimplementation.SqlColumnsHandlerConfig.forConnectionPool; +import static ch.jalu.datasourcecolumns.sqlimplementation.SqlColumnsHandlerConfig.forSingleConnection; import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException; /** @@ -43,8 +44,9 @@ public final class AuthMeColumnsHandler { String nameColumn = settings.getProperty(DatabaseSettings.MYSQL_COL_NAME); SqlColumnsHandler sqlColHandler = new SqlColumnsHandler<>( - PreparedStatementGenerator.fromConnection(connection), columnContext, tableName, nameColumn, - new ResultSetValueRetriever<>(columnContext), new PredicateSqlGenerator<>(columnContext, true)); + forSingleConnection(connection, tableName, nameColumn, columnContext) + .setPredicateSqlGenerator(new PredicateSqlGenerator<>(columnContext, true)) + ); return new AuthMeColumnsHandler(sqlColHandler); } @@ -61,8 +63,7 @@ public final class AuthMeColumnsHandler { String nameColumn = settings.getProperty(DatabaseSettings.MYSQL_COL_NAME); SqlColumnsHandler sqlColHandler = new SqlColumnsHandler<>( - new MySqlPreparedStatementGenerator(connectionSupplier), columnContext, tableName, nameColumn, - new ResultSetValueRetriever<>(columnContext), new PredicateSqlGenerator<>(columnContext)); + forConnectionPool(connectionSupplier, tableName, nameColumn, columnContext)); return new AuthMeColumnsHandler(sqlColHandler); } diff --git a/src/main/java/fr/xephi/authme/datasource/columnshandler/ConnectionSupplier.java b/src/main/java/fr/xephi/authme/datasource/columnshandler/ConnectionSupplier.java deleted file mode 100644 index 77fbe8f3a..000000000 --- a/src/main/java/fr/xephi/authme/datasource/columnshandler/ConnectionSupplier.java +++ /dev/null @@ -1,20 +0,0 @@ -package fr.xephi.authme.datasource.columnshandler; - -import java.sql.Connection; -import java.sql.SQLException; - -/** - * Supplier of connections to a database. - */ -@FunctionalInterface -public interface ConnectionSupplier { - - /** - * Returns a connection to the database. - * - * @return the connection - * @throws SQLException . - */ - Connection get() throws SQLException; - -} diff --git a/src/main/java/fr/xephi/authme/datasource/columnshandler/MySqlPreparedStatementGenerator.java b/src/main/java/fr/xephi/authme/datasource/columnshandler/MySqlPreparedStatementGenerator.java deleted file mode 100644 index c20357ae4..000000000 --- a/src/main/java/fr/xephi/authme/datasource/columnshandler/MySqlPreparedStatementGenerator.java +++ /dev/null @@ -1,44 +0,0 @@ -package fr.xephi.authme.datasource.columnshandler; - -import ch.jalu.datasourcecolumns.sqlimplementation.PreparedStatementGenerator; -import ch.jalu.datasourcecolumns.sqlimplementation.PreparedStatementResult; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; - -/** - * Implementation of {@link PreparedStatementGenerator} for MySQL which ensures that the connection - * taken from the connection pool is also closed after the prepared statement has been executed. - */ -class MySqlPreparedStatementGenerator implements PreparedStatementGenerator { - - private final ConnectionSupplier connectionSupplier; - - MySqlPreparedStatementGenerator(ConnectionSupplier connectionSupplier) { - this.connectionSupplier = connectionSupplier; - } - - @Override - public PreparedStatementResult create(String sql) throws SQLException { - Connection connection = connectionSupplier.get(); - return new MySqlPreparedStatementResult(connection, connection.prepareStatement(sql)); - } - - /** Prepared statement result which also closes the associated connection. */ - private static final class MySqlPreparedStatementResult extends PreparedStatementResult { - - private final Connection connection; - - MySqlPreparedStatementResult(Connection connection, PreparedStatement preparedStatement) { - super(preparedStatement); - this.connection = connection; - } - - @Override - public void close() throws SQLException { - super.close(); - connection.close(); - } - } -}