以前的一个项目用jdbc操作数据库,事务方面未做好。最近出现了许多问题,所以对它进行修改。
只好采用最原始的connection.setAutoCommit(false);来设置事务,由于业务逻辑过于复杂,这个方法写了381行。当中涉及到许多增删改查操作,其中用到了多个ResultSet。经过测试发现,当出现多个ResultSet时经常会报错说ResultSet已关闭。
在网上查了一下,是由于一个Statement操作获得多个ResultSet时就会产生这种错误。这种情况下只需要多创建几个Statement,当需要多个ResultSet时分别用多个Statement执行就不会报错了。
我只写了解决方法,至于为什么会这样,具体原因我也不太了解。
查到的原因如下:
the object used for executing a static sql statement and returning the results it produces. by default, only one resultset object per statement object can be open at the same time. therefore, if the reading of one resultset object is interleaved with the reading of another, each must have been generated by different statement objects. all execution methods in the statement interface implicitly close a statments current resultset object if an open one exists.
原文地址:http://www.sudu.cn/info/html/edu/java/20071225/19718.html
只好采用最原始的connection.setAutoCommit(false);来设置事务,由于业务逻辑过于复杂,这个方法写了381行。当中涉及到许多增删改查操作,其中用到了多个ResultSet。经过测试发现,当出现多个ResultSet时经常会报错说ResultSet已关闭。
在网上查了一下,是由于一个Statement操作获得多个ResultSet时就会产生这种错误。这种情况下只需要多创建几个Statement,当需要多个ResultSet时分别用多个Statement执行就不会报错了。
我只写了解决方法,至于为什么会这样,具体原因我也不太了解。
查到的原因如下:
the object used for executing a static sql statement and returning the results it produces. by default, only one resultset object per statement object can be open at the same time. therefore, if the reading of one resultset object is interleaved with the reading of another, each must have been generated by different statement objects. all execution methods in the statement interface implicitly close a statments current resultset object if an open one exists.
原文地址:http://www.sudu.cn/info/html/edu/java/20071225/19718.html