gpt4 book ai didi

java - 在 mariadb 中显示状态查询错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:39 25 4
gpt4 key购买 nike

我有 jdbc 客户端(请参阅下面的代码)定期连接到本地 mariadb 服务器并查询复制状态。

    private void closeResultSet(ResultSet rs) {
try {
rs.close();
} catch (Exception e) {}
}
private void closeStatement (Statement st) {
try {
st.close();
} catch (Exception e) {}
}
private void closeConnection(Connection c) {
try {
c.close();
} catch (Exception e) {}
}
public boolean getStatus() {
boolean status = false;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("org.mariadb.jdbc.Driver");
// System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SHOW STATUS LIKE 'wsrep_%'";
rs = stmt.executeQuery(sql);
while (rs.next()) {
String name = rs.getString(1);
String value = rs.getString(2);
if (name != null && !name.isEmpty() && value != null && !value.isEmpty())
{
System.out.println(value);

}
}
status = true;
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
closeResultSet(rs);
closeStatement(stmt);
closeConnection(conn);

} // end try
if (!status)

return status;
}

public void run()
{
while(true) {
if (!getStatus())
//failed
break;
else {
sleep 60s
}
}
}

大约 3 小时后我明白了

java.sql.SQLNonTransientConnectionException: (conn:17945) Could not read resultset: unexpected end of stream, read 0 bytes from 4

我的代码有问题吗?

我想知道“(conn:17945)”是否意味着我的连接数量不断增长,超出了可持续的最大值......?

最佳答案

看起来像是 MariaDB 的问题,这个问题的解决方案似乎很奇怪,因为您必须将 wait_timeout 保持相当长的持续时间才能消除此类问题!

您可以考虑为数据库连接设置一个虚拟验证查询。此查询可能会有所帮助!

validationQuery="SELECT 1"

除了上述内容之外,您可能还需要设置一个相当有效的值,例如 8 小时等。

set wait_timeout=57600

This链接提供了一些类似的信息,供您引用,也this有关相关讨论的问题。

希望这有帮助!

关于java - 在 mariadb 中显示状态查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41332193/

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