开发者

Spring Transaction Manager

I have a j2ee application running开发者_如何转开发 on spring framework. I am implementing a transaction manager with AOP. It works fine.

When an exception occurs in my method it is detected by my AOP configuration and rolls back the changes in the DB. But the problem is the code where you expect the error should not be surrounded with try-catch. When I surround it with try-catch it won't roll-back. But I need to do some stuffs like logging whenever there are errors and the only place I can think of placing it is in the catch block.

public class RegisterGLogic implements BLogic
{

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

              //do some  db operation

               return result;
        }
}

here's my AOP transaction configuration

<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>


You can re-throw the exception after logging. That will make Spring do a rollback.


First, why don't you use the built-in transaction managers? They are surely more stable than yours.

Second, you can rethrow a RuntimeException I guess, or even let the exception bubble-up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜