如何修复“ SQLServerException:连接已关闭”。在Try-With-Resources块中发生了?

时间:2019-05-22 09:00:20

标签: java jdbc try-with-resources

我正在对DBMS进行性能测试。对于我的硕士论文,我必须手动进行。我必须有一定数量的线程,每个线程都打开自己的dbdb JDBC连接(连接池不是一种选择),并提交相同数量的相同事务(每个线程执行相同的工作)。我在try-with-resources块中将连接作为资源打开了。连接应保持打开状态,直到try-with-resources范围的末尾,但有时会打开,有时则不会。

try (Connection conn = getConn();
    PreparedStatement simpleSelectStmt = conn
                         .prepareStatement(tcInstance.getQueryMap().get("SimpleSelect").getSimpleSelect())) {

        setConnIsolation(conn);

        long startTime = System.nanoTime();
        singleThread.execute(new Runnable() {

            @Override
            public void run() {

                for (int j = 0; j < tcInstance.getnT(); j++) {
                    try {
                        conn.setAutoCommit(false);
                        simpleSelectStmt.execute();
                        conn.commit();
                    } catch (Exception e) {
                        error++;
                        if (detectDeadlock(e.getMessage())) {
                            deadlock++;
                            System.out.println("Deadlock detected!");
                        } else {
                            e.printStackTrace();
                        }

                        try {
                            if (conn != null) {
                                conn.rollback();
                            }
                        } catch (SQLException e1) {
                            System.out.println("There was an error in rolling back the transaction.");
                            e1.printStackTrace();
                        }

                    }

                    if (j != (tcInstance.getnT() - 1)) {
                        try {
                            Thread.sleep(t);// ms
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

                measureCPUusage(Thread.currentThread().getId());
                singleThread.shutdown();

        });

        long endTime = System.nanoTime();
        ....doing some other measurements....

    } catch (Exception e) {
        System.err.println("Error");
    }

这是用于获取jdbc连接的getConn()方法:

private Connection getConn() throws SQLException {
    return DriverManager.getConnection(tcInstance.getJDBCurl(), tcInstance.getUser(), tcInstance.getPassword());
}

我希望通过整个try-with-resources块打开连接,但是在conn.setAutoCommit(false);conn.rollback();行上有Connection is closeed异常

2 个答案:

答案 0 :(得分:1)

简而言之,您的代码流似乎是这样的:

try (Connection conn = getConn()) {
    setConnIsolation(conn);

    // Posted from Thread-1
    singleThread.execute(new Runnable() {

        @Override
        public void run() {

        // Thread-2 accesses conn created on Thread-1
        // use conn here..
    });

   // ....doing some other work....

} catch (Exception e) {
    System.err.println("Error");
}

//Conn is released

当可运行对象开始工作时,其相应线程可能正在使用已释放的conn。这是因为,在发布可运行对象之后,发布线程从Try-With-Resources块中出来,并且Conn被释放。

解决方案: 将Conn从Try-With-Resources块中移除。

答案 1 :(得分:0)

请为每个线程打开单独的连接。一个线程的打开连接被另一个线程关闭,因此您遇到了异常。此外,请提供有关detectDeadlock的更多信息,以进一步帮助您。我希望您在多线程环境中不需要用于简单连接的东西。