JFinal Database "manual" transactions (commit, rollback)

First, the transaction is rolled back with the realization comment @Before (Tx.class)

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

  

The method can not capture the abnormal body, all exceptions are thrown when an exception occurs things will roll back (ie, roll back the transaction is dependent thrown achieved)

Pros: Simple violence, do not need to deal with each exception can be thrown directly;

Disadvantages: can not return data to distinguish detailed, view, only general reported anomaly;

 

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

public void pay() {
    final Map<String,String> map = new HashMap<String, String>();
    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);
}

  

  1. return false or an exception will be thrown  回滚事务, return true only to commit the transaction;
  2. Db.tx return value is true / false, may be returned as the return value of the service;
  3. If you want to run () method outwardly variable transmission layer, the outer layer may be modified at a final definition  容器类的对象 or  定义map;

Method two methods compared with a more comprehensive, more delicate handling, we recommend two.

 

Note: Method II can be abbreviated (Java8 grammar)

public void pay() {
    final Map<String,String> map = new HashMap<String, String>();
    boolean bl = Db.tx(() -> {
        if (...) {
            //...
            return false;
        } else {
            ...
            return true;
        }
          
        return true;
    });
      
    this.rendJson(bl, null, map.get("return_words"), null);
}

  

 

Guess you like

Origin www.cnblogs.com/freespider/p/11871589.html