5.7 事务
- // 本例仅为示例, 并未严格考虑账户状态等业务逻辑
- @Before(Tx.class)
- public void trans_demo() {
- // 获取转账金额
- Integer transAmount = getParaToInt("transAmount");
- // 获取转出账户id
- Integer fromAccountId = getParaToInt("fromAccountId");
- Integer toAccountId = getParaToInt("toAccountId");
- // 转出操作
- Db.update("update account set cash = cash - ? where id = ?",
- transAmount, fromAccountId);
- // 转入操作
- Db.update("update account set cash = cash + ? where id = ?",
- transAmount, toAccountId);
- }
- @TxConfig("otherConfigName")
(Tx.class)
public void doIt() {
…
}
2、Db.tx 事务
- Db.tx(new IAtom() {
- public boolean run() throws SQLException {
- Db.update("update t1 set f1 = ?", 123);
- Db.update("update t2 set f2 = ?", 456);
- return true;
- });