事务管理

2019-01-24  本文已影响0人  wei_lu_lu

编程式事务管理:

public boolean transfer(final Long fromId, final Long toId, final double amount) {
     return (Boolean) transactionTemplate.execute(new TransactionCallback(){
        public Object doInTransaction(TransactionStatus status) {
           Object result;
           try {
             result = bankDao.transfer(fromId, toId, amount);
           } catch (Exception e) {
             status.setRollbackOnly();
             result = false;
             System.out.println("Transfer Error!");
           }
         return result;
        }
      });
   }

声明式事务管理:

@Transactional(propagation = Propagation.REQUIRED)
   public boolean transfer(Long fromId, Long toId, double amount) {
   return bankDao.transfer(fromId, toId, amount);
}
上一篇 下一篇

猜你喜欢

热点阅读