正常的 Spring + Hibernate 事务行为? [英] Normal Spring + Hibernate Transaction behaviour?

查看:33
本文介绍了正常的 Spring + Hibernate 事务行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Spring AOP 将我的 Spring + Hibernate 项目配置为事务性的.

I've got my Spring + Hibernate project configured to be transactional using Spring AOP.

当我使用 TransactionSynchronizationManager.isActualTransactionActive() 检查 DAO 方法时,它显示为 true.

我正在从我的服务类调用两个 DAO 方法,在第二个 DAO 方法中,我故意执行 'devide by zero' 以抛出错误并查看这两个方法是否回滚.

目前第一个似乎插入新记录就好了.第一种方法不应该
回滚吗?

更新您可以参考我之前的问题,但我认为这可能太长了.
Spring 3.x 和 Hibernate 4 中的事务管理

When I check in the DAO method with TransactionSynchronizationManager.isActualTransactionActive() it says true.

I'm calling two DAO methods from my service class and in the second DAO method, I do 'devide by zero' intentionally to throw an error and see if the two method rollback.

Currently the first one seems to insert a new record just fine. Shouldn't the first method
rollback ?

Update You can refer to my previous question but I think this may be too lengthy.
Transaction Management in Spring 3.x and Hibernate 4


总结一下这个问题(尽我所能),
我有一个从 DAO 类调用两个方法的服务类方法.
正在从 spring mvc 控制器及其委托者调用该服务类.


To sum the question (as much as I can),
I have one service class method that calls two methods from a DAO class.
That service class is being called from a spring mvc controller and its delegater.

我的 aop:config 如下

I've got my aop:config as follows

<aop:config proxy-target-class="true">
  <aop:pointcut id="transactionalServiceOperation" expression="execution(* kr.co.sgis.services.web.cooingbee.Crudable.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionalServiceOperation"/>
</aop:config>


我的界面 Crudable 看起来像这样


My interface Crudable looks like this

public interface Crudable {

    public int delete(CooingbeeFetchable bean);
    public int save(CooingbeeFetchable bean);
    public int update(CooingbeeFetchable bean);
    public Set<CooingbeeFetchable> list(CooingbeeFetchable bean);
    public CooingbeeFetchable get(int id);
    public int count(CooingbeeFetchable bean);
    public int save2(CooingbeeFetchable bean);
}


DAO 和 Service 类都实现了 Cruadable 接口.现在我想也许这就是原因是它没有按我预期的那样工作?
希望我给了你足够的信息而不会太长:)


Both DAO and Service class implement Cruadable interface. Now I think maybe that's the reason it's not working as I expected ?
Hope I gave you enough information without being too lengthy : )

推荐答案

我终于想通了.
读这个.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-rolling-back

I've figured it out finally.
Read this.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-rolling-back

文档说需要抛出异常才能让Spring知道有问题使用 DAO 方法,以便它可以回滚.以前我的 DAO 方法没有抛出任何异常,而是通过将方法体包围在一个try-catch 块.

The document says you need to throw an exception for Spring to know that something's wrong with a DAO method so that it could rollback. Previously my DAO methods didn't throw any exceptions, instead they handled exceptions by themselves by surrounding the method body in a try-catch block.

另外,你需要像这样配置你的 aop:config 标签.

also, you need to configure your aop:config tag like this.

<aop:config proxy-target-class="true">
  <aop:pointcut id="transactionalServiceOperation" expression="execution(* kr.co.sgis.services.web.cooingbee.Crudable.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionalServiceOperation"/>
</aop:config>

我希望这会有所帮助!:)
干杯

I hope this helps ! : )
cheers

这篇关于正常的 Spring + Hibernate 事务行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆