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:
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.
There are other issues in the code, for example the statement st is never closed, or the statements are not prepared then reused.
You can also use the ARRAY_AGG function in HSQLDB to return your arrays, instead of creating it yourself. Link to the Guide and example of usage is given below.
http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#N12538
The array is a JDBC array and can be referenced via its JDBC methods.