iBatis使用SQLServer出现Can't start a cloned connection while in manual transaction mode.错误

        今天使用spring+iBatis+SQL Server2000做东东,可在执行query时老抛异常:
Cause: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.

        然而之前使用MySQL的时候还是好好的,怎么改用了SQL Server后就变了样呢?后来上网发现,这个异常是由于“在一个SQL SERVER的JDBC连接上执行多个STATEMENTS的操作,或者是手动事务状态(AutoCommit=false) 并且使用默认的模式. direct (SelectMethod=direct) 模式”。其实不仅仅是iBatis,有时候如果在JDBC中的一个connection写了好些SQL语句,但又忘了将connection关闭,那么也会出现类似的问题。

先看看从别处摘下来的解决方法吧:

--------------------------------------------------------------------摘要部分--------------------------------------------------------------------
解决方案:(1)

你在尝试打开一个克隆连接,有两种解决办法: 每个数据库的连接在执行完毕后,关闭,第二,在连接时,判断是否已经有连接存在,有则使用现成的连接。 数据库连接可以设置成一个常量,不需要到处定义。便于维护。

解决方案:(2)

你在DatabaseURL字符串中加入 SelectMethod=Cursor;如下
jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Partner;SelectMethod=Cursor;

--------------------------------------------------------------------------------------------------------------------------------

If you pass "SelectMethod=cursor" attribute in connectio url, will allow more than one active statement per connection." jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor;","user","pwd"The reason for this is;When SelectMethod is set to direct, SQL Server does not support multiple active statements on a single connection within a transaction; however, when auto-commit mode is enabled (the default), the Microsoft SQL Server JDBC driver provides the ability to have multiple JDBC statements open on a single JDBC connection. This is done by cloning physical SQL Server connections as needed. To avoid cloning physical SQL Server connections in this circumstance, you should create only one Statement, PreparedStatement, CallableStatement, or DatabaseMetaData object per JDBC Connection
object. Be sure to invoke the "close" methods on these objects when you are finished with them, that is, before creating another object of the types described.Sridhar PaladuguMicrosoft Developer SupportJDBC Webdata

--------------------------------------------------------------------------------------------------------------------------------
是 因为commit设置为false了,查资料,对URL做修 改:jdbc:microsoft:sqlserver://localhost:1433;user=test;password=secret;SelectMethod=cursor;问题解决。

--------------------------------------------- 感觉比较有理由的下面的这个说法------------------------------------------------
原因一般是 当你在一个SQL SERVER的JDBC连接上执行多个STATEMENTS的操作,或者是手动事务状态(AutoCommit=false) 并且使用默认的模式. direct (SelectMethod=direct) 模式.

解决办法
当你使用手动事务模式时,必 须把SelectMethod 属性的值设置为 Cursor, 或者是确保在你的连接只有一个STATEMENT操作。

我的解决办法:

在连接数据库的URL后面添加了一段SelectMethod=cursor;就可以正常使用了

--------------------------------------------------------------------摘要部分 --------------------------------------------------------------------

其实,iBatis也不是没有漏洞的(好像是废话),就这个问题,我发现原来iBatis的编写者在写iBatis和spring集成时出现了严重的BUG,在SqlMapClientTemplate类中有这样一句话:

finally {
// Only close SqlMapSession if we know we've actually opened it
// at the present level.
if (ibatisCon == null) {
session.close();
}
}

稍微仔细的朋友就会发现:这段代码跟作者的初衷是不一致的!本来是想“假如SqlMapSession已经打开了,我们就将它关闭”,但ibatisCon==null的判断缺与之南辕北辙!!!

所以,问题的症结应该就出现在这里了。至于为什么MySQL没有出现这个问题,而SQL Server缺出现了,我就不再写了,我想可以参考一下这篇文章:http://looxiaohu.javaeye.com/blog/235840,应该对我们会有一定的帮助的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值