Spring事务管理器

发布于 2024-08-26 14:58:31 字数 1503 浏览 2 评论 0原文

我有一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-09-02 14:58:31

您可以在记录后重新抛出异常。这将使 Spring 进行回滚。

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

唔猫 2024-09-02 14:58:31

首先,为什么不使用内置的事务管理器?他们肯定比你的更稳定。

其次,我猜你可以重新抛出 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文