try {
connection.setAutoCommit(false);
bankDao.transferMoney(+2000, 1, connection); //帐号1 增加2000
if(true){
throw new RuntimeException(); //模拟报错
}
bankDao.transferMoney(-2000, 2, connection);//帐号2 减少2000
connection.commit();
} catch (Exception e) {
if(connection!=null){
try {
connection.rollback();
System.out.println("回滚事务");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}finally{
if(connection!=null){
connection.close();
}
}
记录下网上其他人的说法:
本次两个SQL语句包含在一个事物中。
在你的这个事物中包含了更新操作,而mysql的 innodb