Spring事务管理之几种方式实现事务

  1. 编程式事务管理
    对基于 POJO 的应用来说是唯一选择。我们需要在代码中调用beginTransaction()、commit()、rollback()等事务管理相关的方法,这就是编程式事务管理。

  2. 基于 TransactionProxyFactoryBean的声明式事务管理
    <!-- 事务管理器 -->
        <bean id="myTracnsactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean> 
        <!-- 事务代理工厂 -->
        <!-- 生成事务代理对象 -->
        <bean id="serviceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="myTracnsactionManager"></property>
            <property name="target" ref="buyStockService"></property>
            <property name="transactionAttributes">
                <props>
                    <!-- 主要 key 是方法   
                        ISOLATION_DEFAULT  事务的隔离级别
                        PROPAGATION_REQUIRED  传播行为
                    -->
                    <prop key="add*">ISOLATION_DEFAULT,PROPAGATION_REQUIRED</prop>
                    <!-- -Exception 表示发生指定异常回滚,+Exception 表示发生指定异常提交 -->
                    <prop key="buyStock">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-BuyStockException</prop>
                </props>
            </property>
            
        </bean>

  3. 基于 @Transactional 的声明式事务管理

    在需要事务管理的方法上添加:( ISOLATION_DEFAULT  事务的隔离级别
          PROPAGATION_REQUIRED  传播行为)
    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,rollbackFor=BuyStockException.class)
    applicationContenxt.xml添加如下:
    <!-- 事务管理器 -->
        <bean id="myTracnsactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 启用事务注解 -->
        <tx:annotation-driven transaction-manager="myTracnsactionManager"/>
    ps:@Transactional注解的方式配置文件要简单的多,将事务交给事务注解驱动。它有个缺陷是他会把所有的连接点都作为切点将事务织入进去,显然只需要在buyStock()方法织入事务即可。下面看看最后一种实现,它就可以精准的织入到指定的连接点

  4. 基于Aspectj AOP配置事务

     

    <!-- 事务管理器 -->
        <bean id="myTracnsactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <tx:advice id="txAdvice" transaction-manager="myTracnsactionManager">
            <tx:attributes>
                <!-- 为连接点指定事务属性 -->
                <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
                <tx:method name="buyStock" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BuyStockException"/>
            </tx:attributes>
        </tx:advice>
        
        <aop:config>
            <!-- 切入点配置 -->
            <aop:pointcut expression="execution(* *..service.*.*(..))" id="point"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>

    </aop:config>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值