2

除了@Transactional註釋之外,春季的回滾事務的替代方法是什麼。 我已經使用這個註釋,但我想要的方式,我可以回滾catch塊的交易。有什麼辦法嗎?@Transactional註釋的替代

Thanx提前。

+0

你爲什麼需要在catch塊回滾? – shlomi33 2014-09-11 08:59:57

+0

我有我需要在catch塊中回滾的情況,並需要在catch塊中執行一些過程。 – 2014-09-11 09:04:18

+0

你需要回滾,然後處理或處理,然後回滾? – shlomi33 2014-09-11 09:10:03

回答

2

這裏是一個草案:

public class SomeService implements SomeInterface { 

    private SomeDao thisDaoWrapsJdbcTemplate; 
    private PlatformTransactionManager transactionManager; 

    public void setTransactionManager(PlatformTransactionManager transactionManager) { 
     this.transactionManager = transactionManager; 
    } 

    public void doBusiness(Business: business) { 

     TransactionDefinition def = new DefaultTransactionDefinition(); 
     TransactionStatus status = transactionManager.getTransaction(def); 

     try { 

     // do business here 
     Money money = Money.LOTS_OF 
     ... 
     // wire the money in.. 
     thisDaoWrapsJdbcTemplate.depositLotsOfMoney(money) 

     transactionManager.commit(status); 

     } catch (DataAccessException dae) { 

     transactionManager.rollback(status); 
     throw dae; 
     } 
     return; 
    } 
+0

我在catch塊中使用了TransactionAspectSupport.currentTransactionStatus()。setRollbackOnly(),它在回滾塊中回滾事務。但是,如果我在catch line中的這條線之後做任何交易怎麼辦?也會回滾? – 2014-09-11 09:32:34

+0

在上面的草案中將僅回滾事務狀態(當前)...如果您設置RollbackOnly,通過javadoc「這會指示事務管理器事務的唯一可能結果可能是回滾」 – Xstian 2014-09-11 09:41:16