我有一个运行在spring框架上的j2ee应用程序。我正在用AOP实现一个事务管理器。它工作得很好。
当我的方法中发生异常时,我的AOP配置会检测到它,并回滚DB中的更改。但问题是代码中您期望的错误不应该包含在try-catch中。当我用try-catch包围它时,它不会回滚。但我需要做一些事情,比如每当出现错误时记录日志,而我唯一能想到的地方就是将它放在catch块中。
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>发布于 2010-03-23 23:50:39
您可以在日志记录后重新抛出异常。这将使Spring进行回滚。
发布于 2010-03-23 23:53:34
首先,为什么不使用内置的事务管理器?他们肯定比你的更稳定。
其次,我想你可以重新抛出一个RuntimeException,甚至让异常冒泡起来。
https://stackoverflow.com/questions/2501323
复制相似问题