Spring框架

8.8.2设置

  • 注解

@Transaction注解

 

  • XML

在Spring 2.x事务通知中,超时和只读属性可以在<tx:method>元素中进行指定

 

8.9 基于XML文档的声明式事务配置

         <!-- 配置事务切面 -->

         <aop:config>

                   <aop:pointcut

                            expression="execution(* com.atguigu.tx.component.service.BookShopServiceImpl.purchase(..))"

                            id="txPointCut"/>

                   <!-- 将切入点表达式和事务属性配置关联到一起 -->

                   <aop:advisor advice-ref="myTx" pointcut-ref="txPointCut"/>

         </aop:config>

 

         <!-- 配置基于XML的声明式事务  -->

         <tx:advice id="myTx" transaction-manager="transactionManager">

                   <tx:attributes>

                            <!-- 设置具体方法的事务属性 -->

                            <tx:method name="find*" read-only="true"/>

                            <tx:method name="get*" read-only="true"/>

                            <tx:method name="purchase"

                                     isolation="READ_COMMITTED"

         no-rollback-for="java.lang.ArithmeticException,java.lang.NullPointerException"

                                     propagation="REQUIRES_NEW"

                                     read-only="false"

                                     timeout="10"/>

                   </tx:attributes>

         </tx:advice>