Transaction doesn't seem to work in Spring Declarative Transaction Mode
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 T开发者_开发技巧est1 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.
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 />
精彩评论