<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <aop:config> <aop:pointcut id="productServiceMethods" expression="execution(* product.ProductService.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="myTxManager"> <tx:attributes> <tx:method name="increasePrice*" propagation="REQUIRED"/> <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/> <tx:method name="*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice>
Spring在TransactionDefinition接口中规定了7种类型的事务传播行为。 spring
它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播。 express
事务传播行为类型 | 说明 |
PROPAGATION_REQUIRED | 若是当前没有事务,就新建一个事务,若是已经存在一个事务中,加入到这个事务中。这是最多见的选择。 |
PROPAGATION_SUPPORTS | 支持当前事务,若是当前没有事务,就以非事务方式执行。 |
PROPAGATION_MANDATORY | 使用当前的事务,若是当前没有事务,就抛出异常。 |
PROPAGATION_REQUIRES_NEW | 新建事务,若是当前存在事务,把当前事务挂起。 |
PROPAGATION_NOT_SUPPORTED | 以非事务方式执行操做,若是当前存在事务,就把当前事务挂起。 |
PROPAGATION_NEVER | 以非事务方式执行,若是当前存在事务,则抛出异常。 |
PROPAGATION_NESTED | 若是当前存在事务,则在嵌套事务内执行。若是当前没有事务,则执行与PROPAGATION_REQUIRED类 似的操做。 |