Can't start a cloned connection while in manual transaction mode.错误的解决方案

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.setupImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.<init>(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.<init>(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.prepareStatement(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.prepareStatement(Unknown Source)
at com.capitel.telecom.tobacco.dao.TightsmokeDAO.insertTightsmokeInfo(TightsmokeDAO.java:57)
at com.capitel.telecom.tobacco.logic.DataMaintainManager.tightsmokeAdd(DataMaintainManager.java:616)

。。。。。。。。。。

上面是从mysql数据库升级到SQL Server数据库时,第一次报的错误

从网上找了一些解决方案,先PS在下面:

--------------------------------------------------------------------------------------------------------------------------------
解决方案:(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即可

总结一下:

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

你可能感兴趣的:(sql,mysql,SQL Server,jdbc,Microsoft)