Spring声明式事务模式下事务似乎不起作用
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当自定义异常扩展了 RuntimeException 时,Spring 才会在异常时回滚事务。确保
FooException
扩展RuntimeException
(参考):根据您的评论,还有两件事需要检查:1)确保您配置了平台事务管理器。示例:
此外,如果您想对事务使用注释,您也需要进行配置:
Spring rolls back transactions on exception only if the custom exception extends
RuntimeException
. Make sureFooException
extendsRuntimeException
(reference):Based on your comment, here are 2 more things to check: 1) make sure you have a platform transaction manager configured. Example:
Additionally, if you want to use annotations for transactions, you need to config that too: