一、用注解 @Before(Tx.class) 实现 事务回滚
@Before(Tx.class)
public void pay() throws Exception {
//throws exception;
}
方法体不能扑捉异常,所有的异常都抛出,当出现异常时事物将回滚(即 事务的回滚 是依赖 抛出异常 来实现的)
优点:简单暴力,不需要去处理每个异常,直接抛出即可;
缺点:不能详细的区分返回数据、视图,只能笼统的报出异常;
二、Db.tx(new IAtom(){})
public void pay() {
final Map map = new HashMap();
boolean bl = Db.tx(new IAtom() {
@Override
public boolean run() throws SQLException {
if (...) {
//...
return false;
} else {
...
return true;
}
return true;
}
});
this.rendJson(bl, null, map.get("return_words"), null);
}
return false 或者 有异常抛出 都会 回滚事务,return true 才会提交事务;
Db.t