Normal Spring + Hibernate Transaction behaviour?

故事扮演 提交于 2019-12-12 01:44:16

问题


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 : )


回答1:


If both methods are transactional, then naturally the second one's failing won't affect the first one. If you think both methods are part of a single (correct) transaction, then you're mistaken either about that or about the fact that it seemed to partially commit. You'd need to provide a lot more information to get a good answer as to why one or the other is true.




回答2:


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



来源:https://stackoverflow.com/questions/14845381/normal-spring-hibernate-transaction-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!