我正在使用 JTDS 连接到 MS-SQL 2005。我使用 c3p0 作为数据库连接池,配置了 Spring。
我随机得到一个 SQLException:Invalid state, the ResultSet object is closed
在一个 Groovy 脚本中,我在其中传递了对连接池的引用。该脚本由计时器每隔一段时间执行一次。随机,我的意思是脚本在 99% 的情况下都能完美运行,但是当它失败时,它会这样做几次,然后再次恢复正常工作,从中断的地方开始。所有关键工作都在事务中完成,从消息队列中拉出。
下面的逻辑:
//passed into the groovy context
DataSource source = source;
Connection conn = source.getConnection();
...
//Have to omit proprietary DB stuff... sorry...
PreparedStatement fooStatement = conn.prepareStatement("INSERT INTO foo (x,y,z) VALUES (?,?,?) select SCOPE_IDENTITY();");
ResultSet identRes = fooStatement.executeQuery();
//This is where the execption is thrown.
identRes.next();
...
try{
log.info("Returning SQL connection.");
conn.close();
}catch(Exception ex){}
有一个单独的计时器线程运行类似的 groovy 脚本,我们在其中没有看到这个问题。该脚本使用类似的调用来获取连接并关闭它。
最初,我们认为第二个脚本可能已经从池中获取相同的连接,先完成,然后关闭连接。但是 c3p0 的文档说调用conn.close()
应该简单地将它返回到池中。
有没有其他人看到这个,或者我在这里错过了什么大事?
谢谢。