Close old and return new if connection invalid

This commit is contained in:
Rsl1122 2018-08-06 12:05:54 +03:00
parent 8641bd5f0d
commit 6aae5be083

View File

@ -81,11 +81,14 @@ public class MySQLDB extends SQLDB {
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
Connection connection = dataSource.getConnection(); Connection connection = dataSource.getConnection();
if (!connection.isValid(5)) { if (!connection.isValid(5)) {
connection.close();
if (dataSource instanceof HikariDataSource) { if (dataSource instanceof HikariDataSource) {
((HikariDataSource) dataSource).close(); ((HikariDataSource) dataSource).close();
} }
try { try {
setupDataSource(); setupDataSource();
// get new connection after restarting pool
return dataSource.getConnection();
} catch (DBInitException e) { } catch (DBInitException e) {
throw new DBOpException("Failed to restart DataSource after a connection was invalid: " + e.getMessage(), e); throw new DBOpException("Failed to restart DataSource after a connection was invalid: " + e.getMessage(), e);
} }