jfinal事物回滚,@Before(Tx.class)和Db.tx(new IAtom() { })

来源:互联网 发布:广联达软件官网 编辑:程序博客网 时间:2024/04/30 05:44

一、注解 @Before(Tx.class) 加事物回滚

@Before(Tx.class)public void pay() throws Exception {//throws exception;}

方法体不能扑捉异常,所有的异常都抛出,当出现异常时事物将回滚

优点:简单暴力,不需要去处理每个异常,直接抛出即可;

缺点:不能详细的区分返回数据、视图,只能笼统的报出异常


二、Db.tx(new IAtom() { }) 

public void pay() {final Map<String,String> map = new HashMap<String, String>();boolean bl = Db.tx(new IAtom() {@Overridepublic 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.tx 方法是有返回值true/false,可对改返回值作业务返回,如果想让 run 方法中往外层传递变量,可以在外层定义一个 final 修饰的容器类的对象或者定义map,二较一更全面,处理更细腻,推荐使用二。

0 0
原创粉丝点击