Add reconnect to ConnectionProvider#getConnection method if Connection#isValid is false
This commit is contained in:
parent
b65c72bbe3
commit
27531362ba
5 changed files with 50 additions and 8 deletions
|
|
@ -4,14 +4,19 @@ import java.sql.Connection;
|
|||
import java.sql.SQLException;
|
||||
|
||||
abstract class AbstractProvider implements ConnectionProvider {
|
||||
private final Connection connection;
|
||||
private Connection connection;
|
||||
|
||||
AbstractProvider(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
abstract Connection establishConnection();
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
try {
|
||||
if (connection == null || !connection.isValid(1)) {
|
||||
connection = establishConnection();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new SqlException(e);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,21 @@ package net.zhira.varioutil.sql;
|
|||
|
||||
import net.zhira.varioutil.util.SqlUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class H2Provider extends AbstractProvider {
|
||||
private final String path;
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
public H2Provider(String path, String username, String password) {
|
||||
super(SqlUtils.createH2Connection(path, username, password));
|
||||
this.path = path;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
Connection establishConnection() {
|
||||
return SqlUtils.createH2Connection(path, username, password);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,24 @@ package net.zhira.varioutil.sql;
|
|||
|
||||
import net.zhira.varioutil.util.SqlUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class MysqlProvider extends AbstractProvider {
|
||||
private final String address;
|
||||
private final String dbname;
|
||||
private final String username;
|
||||
private final String password;
|
||||
private final boolean ssl;
|
||||
|
||||
public MysqlProvider(String address, String dbname, String username, String password, boolean ssl) {
|
||||
super(SqlUtils.createMysqlConnection(address, dbname, username, password, ssl));
|
||||
this.address = address;
|
||||
this.dbname = dbname;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.ssl = ssl;
|
||||
}
|
||||
|
||||
Connection establishConnection() {
|
||||
return SqlUtils.createMysqlConnection(address, dbname, username, password, ssl);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,5 +86,4 @@ public final class SqlAdapter implements AutoCloseable {
|
|||
}
|
||||
provider = ConnectionProvider.CLOSED;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,17 @@ package net.zhira.varioutil.sql;
|
|||
|
||||
import net.zhira.varioutil.util.SqlUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class SqliteProvider extends AbstractProvider {
|
||||
private final String path;
|
||||
|
||||
public SqliteProvider(String path) {
|
||||
super(SqlUtils.createSqliteConnection(path));
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
Connection establishConnection() {
|
||||
return SqlUtils.createSqliteConnection(path);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue