Move close methods out of sql datasource utils class

As noticed by @Gnat008
- We need two different implementations for MySQL and SQLite because SQLite uses an older version where #isClosed is not implemented
This commit is contained in:
ljacqu
2017-04-29 08:18:43 +02:00
parent e56a3c0ab6
commit 82d74ca0a7
3 changed files with 32 additions and 147 deletions
@@ -29,7 +29,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.close;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
public class MySQL implements DataSource {
@@ -979,12 +978,42 @@ public class MySQL implements DataSource {
.build();
}
/**
* Closes a {@link ResultSet} safely.
*
* @param rs the result set to close
*/
private static void close(ResultSet rs) {
try {
if (rs != null && !rs.isClosed()) {
rs.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close ResultSet", e);
}
}
/**
* Closes a {@link Statement} safely.
*
* @param st the statement set to close
*/
private static void close(Statement st) {
try {
if (st != null && !st.isClosed()) {
st.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close Statement", e);
}
}
/**
* Checks if the last login column has a type that needs to be migrated.
*
* @param con connection to the database
* @param metaData lastlogin column meta data
* @throws SQLException
* @throws SQLException .
*/
private void migrateLastLoginColumn(Connection con, DatabaseMetaData metaData) throws SQLException {
final int columnType;
@@ -2,10 +2,7 @@ package fr.xephi.authme.datasource;
import fr.xephi.authme.ConsoleLogger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Utilities for SQL data sources.
@@ -23,53 +20,4 @@ final class SqlDataSourceUtils {
static void logSqlException(SQLException e) {
ConsoleLogger.logException("Error during SQL operation:", e);
}
// We use overloaded close() methods instead of one close(AutoCloseable) method in order to limit the
// checked exceptions to SQLException, which is the only checked exception these classes throw.
/**
* Closes a {@link ResultSet} safely.
*
* @param rs the result set to close
*/
static void close(ResultSet rs) {
try {
if (rs != null && !rs.isClosed()) {
rs.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close ResultSet", e);
}
}
/**
* Closes a {@link Statement} safely.
*
* @param st the statement set to close
*/
static void close(Statement st) {
try {
if (st != null && !st.isClosed()) {
st.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close Statement", e);
}
}
/**
* Closes a {@link Connection} safely.
*
* @param con the connection set to close
*/
static void close(Connection con) {
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
ConsoleLogger.logException("Could not close Connection", e);
}
}
}