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

來源:互聯網
上載者:User

一、註解 @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,二較一更全面,處理更細膩,推薦使用二。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.