Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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(){
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
Connection is closed after the execution of first 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(){
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();
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.