我正在尝试改进从 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;
}
我读到由不同的线程执行多个选择会关闭连接。这是真的吗?如何解决?