程序很长,但是都知道这种程序意思很简单的:package testSetAutoCommit;import java.sql.*;public class TestSetAutoCommit {
private static final String DRIVER="com.mysql.jdbc.Driver";
static{try{
Class.forName(DRIVER);
}catch(Exception e){
e.printStackTrace();
}
}
private static final String URL="jdbc:mysql://localhost/mybbs?useUnicode=true&characterEncoding=gb2312";
private static final String USERNAME="root";
private static final String PASSWORD="root";
public static Connection getConnection(){
try{
Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void closeConnection(ResultSet rs,PreparedStatement pst,Connection conn)
{
try{
if(rs!=null)
{
rs.close();
}
}catch(Exception e){}

try{
if(pst!=null)
{
pst.close();
}
}catch(Exception e){}

try{
if(conn!=null)
{
conn.close();
}
}catch(Exception e){}

}

public static void main(String []aa){
TestSetAutoCommit p=new TestSetAutoCommit();
     Connection conn=p.getConnection();
    
     String sql1="update board  set boardName='5555'  where boardId=20";
     String sql2="update board  set boardName='4444'  where boardId=21";;
     Statement stmt=null;
     try {
    
conn.setAutoCommit(false);
stmt=conn.createStatement();
stmt.executeUpdate(sql1);
                                /*********************关键在于这句**************************/                              Thread.sleep(6000);
                                 /***********************************************/ stmt.executeUpdate(sql2);

conn.commit();
conn.setAutoCommit(true);

} catch (Exception e) {

e.printStackTrace();
try {
if(conn!=null){
conn.rollback();
conn.setAutoCommit(true);
}
} catch (SQLException e1) {
e1.printStackTrace();
}

}finally{
try {
if(stmt!=null){stmt.close();}
if(conn!=null){conn.close();}
} catch (SQLException e) {
e.printStackTrace();
}

}
    }
}问题: 都已经有了  Thread.sleep(6000);
意思明明是该在6秒钟以后 同时改变数据库的两条记录,
但是,在我不断刷新mysql的过程中,第一条记录一运行就改变了,第二条才是运行后6秒钟在改变,
这明明就不是conn.setAutoCommit(FALSE)该有的阻止自动提交的功能的嘛!
我觉得数据库中两条记录应该是6秒钟以后同时改变的呀!

解决方案 »

  1.   

    怎么没有人来看一下呀?
    再补充一下我的问题:我根本就没有发现设置了conn.setAutoCommit(false/true)的任何作用,
    因为后来我在第二条sql语句中个设置了一个错误,当然已经catch到了Exception,
    按理说应该回滚,但是很失望,第一条语句在数据库中修改成功了,第二句却没有。怎么回事?急!急!
      

  2.   

    mysql数据库是吧,检查下你的数据库支持事务吗先。查看你的MySQL数据库引擎和表类型。
    以上。
      

  3.   

    哦,还要设置啊,从来就不知道还有设置mysql!怎么设置mysql呢?
      

  4.   

    如果MYSQL支持事务,这样方式可以提交或者回滚么?
      

  5.   

    怎么知道他支不支持事事物呢,或者怎么设置mysql呢 ?
      

  6.   

    你什么版本的mysql?老的不支持,新的都支持了吧
      

  7.   

    试试 Connection conn=TestSetAutoCommit.getConnection();