结果集对象已关闭 - jtds
我正在使用 JTDS 连接到 MS-SQL 2005。我使用 c3p0 作为数据库连接池,并使用 Spring 配置。
我在 Groovy 脚本中随机收到 SQLException: 无效状态,ResultSet 对象已关闭
,在该脚本中我传递了对连接池的引用。该脚本由计时器每隔一段时间执行一次。我所说的随机,是指该脚本在 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() 应该简单地将其返回到池中。
有其他人看过这个吗,还是我在这里错过了一些大事?
谢谢。
I am using JTDS to connect to MS-SQL 2005. I am using c3p0 as the DB Connection pool, configured with Spring.
I am randomly getting an SQLException: Invalid state, the ResultSet object is closed
in a Groovy script in which I have passed a reference to the connection pool. The script is executed by a timer every so often. By random, I mean that the script works perfectly 99% of the time, but when it fails, it will do so a couple of times, then go back to working correctly again, picking up where it left off. All of the critical work is done in a transaction, pulling off of a Message Queue.
Logic below:
//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){}
There is a separate timer thread that runs a similar groovy script, in which we have not seen this issue. That script uses similar calls to get the connection, and close it.
Originally, we thought that the second script may have been grabbing the same connection off the pool, finishing first, then closing the connection. But c3p0's documentation says that calling conn.close()
should simply return it to the pool.
Has anyone else seen this, or am I missing something big here?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们解决了这个问题...C3P0 配置为删除签出时间超过 30 秒的连接,我们这样做是为了防止数据库中出现死锁(我们不控制调整)。其中一项事务需要非常长的时间才能完成,并且 C3P0 正在断开连接,从而导致 ResultSet Closed 错误。然而,令人惊讶的是,C3P0 没有记录该事件,因此我们没有在应用程序的日志中看到这一点。
We solved this... C3P0 was configured to drop connections that were checked out longer than 30 seconds, we did this to prevent dead-lock in the database (we don't control the tuning). One of the transactions was taking horridly long to complete, and C3P0 was dropping the connection, resulting in the ResultSet Closed error. Surprisingly, however, C3P0 was not logging the incident, so we didnt see this in the application's logs.