普通的Spring + Hibernate事务行为? [英] Normal Spring + Hibernate Transaction behaviour?
问题描述
我已经使用Spring AOP将我的Spring + Hibernate项目配置为事务处理了。
当我检查在DAO方法中使用 我的aop:config如下所示: 我终于弄清楚了 该文件说你需要为Spring引发一个异常,以知道某个错误的 TransactionSynchronizationManager.isActualTransactionActive()
它表示为真。
我从服务类中调用两个DAO方法而在第二个DAO方法中,我会执行'devide by zero'
来故意抛出一个错误,并查看两个方法是否回滚。
目前第一个似乎插入新纪录就好了。不应该第一种方法回滚?
更新
你可以参考我以前的问题,但我认为这可能太冗长。
Spring 3.x和Hibernate 4中的事务管理 a>
总结这个问题(尽可能多),
I有一个服务类方法可以调用DAO类中的两个方法。
该服务类是从spring mvc控制器及其代理调用的。
< aop:config proxy -target级= 真 >
我的界面Crudable看起来像这样
public interface Crudable {
public int delete(CooingbeeFetchable bean);
public int save(CooingbeeFetchable bean);
public int update(CooingbeeFetchable bean);
public Set< CooingbeeFetchable>列表(CooingbeeFetchable bean);
public CooingbeeFetchable get(int id);
public int count(CooingbeeFetchable bean);
public int save2(CooingbeeFetchable bean);
两者 DAO和Service类实现Cruadable接口。现在我想可能是因为b $ b的原因,它不能按我的预期工作吗?
希望我给你足够的信息,不要太冗长:)
结果。
阅读此文。
http://static.springsource.org/spring /docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-claclarative-rolling-back
和DAO方法,以便它可以回滚。以前,我的DAO方法没有抛出任何异常,而是通过将方法体放在
try-catch块中自行处理异常。
< aop:config proxy -target级= 真 >
< / aop:config>
我希望这有助于您! :)
欢呼
I've got my Spring + Hibernate project configured to be transactional using Spring AOP.
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
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.
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>
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);
}
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 : )
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
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.
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屋!