Spring_声明式事务
  1.导入jar包
 
 - IOC容器必须的jar包
        commons-logging-
    1.1
    .3.
    jar
   
   
        
    spring
    -beans-
    4.0
    .0.RELEASE.
    jar
   
   
        
    spring
    -context-
    4.0
    .0.RELEASE.
    jar
   
   
        
    spring
    -core-
    4.0
    .0.RELEASE.
    jar
   
   
        
    spring
    -expression-
    4.0
    .0.RELEASE.
    jar
   
  - 使用注解的方式配置bean时需要导入的jar包
       spring-aop-
   4.0
   .0.RELEASE.
   jar
  
 - 连接数据库时需导入的jar包
        c3p0-0.9
    .1
    .2.
    jar
   
   
        mysql-connector-java-5.1
    .37
    -bin.
    jar
   
  - 使用JdbcTemplate时需要导入的jar包
        spring-jdbc-
    4.0
    .0.
    RELEASE
    .
    jar
   
   
        
    spring
    -orm-
    4.0
    .0.RELEASE.
    jar
   
   
        
    spring
    -tx-
    4.0
    .0.RELEASE.
    jar
   
  
  2.使用Spring的声明式事务
 
 - 1)配置事务管理器
    <!-- 配置事务管理器 -->
   
   
        
    <
    bean
    id
    =
    "transactionManager"
    class
    =
    "org.springframework.jdbc.datasource.DataSourceTransactionManager"
    >
   
   
             
    <!-- 设置数据源 -->
   
   
             
    <
    property
    name
    =
    "dataSource"
    ref
    =
    "dataSource"
    ></
    property
    >
   
   
        
    </
    bean
    >
   
  - 2)启用使用注解的事务支持
    <!-- 启用事务的注解支持 -->
   
   
        
    <!-- 如果事务管理器的id属性值是transactionManager,transaction-manager属性就可以省略 -->
   
   
        
    <
    tx:annotation-driven
    />
   
  - 3)在要添加事务的方法上添加@Transactional注解
    - 关于@Transactional注解
      - 可以添加到方法上
- 也可以添加到类上
        - 类中所有的方法添加了事务
 
 
 
- 关于@Transactional注解
      
        @Transactional
   
   
        
    @Override
   
   
        
    public
    void
    purchase
    (
    int
    userId
    , String
    isbn
    ) {
   
   
             
    //1.获取图书的价格
   
   
             
    double
    bookPrice
    =
    bookShopDao
    .getBookPriceByIsbn(
    isbn
    );
   
   
             
    //2.更新图书的库存
   
   
             
    bookShopDao
    .updateBookStock(
    isbn
    );
   
   
             
    //3.更新用户账户的余额
   
   
             
    bookShopDao
    .updateUserAccount(
    userId
    ,
    bookPrice
    );
   
   
        }
   
  
  3.@Transactional中的一些属性
 
 -  事务的传播行为
 - 一个方法运行在一个开启了事务的方法中时,该方法是使用原来的事务还是开启一个新的事务
 
- 事务的隔离级别
    - Mysql默认的隔离级别:可重复读
- Oracle默认的隔离级别:读已提交(通常我们就设置该值)
 
- 设置那些异常回滚或者不回滚
- 设置事务超时的时间
- 设置只读属性
    /**
   
   
         * propagation属性:用来设置事务的传播行为
   
   
         *       
    -
    Propagation.REQUIRED:默认,使用调用者的事务
   
   
         *       
    -
    Propagation.REQUIRES_NEW:开启一个新事务
   
   
         *
   
   
         * isolation属性:用来设置事务的隔离级别
   
   
         *       
    -
    Isolation.REPEATABLE_READ:可重复读,
    Mysql
    默认的隔离级别
   
   
         *       
    -
    Isolation.READ_COMMITTED:读已提交,Oracle默认的隔离级别,也是常有的隔离级别
   
   
         *
   
   
         * rollbackFor属性:用来设置出现什么异常时才回滚,值是一个数组,里面放的是异常的类型
   
   
         * rollbackForClassName属性:用来设置出现什么异常时才回滚,值是一个数组,里面放的是异常的名字
   
   
         * noRollbackFor属性:用来设置出现什么异常不回滚,值是一个数组,里面放的是异常的类型
   
   
         * norollbackForClassName属性:用来设置出现什么异常时不回滚,值是一个数组,里面放的是异常的名字
   
   
         *
   
   
         * timeout属性:用来设置超时的时间,单位是秒
   
   
         *
   
   
         * readOnly属性:用来设置当前操作是一个只读的操作,通常对数据库进行查询操作时设置该属性为true
   
   
         */
   
   
        
    @Transactional
    (propagation=Propagation.
    REQUIRES_NEW
    , isolation=Isolation.
    READ_COMMITTED
    ,
   
   
                 noRollbackFor={ArithmeticException.
    class
    },timeout=3,readOnly=
    false
    )
   
   
        
    @Override
   
   
        
    public
    void
    purchase(
    int
    userId
    , String
    isbn
    ) {
   
   
             
    //1.获取图书的价格
   
   
             
    double
    bookPrice
    =
    bookShopDao
    .getBookPriceByIsbn(
    isbn
    );
   
   
             
    //2.更新图书的库存
   
   
             
    bookShopDao
    .updateBookStock(
    isbn
    );
   
   
             
    //3.更新用户账户的余额
   
   
             
    bookShopDao
    .updateUserAccount(
    userId
    ,
    bookPrice
    );
   
   
        }
   
  
  4.通过XML的形式配置事务
 
 
   <?
   xml 
   version
   =
   "1.0"
   encoding
   =
   "UTF-8"
   ?>
  
  
   <
   beans
   xmlns
   =
   "http://www.springframework.org/schema/beans";
  
  
       
   xmlns:xsi
   =
   "http://www.w3.org/2001/XMLSchema-instance";
  
  
       
   xmlns:context
   =
   "http://www.springframework.org/schema/context";
  
  
       
   xmlns:tx
   =
   "http://www.springframework.org/schema/tx";
  
  
       
   xmlns:aop
   =
   "http://www.springframework.org/schema/aop";
  
  
       
   xsi:schemaLocation
   =
   "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
  
  
   http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
  
  
   http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  
  
   http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd";
   >
  
  
  
       
   <!-- 引入外部的属性文件 -->
  
  
       
   <
   context:property-placeholder
   location
   =
   "classpath:db.properties"
   />
  
  
       
   <!-- 配置数据源 -->
  
  
       
   <
   bean
   id
   =
   "dataSource"
   class
   =
   "com.mchange.v2.c3p0.ComboPooledDataSource"
   >
  
  
            
   <
   property
   name
   =
   "user"
   value
   =
   "${jdbc.user}"
   ></
   property
   >
  
  
            
   <
   property
   name
   =
   "password"
   value
   =
   "${jdbc.password}"
   ></
   property
   >
  
  
            
   <
   property
   name
   =
   "jdbcUrl"
   value
   =
   "${jdbc.url}"
   ></
   property
   >
  
  
            
   <
   property
   name
   =
   "driverClass"
   value
   =
   "${jdbc.driverClass}"
   ></
   property
   >
  
  
            
   <
   property
   name
   =
   "minPoolSize"
   value
   =
   "${jdbc.minPoolSize}"
   ></
   property
   >
  
  
            
   <
   property
   name
   =
   "maxPoolSize"
   value
   =
   "${jdbc.maxPoolSize}"
   ></
   property
   >
  
  
       
   </
   bean
   >
  
  
  
       
   <!-- 配置 JdbcTemplate -->
  
  
       
   <
   bean
   id
   =
   "jdbcTemplate"
   class
   =
   "org.springframework.jdbc.core.JdbcTemplate"
   >
  
  
            
   <
   property
   name
   =
   "dataSource"
   ref
   =
   "dataSource"
   ></
   property
   >
  
  
       
   </
   bean
   >
  
  
  
       
   <!-- 配置BookShopDaoImpl -->
  
  
       
   <
   bean
   id
   =
   "bookShopDao"
   class
   =
   "it.test.spring.transaction.xml.dao.BookShopDaoImpl"
   >
  
  
            
   <
   property
   name
   =
   "jdbcTemplate"
   ref
   =
   "jdbcTemplate"
   ></
   property
   >
  
  
       
   </
   bean
   >
  
  
       
   <!-- 配置BookServiceImpl -->
  
  
       
   <
   bean
   id
   =
   "bookShopService"
   class
   =
   "it.test.spring.transaction.xml.service.BookShopServiceImpl"
   >
  
  
            
   <
   property
   name
   =
   "bookShopDao"
   ref
   =
   "bookShopDao"
   ></
   property
   >
  
  
       
   </
   bean
   >
  
  
       
   <!-- 配置事务管理器 -->
  
  
       
   <
   bean
   id
   =
   "transactionManager"
   class
   =
   "org.springframework.jdbc.datasource.DataSourceTransactionManager"
   >
  
  
            
   <
   property
   name
   =
   "dataSource"
   ref
   =
   "dataSource"
   ></
   property
   >
  
  
       
   </
   bean
   >
  
  
       
   <!-- 配置事务 -->
  
  
       
   <
   tx:advice
   id
   =
   "txAdvice"
   >
  
  
            
   <!-- 配置添加事务的方法 -->
  
  
            
   <
   tx:attributes
   >
  
  
                
   <
   tx:method
   name
   =
   "purchase"
   propagation
   =
   "REQUIRED"
   />
  
  
                
   <!-- 给查询的方法添加事务 -->
  
  
                
   <
   tx:method
   name
   =
   "get*"
   read-only
   =
   "true"
   />
  
  
                
   <!-- 给所有的方法添加事务 -->
  
  
                
   <
   tx:method
   name
   =
   "*"
   />
  
  
            
   </
   tx:attributes
   >
  
  
       
   </
   tx:advice
   >
  
  
       
   <!-- 配置中AOP -->
  
  
       
   <
   aop:config
   >
  
  
            
   <!-- 配置切入点表达式 -->
  
  
            
   <
   aop:pointcut
   expression
   =
   "execution(* it.test.spring.transaction.xml.service.BookShopServiceImpl.purchase(..))"
  
  
                    
   id
   =
   "pointCut"
   />
  
  
            
   <!-- 将添加的方法与切入点表达式关联 起来 -->
  
  
            
   <
   aop:advisor
   advice-ref
   =
   "txAdvice"
   pointcut-ref
   =
   "pointCut"
   />
  
  
       
   </
   aop:config
   >
  
  
   </
   beans
   >
  
 本文来自博客园,作者:diligently,转载请注明原文链接:https://www.cnblogs.com/luo12828-foxmail/p/16964234.html
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号