Spring事务管理Transaction【转】

Spring提供了许多内置事务管理器实现(原文链接:https://www.cnblogs.com/qiqiweige/p/5000086.html):

  • DataSourceTransactionManager位于org.springframework.jdbc.datasource包中,数据源事务管理器,提供对单个javax.sql.DataSource事务管理,用于Spring JDBC抽象框架、iBATIS或MyBatis框架的事务管理;
  • JdoTransactionManager位于org.springframework.orm.jdo包中,提供对单个javax.jdo.PersistenceManagerFactory事务管理,用于集成JDO框架时的事务管理;
  • JpaTransactionManager位于org.springframework.orm.jpa包中,提供对单个javax.persistence.EntityManagerFactory事务支持,用于集成JPA实现框架时的事务管理;
  • HibernateTransactionManager位于org.springframework.orm.hibernate3包中,提供对单个org.hibernate.SessionFactory事务支持,用于集成Hibernate框架时的事务管理;该事务管理器只支持Hibernate3+版本,且Spring3.0+版本只支持Hibernate 3.2+版本;
  • JtaTransactionManager位于org.springframework.transaction.jta包中,提供对分布式事务管理的支持,并将事务管理委托给Java EE应用服务器事务管理器;
  • OC4JjtaTransactionManager位于org.springframework.transaction.jta包中,Spring提供的对OC4J10.1.3+应用服务器事务管理器的适配器,此适配器用于对应用服务器提供的高级事务的支持;
  • WebSphereUowTransactionManager位于org.springframework.transaction.jta包中,Spring提供的对WebSphere 6.0+应用服务器事务管理器的适配器,此适配器用于对应用服务器提供的高级事务的支持;
  • WebLogicJtaTransactionManager位于org.springframework.transaction.jta包中,Spring提供的对WebLogic 8.1+应用服务器事务管理器的适配器,此适配器用于对应用服务器提供的高级事务的支持。

DataSourceTransactionManager为例管理iBATIS或MyBatis框架的事务

在applicationContext.xml中配置:

<!-- 事务管理器 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置事务特性 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"
                rollback-for="Exception" />
            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"
                rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"
                rollback-for="Exception" />
            <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"
                rollback-for="Exception" />
            <tx:method name="create*" propagation="REQUIRED" isolation="DEFAULT"
                rollback-for="Exception" />
            <tx:method name="getExpressBuss" propagation="REQUIRED"
                isolation="DEFAULT" rollback-for="Exception" />
            <tx:method name="getInvoiceInfo" propagation="REQUIRED"
                isolation="DEFAULT" rollback-for="Exception" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置哪些类的方法需要进行事务管理 -->
    <aop:config>
        <aop:pointcut id="allServiceMethod"
            expression="execution(* com.qiqi.web.service..*.*(..)) || execution(* test.service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" />
    </aop:config>

可以根据不同的模块配置不同的事务传播特性

原文地址:https://www.cnblogs.com/xdouby/p/8818201.html