Basically I have:
String query = \"SELECT * FROM table WHERE UNIQUEID=? AND DIR IS NOT NULL AND NAME IS NOT NULL AND PAGETYPE IS NOT NULL\";
DBConnect Databa
It's because of two reasons:
PreparedStatement
getcon
uses an instance
variable instead of local
variable. Due to this, same conn
variable (the one that was closed earlier) gets returned when getcon
is called next time.To fix this, getcon
and DBConnect
need to be modified to declare a local conn
variable and return it (in fact, you don't need DBConnect
at all), e.g.:
public Connection getcon(){
try{
Class.forName("com.mysql.jdbc.Driver");
String unicode="useSSL=false&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8";
return DriverManager.getConnection("jdbc:mysql://localhost:15501/duckdb?"+unicode, "root", "_PWD");
}catch(Exception ex){
System.out.println(ex.getMessage());
System.out.println("couldn't connect!");
throw new RuntimeException(ex);
}
}
This is a MySQL settings issue. The response time of the MySQL server (60,621 ms in the example above) exceeds the MySQL server's configured wait_timeout value.
Solution
To resolve this issue, work with the MySQL database administrator to increase the value of the wait_timeout parameter.
Align Hibernate Configuration with you MySQL server to fix this problem.
This setting is configurable in the my.cnf file. By default, mysql sets this value to "28800" seconds. If this value has been modified from the default, consider reverting it back to the default to restore connectivity to the Datameer server.
A good approach is to use one try - finally block like this
String query = "SELECT * FROM table WHERE UNIQUEID=? AND DIR IS NOT NULL AND NAME IS NOT NULL AND PAGETYPE IS NOT NULL";
DBConnect Database = new DBConnect();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
try {
con = Database.getcon();
ps = con.prepareStatement(query);
ps.setString(1, URI);
rs=ps.executeQuery();
if(rs.next()){
}
query = "SELECT COUNTCOMMENTS FROM videosinfos WHERE UNIQUEID=?";
ps = con.prepareStatement(query); // Getting error here
rs=ps.executeQuery();
ps.setString(1, URI);
rs=ps.executeQuery();
if(rs.next()){
comments = rs.getInt(1);
}
} finally {
if(ps != null)
ps.close();
if(rs != null)
rs.close();
if(con != null)
con.close();
}