代码很简单啊!就是得到一个连接:代码1:Connection co = DriverManager.getConnection(url);
Statement st1 = co.createStatement();
Statement st2 = co.createStatement();这段代码不会抛出异常。代码2:Connection co = DriverManager.getConnection(url);
co.setAutoCommit(false);
Statement st1 = co.createStatement();
Statement st2 = co.createStatement();此代码抛出异常:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a
cloned connection while in manual transaction mode.“cloned connection ”新建一个克隆连接?难道说,当一个Connection建立两个Statement时,会建立一个新连接(克隆自身数据),然后用该连接产生第二个Statement?
当连接处于自动提交模式时,可以克隆连接。而当处于用户提交模式时,无法克隆连接,抛出异常。可是当我让处于自动提交模式的连接产生两个Statement,然后分别对它们调用getConnection(),发现产生两个Statement的Connection是相同的,也就是说,没有克隆。那麽,既然处于自动提交模式下的同一个连接可以产生两个Statement,为什麽处于用户提交模式时就无法产生两个Statement?两个会话的事务可以互不影响啊!而且异常中的cloned connection 是怎麽回事?

解决方案 »

  1.   

    楼主,我用下面的代码测试没有你所说的问题。不过我用的是MYSQL,不是SQL Server,我以前做也不会有你所说的那样的问题。import java.sql.*;
    public class  MutilStatement
    {
    public static void main(String[] args) 
    {
    try 
    {
    Class.forName("org.gjt.mm.mysql.Driver");
    }
    catch(ClassNotFoundException e)
    {
    System.out.println("ClassNotFoundException ->"+e);
    }
    try
    {
    String url="jdbc:mysql://localhost:3306/simple?useUnicode=true&useUnicode=true&characterEncoding=GBK";
    String uid = "root";
    String pwd = "roger";
    Connection con = DriverManager.getConnection(url,uid,pwd);
    Statement stat=con.createStatement();
    Statement state=con.createStatement();
    System.out.println(true);
    }
    catch(SQLException e)
    {
    System.out.println(e);
    }
    }
    }
      

  2.   

    co.setAutoCommit(false);就是加上这句也没有问题。
      

  3.   

    SelectMethod 属性设置为 Cursor
      

  4.   

    楼上的说得对,将SelectMethod 属性设置为 Cursor
      

  5.   

    请问一下,SelectMethod是在哪个类里?
      

  6.   

    谢谢了,SelectMethod=Cursor是什麽意思啊?搞懂后,马上给分!
      

  7.   

    而且,若调用Connection.commit(),会提交哪个Statement的事务啊?都提交吗?
      

  8.   

    con = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;SelectMethod=Cursor;User=User;Password=Password");
      

  9.   

    我想知道,SelectMethod=Cursor作用是什麽?为什麽加上它就不会抛出异常?而且,若调用Connection.commit(),会提交哪个Statement的事务啊?都提交吗?
      

  10.   

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;q313181