Spring事务管理器
我有一个在 spring 框架上运行的 j2ee 应用程序。我正在使用 AOP 实现事务管理器。效果很好。
当我的方法中发生异常时,我的 AOP 配置会检测到异常并回滚数据库中的更改。但问题是您期望错误的代码不应该被 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在记录后重新抛出异常。这将使 Spring 进行回滚。
You can re-throw the exception after logging. That will make Spring do a rollback.
首先,为什么不使用内置的事务管理器?他们肯定比你的更稳定。
其次,我猜你可以重新抛出 RuntimeException,甚至让异常冒泡。
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.