spring事务使用的一点总结

spring在处理事务的时候有两种方式:声明式事务和编程式事务,下面就这两种分开来阐述;

1、声明式事务
声明式事务也有两种实现方式:
a、通过TranscationProxyFactoryBean设置spring事务代理,配置方式如下:
   
               
               
       

      
               
               
                       
                                PROPAGATION_NESTED
                       

               

       

        业务代码配置需要继承于公用代理类:
       
               
                       
                               
                       

               

       

        程序代码中不在需要涉及相关事务代码
    b、通过BeanNameAutoProxyCreator进行自动事务代理,配置方式如下:
       
          
               
               
                       
                                PROPAGATION_NESTED
                       

               

       

       
       
               
                       
                                *
                       

               

               
                       
               

       

       
       
               
               
                       
                                *Service
                       

               

               
                       
                                transcationMethodPointcutAdvisor
                       

               

       

         程序代码中不在需要涉及相关事务代码
2、编程式事务
编程式事务有两种实现方式:
a、一种是通过注入TransactionTemplate,这种方式需要在配置文件中添加如下代码:
       
               
               
       

          
               
       

    dataSource是数据源根据框架要求可以使用JNDI,也可以配置特定数据源
    程序中需要将transactionTemplate注入,例如:
        private TransactionTemplate transactionTemplate;
        public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
                this.transactionTemplate = transactionTemplate;
                //设置事务传播方式
                this.transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
        }
    程序中调用需要通过回调方式来实现,例如:
       return (Map)this.transactionTemplate.execute(new TransactionCallback(){
                public Object doInTransaction(final TransactionStatus transactionStatus){
                //your self coding
        }
    b、另一中是通过注入PlatformTransactionManager,这种方式需要在配置文件中添加如下代码:
       
               
               
       

     dataSource是数据源根据框架要求可以使用JNDI,也可以配置特定数据源
     程序中需要将transactionManager注入,例如:
        private TransactionStatus transactionStatus;
        private DefaultTransactionDefinition defaultTransactionDefinition;
       
        protected PlatformTransactionManager transactionManager;
        public void setTransactionManager(PlatformTransactionManager transactionManager) {
                this.transactionManager = transactionManager;
                defaultTransactionDefinition=new DefaultTransactionDefinition();
                //设置事务传播方式
                defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
        }
     调用方式比较简单,
        TransactionStatus transactionStatus = transactionManager.getTranscationDefinition(def);
        try{
                //execute code
        }catch(Exception ex){
                transactionManager.rollback(transactionStatus);
                throws ex;
        }
        transactionManager.commit(transactionStatus);

你可能感兴趣的:(spring事务使用的一点总结)