gpt4 book ai didi

java - Docker暂停数据库让语句永远挂起

转载 作者:行者123 更新时间:2023-12-01 16:13:19 25 4
gpt4 key购买 nike

我有一个在 docker 上运行的 PostgreSQL 数据库,我想测试我的 java 系统处理它的连接问题的能力。我在两种不同的场景下测试我的 java 服务:停止容器 (docker stop postgresContainer) 和暂停它 (docker Pause postgresContainer)

当我停止容器时,JDBC 会立即抛出一个异常,我可以捕获并处理它,但是当我暂停时,服务会处于锁定状态,JDBC 语句将永远挂起执行,直到容器“取消暂停” ,当方法流程返回并执行挂起的语句时。

我还注意到,当我通过调用 connection.isValid(1); 获取 JDBC 连接状态时,它也永远卡在这里。

public void executeStatement(String query) {
try {
Connection connection = getConnection();
Statement statement = connection.createStatement();
// It gets stuck here forever when the container is "paused"
// When I unpause, the query to be executed is persisted in DB
statement.execute(query);

statement.close();
connection.close();
}
catch (Exception e) {
statement.close();
connection.close();
}

最佳答案

参见https://jdbc.postgresql.org/documentation/head/connect.html

特别是:

connectTimeout = int

The timeout value used for socket connect operations. If connecting to the server takes longer than this value, the connection is broken. The timeout is specified in seconds and a value of zero means that it is disabled.

socketTimeout = int

The timeout value used for socket read operations. If reading from the server takes longer than this value, the connection is closed. This can be used as both a brute force global query timeout and a method of detecting network problems. The timeout is specified in seconds and a value of zero means that it is disabled.

关于java - Docker暂停数据库让语句永远挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62473728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com