Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

5.6 Declared transactions


May 14, 2021 JFinal manual



ActiveRecord supports well-named transactions, which need to be implemented using interceptors provided by ActiveRecordPlugin, which are configured in the relevant section of Interceptor. The following code is an example of a declared transaction:

This example is an example only and does not strictly consider business logic such as account status
@Before(Tx. class )
public void trans_demo() {
Get the transfer amount
Integer transAmount = getParaToInt("transAmount");
Get the transfer-out account id
Integer fromAccountId = getParaToInt("fromAccountId");
Get the transfer account ID
Integer toAccountId = getParaToInt("toAccountId");
Turn out the operation
Db.update("update account set cash = cash - ? where id = ?", transAmount, fromAccountId);
Go to the operation
Db.update("update account set cash = cash + ? where id = ?", transAmount, toAccountId);
}

In the above code, just one Tx interceptor is declared to add transaction support to action. In addition, ActiveRecord is equipped with TxByActionKeys, TxByActionKeyRegex, TxByMethodRegex, which support actionKeys, actionKeys, actionMethods, actionMethods, and actionMethod claims, respectively, and the following is the sample code:

public void configInterceptor(Interceptors me) { me.add( new TxByMethodRegex("(.*save.*|. * update.*)")); me.add( new TxByMethods("save", "update"));

me.add( new TxByActionKeyRegex("/trans.*")); me.add( new TxByActionKeys("/tx/save", "/tx/update"));

The TxByRegex interceptor in the example above intercepts action by passing in regular expressions, and the transaction opens when the actionKey is matched. TxByActionKeys can intercept and turn on transactions on the specified actionKey, and TxByMethods can intercept and turn on transactions on the specified method.

Note: The MySql database table must be set to the InnoDB engine to support transactions, and MyISAM does not support transactions.