简体   繁体   English

多个线程中的ExecuteQuery关闭hsqldb数据库连接

[英]ExecuteQuery in multiple threads closes hsqldb database connection

I'm trying to improve a function which selects data from hsqldb so I tried to run it in multiple threads but I got the following exception: 我正在尝试改进从hsqldb中选择数据的功能,因此我尝试在多个线程中运行它,但是出现以下异常:

java.sql.SQLNonTransientConnectionException: connection exception: closed
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatementBase.checkClosed(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatementBase.getResultSet(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.getResultSet(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)

This is the function I tried to run it in a new thread every time: 这是我每次尝试在新线程中运行的函数:

public List<String> getAllClassNames(boolean concrete) throws ClassMapException {
        List<String> result = new ArrayList<String>();


        try {
            ResultSet rs = null;
            Connection connection = DBConnection.getConnection();
            Statement st = connection.createStatement();
            if (concrete)
                rs = st.executeQuery("select cd_name from CLASS_DESCRIPTORS where CD_CONCRETE=true  order by cd_name");
            else
                rs = st.executeQuery("select cd_name from CLASS_DESCRIPTORS order by cd_name");
            while (rs.next()) {
                result.add(rs.getString("cd_name"));
            }
        } catch (SQLException e) {
            _log.error("SQLException while retrieve all class names." + e.getMessage(), e);
            throw new ClassMapException("SQLException while retrieve all class names." + e.getMessage(), e);
        } finally {
            DBConnection.closeConnection();
        }
        return result;
    }

I read that executing multiple selects by different threads closes connection. 我读到由不同线程执行多个选择会关闭连接。 Is this true and how to solve it? 这是真的,如何解决?

It seems the connection is closed because the same connection is used by the function and is closed after the first use. 似乎该连接已关闭,因为该功能使用了相同的连接,并且在首次使用后关闭了该连接。 Check how your DBConnection class is written. 检查如何编写DBConnection类。

There are other issues in the code, for example the statement st is never closed, or the statements are not prepared then reused. 代码中还有其他问题,例如,语句st永不关闭,或者未准备好然后再使用的语句。

You can also use the ARRAY_AGG function in HSQLDB to return your arrays, instead of creating it yourself. 您还可以在HSQLDB中使用ARRAY_AGG函数来返回数组,而不是自己创建数组。 Link to the Guide and example of usage is given below. 链接到指南,用法示例如下。

http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#N12538 http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#N12538

select array_agg(cd_name order by cd_name) as arr from CLASS_DESCRIPTORS where CD_CONCRETE=true

Array array = rs.getArray(1)

The array is a JDBC array and can be referenced via its JDBC methods. 该数组是一个JDBC数组,可以通过其JDBC方法进行引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM