java.sql.SQLException : Invalid state, CallableStatement 对象已关闭

标签 java sql servlets jdbc

下面的代码生成此异常:

java.sql.SQLException: Invalid state, the CallableStatement object is closed.
    at net.sourceforge.jtds.jdbc.JtdsCallableStatement.checkOpen(JtdsCallableStatement.java:120)
    at net.sourceforge.jtds.jdbc.JtdsStatement.getConnection(JtdsStatement.java:1207)
    at net.sourceforge.jtds.jdbc.JtdsResultSet.getConnection(JtdsResultSet.java:409)
    at net.sourceforge.jtds.jdbc.JtdsResultSet.close(JtdsResultSet.java:470)
    at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.close(DelegatingResultSet.java:152)
    at 

下面的代码有时会生成上述错误,但有时不会:

   private void doRequest(HttpServletRequest request) throws IOException, ServletException {
        CallableStatement stmt = null;
        ResultSet rs = null;
        String someString;
        try {
            this.connectDB();
            stmt = this.conn.prepareCall("{call sp_SomeSP1(?)}");
            stmt.setLong(1, someFunc());

            rs = stmt.executeQuery();

            while (rs.next()) {
                if (rs.getInt(1)==someOtherFunc()) {
                    someString = rs.getString(2);
                    break;
                }
            }

            stmt = conn.prepareCall("{call sp_someSP(?, ?)}");
            stmt.setLong(1, someFunc());
            stmt.setTimestamp(2, new Timestamp(getFrom().getTime()));

            rs = stmt.executeQuery();
            if (rs.next()) {
                lastUpdated = rs.getTimestamp("LastUpdated");
            }

            request.setAttribute("lastUpdated", lastUpdated);

            LOGGER.debug("Forwarding to view...");
            getServletContext().getRequestDispatcher("/SomeJSP.jsp").forward(this.request, this.response);

        } catch (NamingException e) {
            LOGGER.error("Database connection lookup failed", e);
            sendError("Server Error");
        } catch (SQLException e) {
            LOGGER.error("Query failed", e);
            sendError("Server Error");
        } catch (IllegalStateException e) {
            LOGGER.error("View failed", e);
        } finally {
            try {
                if (rs!=null) rs.close(); 
            } catch (NullPointerException e) {
                LOGGER.error("Result set closing failed", e);
            } catch (SQLException e) {
                LOGGER.error("Result set closing failed", e);
            }
            try {
                if (stmt!=null) stmt.close();
            } catch (NullPointerException e) {
                LOGGER.error("Statement closing failed", e);
            } catch (SQLException e) {
                LOGGER.error("Statement closing failed", e);
            }
            try {
                this.closeDB();
            } catch (NullPointerException e) {
                LOGGER.error("Database connection closing failed", e);
            } catch (SQLException e) {
                LOGGER.error("Database connection closing failed", e);
            }
        }

这意味着 doRequest() 大部分时间都能正常工作,但有时我们会收到 HTTP 错误 500,如果我们检查 tomcat 日志,我们会看到:

java.sql.SQLException: Invalid state, the CallableStatement object is closed.

最佳答案

您似乎在 Servlet 中使用成员变量(conn 变量)。然而,Servlet 通常可以由多个线程同时调用。您如何确保多个线程不会意外使用/关闭同一个连接?

关于java.sql.SQLException : Invalid state, CallableStatement 对象已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13944954/

相关文章:

java - JSP 找不到我的 servlet,有时会出现错误 500,有时会出现错误 404

Java 比较器 Arrays.sort()

java - 获取队列中最大号码位置的方法

sql - Entity Framework 返回与 SSMS 不同的结果

sql - 如何在数据库中模拟标记联合?

java - 如何配置注释来映射 servlet?

Java 碰撞 Swing 无法从侧面工作

java - java中Number of(10,20)和Number on=new Number(10,20)有什么区别

mysql - 如何在 MySQL 的函数中返回选择的结果?

java - 为我的 servlet 使用配置 sqlite 数据库路径的正确方法是什么?