错误信息:
Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
原因:
在手工事务模式 (AutoCommit=false) 下,如果使用直接 (SelectMethod=direct) 模式,当您尝试对使用 JDBC 驱动程序的 SQL Server 数据库执行多个语句时,将会出现此问题。直接模式是该驱动程序的默认模式
解决方法:
当您使用手工事务模式时,必须将驱动程序的 SelectMethod 属性设置为 Cursor。
相关测试代码:
package test.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class TestCur2000 { public static void main(String[] args) throws Exception { Connection con = getCon(); //设置事务不自动提交 con.setAutoCommit(false); //创建第一个句柄 PreparedStatement ps = con.prepareStatement("select * from t_file_user"); ResultSet rs = ps.executeQuery(); int count = 0; while(rs.next()){ count++; } System.out.println("count="+count); //创建第二个句柄 PreparedStatement ps2 = con.prepareStatement("select * from t_audit"); } public static Connection getCon() { try { //添加 SelectMethod=cursor 可解决该问题 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testlab;SelectMethod=cursor"; //String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testlab"; Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); return DriverManager.getConnection(url, "sa","loushang"); } catch (Exception e) { e.printStackTrace(); } return null; } }