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...?