[英]ExecuteQuery in multiple threads closes hsqldb database connection
我正在尝试改进从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)
这是我每次尝试在新线程中运行的函数:
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;
}
我读到由不同线程执行多个选择会关闭连接。 这是真的,如何解决?
似乎该连接已关闭,因为该功能使用了相同的连接,并且在首次使用后关闭了该连接。 检查如何编写DBConnection类。
代码中还有其他问题,例如,语句st永不关闭,或者未准备好然后再使用的语句。
您还可以在HSQLDB中使用ARRAY_AGG函数来返回数组,而不是自己创建数组。 链接到指南,用法示例如下。
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)
该数组是一个JDBC数组,可以通过其JDBC方法进行引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.