Spring声明式事务模式下事务似乎不起作用

发布于 2024-12-01 16:31:35 字数 794 浏览 5 评论 0原文

我正在尝试在 Spring+Struts+Hibernate 设置中实现声明式事务管理。 这是我的 applicationcontext.xml 的一部分

<property>
  <props>
    <prop key="foodoo*">PROPAGATION_REQUIRED,-FooException</prop>
  </props>
</property>

,我在 FooService 类中定义了两个公共方法 -> UpdateFoo、foodooTest 和两个私有方法(具有更新逻辑)-> Test1 和 Test2

调用如下:

UpdateFoo->foodooTest->Test1
                     ->Test2

(foodooTest 应该触发事务) 我创建 bean

FooService fooService = (FooService)context.getBean("fooService");
//and call
fooService.UpdateFoo();

Test2 抛出 FooException,因此我希望事务回滚。但事实并非如此。提交发生在每个 Test1 和 Test2 中(直到出现异常)。 我使用 Hibernate getHibernateTemplate.Merge() 进行更新。我的数据库引擎是InnoDB。

我不确定我在这里缺少什么。

I am trying to implement declarative transaction management in a Spring+Struts+Hibernate setup.
This is a part of my applicationcontext.xml

<property>
  <props>
    <prop key="foodoo*">PROPAGATION_REQUIRED,-FooException</prop>
  </props>
</property>

I have defined two public methods in my FooService class -> UpdateFoo, foodooTest
and two private methods (which have the update logic) -> Test1 and Test2

The call goes like:

UpdateFoo->foodooTest->Test1
                     ->Test2

(foodooTest should trigger the transaction)
I create the bean

FooService fooService = (FooService)context.getBean("fooService");
//and call
fooService.UpdateFoo();

Test2 throws FooException and hence I expect the transaction to roll back. But it does not. Commit happens in each Test1 and Test2 (until the exception).
I use Hibernate getHibernateTemplate.Merge() for the updates. My db engine is InnoDB.

I'm not sure what am I missing here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷情 2024-12-08 16:31:35

仅当自定义异常扩展了 RuntimeException 时,Spring 才会在异常时回滚事务。确保 FooException 扩展 RuntimeException(参考):

在其默认配置中,Spring 框架的事务基础结构代码仅在运行时、未检查异常的情况下标记要回滚的事务;也就是说,当抛出的异常是 RuntimeException 的实例或子类时。 (默认情况下,错误也会导致回滚)。从事务方法引发的已检查异常不会导致默认配置中的回滚。

根据您的评论,还有两件事需要检查:1)确保您配置了平台事务管理器。示例:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
</bean>

此外,如果您想对事务使用注释,您也需要进行配置:

<tx:annotation-driven />

Spring rolls back transactions on exception only if the custom exception extends RuntimeException. Make sure FooException extends RuntimeException(reference):

In its default configuration, the Spring Framework's transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. (Errors will also - by default - result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration.

Based on your comment, here are 2 more things to check: 1) make sure you have a platform transaction manager configured. Example:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
</bean>

Additionally, if you want to use annotations for transactions, you need to config that too:

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