Update datasource columns version

- Fixes #1551 Bad closing of resources in case of an exception
- Facilitates initialization of SQL handler implementation
This commit is contained in:
ljacqu 2018-05-21 13:07:13 +02:00
parent 68b896cfc3
commit 768ef9179a
4 changed files with 8 additions and 71 deletions

View File

@ -800,7 +800,7 @@
<dependency>
<groupId>ch.jalu</groupId>
<artifactId>datasourcecolumns</artifactId>
<version>0.1-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>

View File

@ -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<ColumnContext, String> 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<ColumnContext, String> sqlColHandler = new SqlColumnsHandler<>(
new MySqlPreparedStatementGenerator(connectionSupplier), columnContext, tableName, nameColumn,
new ResultSetValueRetriever<>(columnContext), new PredicateSqlGenerator<>(columnContext));
forConnectionPool(connectionSupplier, tableName, nameColumn, columnContext));
return new AuthMeColumnsHandler(sqlColHandler);
}

View File

@ -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;
}

View File

@ -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();
}
}
}