1. 程式人生 > >Spring事務管理Transaction【轉】

Spring事務管理Transaction【轉】

集成 class advice nag batis spring3 ont weblogic tex

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>

可以根據不同的模塊配置不同的事務傳播特性

Spring事務管理Transaction【轉】