在jfinal中如何实现嵌套的事物

2025-04-23 17:04:48
推荐回答(1个)
回答1:

Db.tx 事务

嵌套(手写待测, 原理就这)

public boolean  fn() {
if(
Db.tx(new IAtom() {
   public boolean run() throws SQLException {
      Db.update("update t1 set f1 = ?", 123);
      Db.update("update t2 set f2 = ?", 456);
      return true;
   }
})){

Db.tx(new IAtom() {
   public boolean run() throws SQLException {
      Db.update("update t1 set f1 = ?", 123);
      Db.update("update t2 set f2 = ?", 456);
      return true;
   }
});

}
 return true;
}

注意:MySql数据库表必须设置为InnoDB引擎时才支持事务,MyISAM并不支持事务。