Java 模拟结果集关闭失败

Java 模拟结果集关闭失败,java,sql,jdbc,Java,Sql,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(Jt

我如何才能模拟此异常:

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 
使用Java代码

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

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

我是这样想的:

  • 创建一个准备好的语句并使用它执行查询,以获取结果集
  • 关闭准备好的语句
  • 在关闭准备好的语句后,尝试关闭结果集
  • 在调用
    execute()之前调用
    语句

    使用:


    “simulate”的确切含义是什么?您是否正在尝试创建返回ResultSet的方法?SQLException-如果发生数据库访问错误或在关闭的语句中调用此方法@GangNamStyleOverflower错误:我没有环境来测试它,但是根据javadoc。@GangNamStyleOverflower错误:您不能期望从MySQL驱动程序获得与SQLServer驱动程序完全相同的异常类型和错误消息。MySQLNonTransientConnectionException扩展了SQLException。@JBNizet ahhh,true。。谢谢你提供的信息:)
    when(mockResultSet.close()).doThrow(new SQLException("Invalid state, the CallableStatement object is closed."));