views:

320

answers:

2

Hi. 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>
A: 

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

rodrigoap
A: 

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.

Bozho
I already tried using the built in DataSourceTransactionManager, etc. My method where I want the roll back to be done is an overriden method of a class of a particular framework. I am using here terasoluna framework. It's a framework from japan. When I rethrow the exception I get an error that an exception is not handled. I am as well not allowed to add thorws in the overriden method
cedric
and why aren't you using the `DataSourceTransactionManager` ?
Bozho
I already am. I went back to using it
cedric