有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mariadb中的java显示状态查询错误

我有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

我的代码有问题吗

我想知道“(康涅狄格州:17945)”是否意味着,不知何故,我有越来越多的联系超出了可持续发展的最大限度


共 (1) 个答案

  1. # 1 楼答案

    这看起来像是MariaDB的一个问题,解决这个问题的方法似乎很奇怪,因为你必须让你的wait_timeout持续相当长的时间才能根除这些问题

    您可以考虑为数据库连接设置一个哑验证查询。这个问题可能会有所帮助

    validationQuery="SELECT 1"
    

    除上述内容外,您可能还需要设置一个相当有效的值,比如8小时或类似的时间

    set wait_timeout=57600
    

    This链接提供了一些类似的信息,供您参考,还提供了相关讨论中的this问题

    希望这有帮助