java.sql.SQLException:无效状态,CallableStatement对象被关闭

时间:2012-12-19 03:11:12

标签: 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.

3 个答案:

答案 0 :(得分:4)

您似乎正在使用带有Servlet的成员变量(conn变量)。但是,Servlet通常可以由多个线程同时调用。你如何确保多个线程不会意外地使用/关闭同一个连接?

答案 1 :(得分:1)

stmt.setLong(1, someFunc());
stmt.setTimestamp(3, new Timestamp(getFrom().getTime()));

将是

stmt.setLong(1, someFunc());
stmt.setTimestamp(2, new Timestamp(getFrom().getTime()));

答案 2 :(得分:0)

在堆栈跟踪中看到此错误消息org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.close,看起来在关闭结果集时出现错误

我建议你改变你的结果集检查条件,检查结果集是否仍然打开,然后关闭它:

    if (rs!=null && ! rs.isClosed()){
        //resultset is there and not in closed state
        rs.close(); 
    }