2012-08-15 52 views
0

我试图改善其选择从HSQLDB数据的功能,所以我试图在多个线程中运行它,但我得到了以下异常:的executeQuery在多线程关闭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; 
    } 

我读到执行多个选择由不同的线程关闭连接。这是真的吗?如何解决?

回答

1

看起来连接已关闭,因为该功能使用了相同的连接,并在首次使用后关闭。检查你的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方法来引用。

+0

我该如何使用ARRAY_AGG?你能修改我的查询来使用它吗? – 2012-08-15 12:19:10

+1

更新了答案。 – fredt 2012-08-15 15:41:44

+0

我试过你的解决方案,但是当我执行rs.getArray(1)时,我得到了“无效游标状态:标识符游标未定位在UPDATE,DELETE,SET或GET语句中的行上;; ResultSet定位在第一行之前”异常。你有什么想法如何解决它? – 2012-08-16 09:14:10