1

I have jdbc client (see the code below) to periodically connect to local mariadb server and query for replication status.

    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
       }
    }
    }

After ~3 hours i see

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

Is there something wrong in my code?

I wonder if "(conn:17945)" means that somehow I have growing number of connections beyond the sustainable max...?

crubio
  • 6,394
  • 4
  • 30
  • 33
vic
  • 45
  • 2
  • 9

1 Answers1

0

Looks like a problem with MariaDB, the solution to this seems to be weird in the sense that you have to keep your wait_timeout to a considerably longer duration to eradicate such issues!

You might consider putting a dummy validation query for your database connection. This query might help in that!

validationQuery="SELECT 1"

Alongside the above, you might have to set a considerably valid value, say 8 hours or the like.

set wait_timeout=57600

This link provides some information on the similar lines, for your reference and also this question on a related discussion.

Hope this helps!

Community
  • 1
  • 1
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46