Spring 事务


01. 编程式事务控制相关的对象

  >> PlatformTransactionManager

    * PlatformTransactionManager 是个接口; 是Spring的事务管理器;

    * 定义事务的行为,针对不同的持久层技术,提供不同的实现;

      > 如 Dao层使用 jdbc 或 mybatis 时,  org.springframework.jdbc.datasource.DataSourceTransactionManager

      > 如 Dao层使用 hibernate 时, org.springframework.orm.hibernate5.HibernateTransactionManager

    * 常用的事务方法

       

  >> TransactionDefinition 

    * 事务的定义信息对象

    * 常用方法

      > 事务隔离级别:   

      > 事务传播行为

  >> TransactionStatus

    * 提供事务具体的运行状态

    * 常用方法如下

 02. 声明式事务

  >> 指在配置文件中进行配置,实现事务控制;

  >> Spring 声明式事务控制,底层就是AOP  

  >> XML配置声明式事务的实现  >  applicationContext.xml 中的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xsi:schemaLocation="
 7        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 9        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
10 ">
11 
12     
13     class="com.alibaba.druid.pool.DruidDataSource">
14         
15         
16         
17         
18     
19     
20     class="org.springframework.jdbc.core.JdbcTemplate">
21         
22     
23     
24     
25         
26     
27 
28     
29     
30         
31     
32 
33     
34     
35     
36     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
37         
38     
39 
40     
41     
42         
43         
44             
45             
46             
47             
48             
49             
50             
51         
52     
53 
54     
55     
56         
57         
58     
59 
60 

  >> 注解方式的实现

    > 上面代码中,红色部分,均可通过注解的方式来配置

    > 当Bean对象,使用注解时,需要在xml配置文件中,添加上组件扫描配置

              

    > 当事务增强配置使用注解时,需要在XML配置文件中,添加上事务的注解驱动