代码:
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost/zhonghongfa?"
+ "user=root&password=841017");

conn.setAutoCommit(false);

pstmt = conn.createStatement();

pstmt.addBatch("insert into ddltable(id,name) values(301,'zhang')");
pstmt.addBatch("insert into ddltable(id,name) values(302,'zhsdfg')");

pstmt.executeBatch();

conn.rollback();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
try {
if(conn!=null)
{
conn.rollback();
conn.setAutoCommit(true);
}
} catch (SQLException e) {
e.printStackTrace();
}

}
代码中没有价conn.commit();甚至还进行回滚conn.rollback();但是看数据库,数据却都是插入了!
为什么会这样?

解决方案 »

  1.   

    和setAutoCommit(false)起不起作用么有任何关系。mysql数据库在创建时需要指定事务类型。并不是所有mysql数据库都是2阶段提交。
      

  2.   

    http://hi.baidu.com/piaochen/item/96730da849a69f706cd455ff
      

  3.   

    哦,明白了,是存储引擎的原因!mysql 只有是InnoDB类型的表,才会支持事务操作!!