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 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()){
}
} finally {
if(ps != null)
ps.close();
if(rs != null)
rs.close();
if(con != null)
con.close();
}
query = "SELECT COUNTCOMMENTS FROM videosinfos WHERE UNIQUEID=?";
try {
con = Database.getcon();
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();
}
Note: The line I get the error, have a comment on it.
Connecting to Database:
public DBConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
String unicode="useSSL=false&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8";
con = DriverManager.getConnection("jdbc:mysql://localhost:15501/duckdb?"+unicode, "root", "_PWD");
st = con.createStatement();
}catch(Exception ex){
System.out.println(ex.getMessage());
System.out.println("couldn't connect!");
}
}
public Connection getcon(){
DBConnect condb = new DBConnect();
Connection connect = con;
return con;
}
But in compilation I get this error:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. at sun.reflect.GeneratedConstructorAccessor18.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) at com.mysql.jdbc.Util.getInstance(Util.java:387) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1246) at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1241) at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4102) at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4071) at duck.reg.pack.DBConnect.getitemfull_details(DBConnect.java:686) at duck.reg.pack.index.doPost(index.java:73) at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
I'm Using Debian with MariaDB v10.x/Mysql
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With