spring事务的理解与测试

一、对事务的说明和配置
经常使用到spring事务的会知道,一般处理到spring的事务,有三种处理方式:
1、编程式事务
较繁琐,适用于处理复杂的业务场景
业务层使用事务模板对象TransactionTemplate完成将多个操作制成同一事务,使用execute方法完成。

//事务模板声明
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
    this.transactionTemplate = transactionTemplate;
}
//该方法内使用到事务
public void transfer(final String out,final  String in,final  Double money) {
    TransactionCallback tc = new TransactionCallbackWithoutResult() {
        protected void doInTransactionWithoutResult(TransactionStatus arg0) {
            //在当前方法中运行的所有操作处于同一个事务
            accountDao.outMoney(out, money);
            accountDao.inMoney(in, money);
        }
    };
    //事务管理操作
    transactionTemplate.execute(tc);
    }

事务的配置:事务管理模板注册到业务bean中,事务管理对象注册到模板对象中。

<!-- 业务层Bean注入事务管理模板  -->
<bean id="accountService" class="cn.itast.tx.account.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="transactionTemplate" ref="transactionTemplate"/>
    </bean>
<!-- 声明一个事务管理模板Bean,该Bean依赖与事务管理器对象 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"/>
</bean>
<!-- 声明事务管理器  -->
<bean id="transactionManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
2、声明式事务
编程式事务其实就是spring能够统一管理事务的原理。
将编程式事务中的通用代码抽取出来,制作成around通知,使用aop原理,将事务管理的代码动态织入到原始方法中。由于该功能使用量较大,sprng已经将该通知制作完毕。
<!--定义Spring提供的事务管理通知-->
<!-- 事务管理Advice,由Spring提供 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="transfer"/>
    </tx:attributes>
</tx:advice>
注意:TxAdvice需要为其指定一个事务管理器的Bean
<!-- 声明事务管理器的Bean,该Bean依赖数据源对象 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

设置aop:
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(void *..*.*.trans*(..))"/>
        </aop:config>

这种要比编程式事务简单许多,但底层还是由编程式事务实现,对业务层的事务配置灵活了许多。

3、事务注解
其实它也是声明式事务的一种,也是最简单的实现事务的方式。

<!-- 在配置文件中,开启注解式事务驱动,需要为其指定事务管理器  -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 定义事务管理器,根据使用的具体实现方式设置 -->
<bean id=" txManager " class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
</bean>

之后只需要在实现事务的接口或方法上添加注解 @Transactional.

二、对事务的测试

 /**
     * 不加事务时,如果捕获了异常(但不报程序错误)则捕捉异常,try内的异常之后不再执行;try外的继续执行
     * 加了事务之后
     *     1、错误出现在try中,异常被捕获,程序继续运行不会报错;try外的继续执行不会回退;try内的异常前的回退,异常后的不执行(如代码所示,当20注释掉导致异常,运行后,23、19、21会被正常执行,22不执行)
     *        -- 此时相当于事务不起作用
     *     2、try外任一处出现异常,try内、外全部回退,程序报错
     *
     */
    @Transactional()
    public void testAdd2(){
        ProductExchangeInfoDTO productExchangeInfo = new ProductExchangeInfoDTO();
        //把id的触发器屏蔽掉,这样不设置id就会导致sql报错。测试如下:

        productExchangeInfo = new ProductExchangeInfoDTO();
        productExchangeInfo.setId((long) 23);
        productExchangeInfo.setProductName("31123");
        productExchangeInfoDAO.insertOneProduct(productExchangeInfo);
        try{
            productExchangeInfo.setId((long)19);
            productExchangeInfo.setProductName("31117");
            productExchangeInfoDAO.insertOneProduct(productExchangeInfo);

//            productExchangeInfo.setId((long) 20);
            productExchangeInfo.setProductName("31118");
            productExchangeInfoDAO.insertOneProduct(productExchangeInfo);

            productExchangeInfo = new ProductExchangeInfoDTO();
            productExchangeInfo.setId((long) 22);
            productExchangeInfo.setProductName("31120");
            productExchangeInfoDAO.insertOneProduct(productExchangeInfo);
        }catch(Exception e){
            e.printStackTrace();
        }


        productExchangeInfo = new ProductExchangeInfoDTO();
        productExchangeInfo.setId((long)21);
        productExchangeInfo.setProductName("31119");
        productExchangeInfoDAO.insertOneProduct(productExchangeInfo);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值