连接关闭MYSQL后不允许任何操作

时间:2017-07-22 08:04:38

标签: java mysql jdbc

基本上我有:

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();
}

注意:我收到错误的行,对其进行评论。

连接数据库:

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

但是在编译中我得到了这个错误:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:连接关闭后不允许任何操作。       at sun.reflect.GeneratedConstructorAccessor18.newInstance(Unknown Source)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:423)       在com.mysql.jdbc.Util.handleNewInstance(Util.java:404)       在com.mysql.jdbc.Util.getInstance(Util.java:387)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)       在com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1246)       在com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1241)       在com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4102)       在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)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:661)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)       在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)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1455)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       在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)       在java.lang.Thread.run(Thread.java:748)

我将Debian与MariaDB v10.x / Mysql一起使用

3 个答案:

答案 0 :(得分:2)

这是因为两个原因:

  • 执行第一个PreparedStatement
  • 后关闭连接
  • getcon使用instance变量而不是local变量。因此,下次调用conn时会返回相同的getcon变量(之前关闭的变量)。

要解决此问题,需要修改getconDBConnect以声明本地conn变量并将其返回(事实上,您不需要DBConnect所有),例如:

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

答案 1 :(得分:0)

这是一个MySQL设置问题。 MySQL服务器的响应时间(上例中为60,621 ms)超过了MySQL服务器配置的wait_timeout值。

解决方案

要解决此问题,请与MySQL数据库管理员合作以增加wait_timeout参数的值。

将Hibernate Configuration与您的MySQL服务器对齐以解决此问题。

此设置可在my.cnf文件中配置。默认情况下,mysql将该值设置为“ 28800”秒。如果此值已从默认值修改,请考虑将其恢复为默认值,以恢复到Datameer服务器的连接。

答案 2 :(得分:-2)

一个好方法是使用一次尝试 - 最终像这样阻止

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();
        }