2010-03-23 111 views
0

我對Spring框架運行的J2EE應用程序。我正在使用AOP實現事務管理器。它工作正常。
當異常在我的方法它是由我的AOP配置中檢測到發生並回滾在DB中的變化。但問題在於你期望錯誤不應該被try-catch包圍的代碼。當我用try-catch包圍它時,它不會回滾。但是我需要做一些事情,比如每當有錯誤時記錄,我能想到的將它放在catch塊中的唯一位置。Spring的事務管理器

public class RegisterGLogic implements BLogic 
{ 

     public BLogicResult execute() 
     { 
       BLogicResult result = new BLogicResult(); 

       //do some db operation 

       return result; 
     } 
} 

這裏是我的AOP事務配置

<bean id="TerasolunaDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="PrototypeDataSource" /> 
    </bean> 
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager"> 
    <tx:attributes> 
    <tx:method name="insert*" propagation="REQUIRED" 
    rollback-for="java.lang.Exception" /> 
    <tx:method name="execute*" propagation="REQUIRED" 
    rollback-for="java.lang.Exception" /> 
    <tx:method name="*" propagation="REQUIRED" read-only="true" /> 
    </tx:attributes> 
</tx:advice> 
<!-- AOPの設定 --> 
<aop:config> 
    <aop:pointcut id="blogicBeans" expression="bean(*BLogic)" /> 
    <aop:pointcut id="serviceBeans" expression="bean(*Service)" /> 
    <aop:advisor pointcut-ref="blogicBeans" advice-ref="transactionInterceptor" /> 
    <aop:advisor pointcut-ref="serviceBeans" advice-ref="transactionInterceptor" /> 
</aop:config> 
+0

怎麼樣給代碼? – Bozho 2010-03-23 15:53:43

+0

我上面 – cedric 2010-03-23 17:20:36

回答

0

登錄後可以重新拋出異常。這將使Spring進行回滾。

0

首先,你爲什麼不使用內置事務管理器?他們肯定比你更穩定。

其次,你可以重新拋出一個RuntimeException我猜想,甚至讓異常泡沫了。

+0

更新我已經使用了內置的DataSourceTransactionManager,等我的方法,我想回滾做的是一類特定的框架的覆蓋方法嘗試。我在這裏使用terasoluna框架。這是來自日本的框架。當我重新拋出異常時,我收到一個錯誤,說明沒有處理異常。我也不允許在重寫方法 – cedric 2010-03-23 17:10:13

+0

中添加thorws,爲什麼不使用'DataSourceTransactionManager'? – Bozho 2010-03-23 17:47:04

+0

我已經是。我回去使用它 – cedric 2010-03-24 01:06:30