spring学习(八)事务操作

一、事务的概念:

  事务是并发控制的单位,一系列操作组成的工作单元,该工作单元内的操作是不可分割的,也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一个都不做,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消。 
  事务的结束有两种,当事务中的所以步骤全部成功执行时,事务提交。如果其中一个步骤失败,将发生回滚操作,撤消撤消之前到事务开始时的所以操作。

1、事务的特性与属性

⑴ 原子性(Atomicity)

  原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚,这和前面两篇博客介绍事务的功能是一样的概念,因此事务的操作如果成功就必须要完全应用到数据库,如果操作失败则不能对数据库有任何影响。

⑵ 一致性(Consistency)

  一致性是指事务必须使数据库从一个一致性状态变换到另一个一致性状态,也就是说一个事务执行之前和执行之后都必须处于一致性状态。

  拿转账来说,假设用户A和用户B两者的钱加起来一共是5000,那么不管A和B之间如何转账,转几次账,事务结束后两个用户的钱相加起来应该还得是5000,这就是事务的一致性。

⑶ 隔离性(Isolation)

  隔离性是当多个用户并发访问数据库时,比如操作同一张表时,数据库为每一个用户开启的事务,不能被其他事务的操作所干扰,多个并发事务之间要相互隔离

  即要达到这么一种效果:对于任意两个并发的事务T1和T2,在事务T1看来,T2要么在T1开始之前就已经结束,要么在T1结束之后才开始,这样每个事务都感觉不到有其他事务在并发地执行。

  关于事务的隔离性数据库提供了多种隔离级别,稍后会介绍到。

⑷ 持久性(Durability)

  持久性是指一个事务一旦被提交了,那么对数据库中的数据的改变就是永久性的,即便是在数据库系统遇到故障的情况下也不会丢失提交事务的操作。

2、事务的并发性问题

(1)、脏读:一个事务看到了另一个事务未提交的更新数据;当事务读取尚未提交的数据时,就会发生这种情况。

  例如事务A正在访问数据,并且对数据进行了修改,更改了一行数据,这种修改还没有提交到数据库中,而事务B在事务A提交更新之前读取了已更新的行,然后使用了这个数据。 
如果事务A回滚该更新,则事务B使用的这个数据由于还没有提交,就会被认为是不曾存在的数据,即为脏数据,根据脏数据所做的操作可能是不正确的。

(2)、 不可重复读:在同一事务中,两次读取同一数据,得到内容不同,也就是有其他事务更改了这些数据;

  例如事务A查询一条记录后,事务B更新了事务A查询的记录,并提交了事务,那事务A再次查询上次的记录时,对同一数据查询了两次,得到的结果不同,这称为不可重复读。

(3)、幻读:一个事务在执行过程中读取到了另一个事务已提交的插入数据;

  即在第一个事务开始时读取到一批数据,但此后另一个事务又插入了新数据并提交,此时第一个事务又读取这批数据但发现多了一条,即好像发生幻觉一样。

3、事务的隔离级别

现在来看看MySQL数据库为我们提供的四种隔离级别:

  ① Serializable (串行化):可避免脏读、不可重复读、幻读的发生。

  ② Repeatable read (可重复读):可避免脏读、不可重复读的发生。

  ③ Read committed (读已提交):可避免脏读的发生。

  ④ Read uncommitted (读未提交):最低级别,任何情况都无法保证。

 4、事务的传播行为

  决定业务方法之间调用,事务应该如何处理

  事务传播行为用来描述由某一个事务传播行为修饰的方法被嵌套进另一个方法的时事务如何传播。

事务传播行为类型 说明
PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。

5、是否只读

  1、true

  2、false

二、spring封装了事务管理的代码

  1、打开事务

  2、提交事务

  3、回滚事务

  由于不同的平台操作事务的代码各不相同(JDBC、Mybatis、Hibernate等),所以spring提供了一个接口platformTransactionManager,不同平台操作有其对应的实现类,例如:JDBCTransactionManager、HibernateTransactionManager

  在spring中最核心的操作事务的对象TransactionManager

 三、spring中操作数据库的事务实例:

 导包:(导入maven依赖)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>demojdbc</groupId>
    <artifactId>demojdbc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

    </dependencies>
</project>

(1)、书写与数据库对应的bean类

 1 package dyh.bean;
 2 
 3 /**
 4  * Created by Duyahui on 2018/12/16.
 5  */
 6 public class Money {
 7     private String name;
 8     private Integer money;
 9 
10     public String getName() {
11         return name;
12     }
13 
14     public void setName(String name) {
15         this.name = name;
16     }
17 
18     public Integer getMoney() {
19         return money;
20     }
21 
22     public void setMoney(Integer money) {
23         this.money = money;
24     }
25 
26     @Override
27     public String toString() {
28         return "Money{" +
29                 "name='" + name + '\'' +
30                 ", money=" + money +
31                 '}';
32     }
33 }

(2)、书写持久层接口以及其实现类

public interface MoneyDao {
    //加钱
    void increaseMoney(Integer id,Double money);
    //减钱
    void decreaseMoney(Integer id,Double money);
}
public class MoneyDaoImpl extends JdbcDaoSupport implements MoneyDao {
    public void increaseMoney(Integer id, Double money) {

        String sql = "update t_money set money = money + ? where id = ? ";
        super.getJdbcTemplate().update(sql,money,id);
    }

    public void decreaseMoney(Integer id, Double money) {
        String sql = "update t_money set money = money - ? where id = ? ";
        super.getJdbcTemplate().update(sql,money,id);
    }
}

(3)、书写业务层代码以及其实现类

 public interface MoneyService { //转账方法 void transfer(Integer from,Integer to,Double money); } 

public class MoneyServiceImpl implements MoneyService {
    private MoneyDao moneyDao;
    private TransactionTemplate tt;

    public void setMoneyDao(MoneyDao moneyDao) {
        this.moneyDao = moneyDao;
    }

    public void setTt(TransactionTemplate tt) {
        this.tt = tt;
    }


    public void transfer(final Integer from,final Integer to,final Double money) {

        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                //减钱
                moneyDao.decreaseMoney(from, money);
                int i = 1/0;
                //加钱
                moneyDao.increaseMoney(to, money);
            }
        });
    }
}

(4)、配置文件的书写(IDEA的maven工程,配置文件必须放在resources中)

  jdbc.properties

 jdbc.jdbcUrl=jdbc:mysql:///school jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=dyhroot 

  applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

    <!-- 指定spring读取db.properties配置 -->
    <context:property-placeholder location="classpath:jdbc.properties"  />

    <!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource" ></property>
    </bean>
    <!-- 事务模板对象 -->
    <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
        <property name="transactionManager" ref="transactionManager" ></property>
    </bean>

    <!-- 配置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager" >
        <tx:attributes>
            <!-- 以方法为单位,指定方法应用什么事务属性
                isolation:隔离级别
                propagation:传播行为
                read-only:是否只读
             -->
            <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
            <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置织入 -->
    <aop:config  >
        <!-- 配置切点表达式 -->
        <aop:pointcut expression="execution(* dyh.service.*ServiceImpl.*(..))" id="txPc"/>
        <!-- 配置切面 : 通知+切点
                 advice-ref:通知的名称
                 pointcut-ref:切点的名称
         -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
    </aop:config>
    
    <!-- 1.将连接池 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
        <property name="driverClass" value="${jdbc.driverClass}" ></property>
        <property name="user" value="${jdbc.user}" ></property>
        <property name="password" value="${jdbc.password}" ></property>
    </bean>
    
    <!-- 2.Dao-->
    <bean name="moneyDao" class="dyh.dao.MoneyDaoImpl" >
        <property name="dataSource" ref="dataSource" ></property>
    </bean>

    <!-- 3.Service-->
    <bean name="accountService" class="dyh.service.MoneyServiceImpl" >
        <property name="moneyDao" ref="moneyDao" ></property>
        <property name="tt" ref="transactionTemplate" ></property>
    </bean>

</beans>

(5)、测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo {
    @Resource(name="accountService")
    private MoneyService as;

    @Test
    public void fun1(){

        as.transfer(1, 2, 100d);

    }
}

测试结果:

出现异常:

数据库中的数据没有改变,如果在业务层去除

 int i = 1/0;

数据库中结果改变




利用注解的方式实现事务的操作(根据上面代码,两个地方需要改变:1、配置文件。2、业务层代码):
(1)、配置文件修改
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

    <!-- 指定spring读取db.properties配置 -->
    <context:property-placeholder location="classpath:jdbc.properties"  />

    <!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource" ></property>
    </bean>
    <!-- 事务模板对象 -->
    <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
        <property name="transactionManager" ref="transactionManager" ></property>
    </bean>

    <!-- 开启使用注解管理aop事务 -->
    <tx:annotation-driven/>

    <!-- 1.将连接池 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
        <property name="driverClass" value="${jdbc.driverClass}" ></property>
        <property name="user" value="${jdbc.user}" ></property>
        <property name="password" value="${jdbc.password}" ></property>
    </bean>

    <!-- 2.Dao-->
    <bean name="moneyDao" class="dyh.dao.MoneyDaoImpl" >
        <property name="dataSource" ref="dataSource" ></property>
    </bean>

    <!-- 3.Service-->
    <bean name="accountService" class="dyh.service.MoneyServiceImpl" >
        <property name="moneyDao" ref="moneyDao" ></property>
        <property name="tt" ref="transactionTemplate" ></property>
    </bean>

</beans>

(2)、业务层代码修改(@Transaction   该注解一般放在业务层类上面,进行这个类的操作)

public class MoneyServiceImpl implements MoneyService {
    private MoneyDao moneyDao;
    private TransactionTemplate tt;

    public void setMoneyDao(MoneyDao moneyDao) {
        this.moneyDao = moneyDao;
    }

    public void setTt(TransactionTemplate tt) {
        this.tt = tt;
    }

    @Transactional
    public void transfer(Integer from, Integer to, Double money) {
//减钱
        moneyDao.decreaseMoney(from, money);
        int i = 1/0;
        //加钱
        moneyDao.increaseMoney(to, money);
    }
}

猜你喜欢

转载自www.cnblogs.com/soft2018/p/10127304.html
今日推荐