我正在使用自己的类来保持jdbc MysqlDataSource类的持久连接(请参阅下面的代码)。然而,在工作正常一天后,它停止工作。仍在进行连接,但查询未通过。此外,当我发送新查询时,连接似乎处于休眠模式。除了我的SHOW PROCESSES中的时间变量不断上升,表明查询没有通过。
这可能是我的问题?
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final MysqlDataSource source = new MysqlDataSource();
private Connection connection;
public MysqlStore(String name, String server, int port, String user, String password) {
source.setDatabaseName(name);
source.setServerName(server);
source.setPort(port);
source.setUser(user);
source.setPassword(password);
}
private Connection getConnection() {
try {
if (connection == null || !connection.isValid(1)) {
connection = source.getConnection();
}
return connection;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public CompletableFuture<QueryResult> runQuery(String sqlText, Object... arguments) {
CompletableFuture<QueryResult> resultFuture = new CompletableFuture<>();
executor.submit(() -> {
Connection connection = getConnection();
try {
PreparedStatement statement = connection.prepareStatement(sqlText);
for (int i = 0; i < arguments.length; i++) {
statement.setObject(i + 1, arguments[i]);
}
resultFuture.complete(new QueryResult(statement));
} catch (SQLException e) {
resultFuture.completeExceptionally(e);
}
});
return resultFuture;
}
public CompletableFuture<Integer> runUpdate(String sqlText, Object... arguments) {
CompletableFuture<Integer> resultFuture = new CompletableFuture<>();
executor.submit(() -> {
Connection connection = getConnection();
try {
PreparedStatement statement = connection.prepareStatement(sqlText);
for (int i = 0; i < arguments.length; i++) {
statement.setObject(i + 1, arguments[i]);
}
resultFuture.complete(statement.executeUpdate());
} catch (SQLException e) {
resultFuture.completeExceptionally(e);
}
});
return resultFuture;
}
public CompletableFuture<Integer> runUpdateGetKey(String sqlText, Object... arguments) {
CompletableFuture<Integer> resultFuture = new CompletableFuture<>();
executor.submit(() -> {
Connection connection = getConnection();
try {
PreparedStatement statement = connection.prepareStatement(sqlText, Statement.RETURN_GENERATED_KEYS);
for (int i = 0; i < arguments.length; i++) {
statement.setObject(i + 1, arguments[i]);
}
statement.executeUpdate();
try (ResultSet res = statement.getGeneratedKeys()) {
if (res.next()) {
resultFuture.complete(res.getInt(1));
} else {
resultFuture.completeExceptionally(new IllegalStateException("Expected a generated key but found nothing."));
}
}
} catch (SQLException e) {
resultFuture.completeExceptionally(e);
}
});
return resultFuture;
}
@Override
public void close() {
try {
executor.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!executor.isTerminated()) {
executor.shutdownNow();
}
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public class QueryResult implements Closeable {
PreparedStatement statement;
ResultSet result;
public QueryResult(PreparedStatement statement) {
this.statement = statement;
try {
result = statement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet get() {
return result;
}
@Override
public void close() {
try {
if (result != null) {
result.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}