Spring事务控制

1.事务介绍

1.1什么是事务?

当你需要一次执行多条SQL语句时,可以使用事务。通俗一点说,如果这几条SQL语句全部执行成功,则才对数据库进行一次更新,如果有一条SQL语句执行失败,则这几条SQL语句全部不进行执行,这个时候需要用到事务。

回顾一下数据库事务的四大特性ACID

原子性:(Atomicity)
       要么都执行,要么都不执行

一致性:(Consistency)
       事务前后的数据都是正确的

隔离性:(Isolation)
      事物之间相互隔离,互不干扰(并发执行的事务彼此无法看到对方的中间状态)

持久性:(Durability)
       事务一旦提交不可再回滚 

 1.2数据库本身控制事物

begin transaction;
      //1.本地数据库操作:张三减少金额
      //2.本地数据库操作:李四增加金额
rollback;
或
commit transation;

 1.3jdbc中使用事物

1.获取对数据库的连接

2.设置事务不自动提交(默认情况是自动提交的)

conn.setAutoCommit(false);   //其中conn是第一步获取的随数据库的连接对象。

  3.把想要一次性提交的几个sql语句用事务进行提交

try{
    Statement stmt = null; 
    stmt =conn.createStatement(); 
    stmt.executeUpdate(sql1); 
    int a=6/0;
    stmt.executeUpdate(Sql2); 
    . 
    . 
    . 
    conn.commit();   //使用commit提交事务 
}

4.捕获异常,进行数据的回滚(回滚一般写在catch块中)

catch(Exception e) { 
   ... 
   conn.rollback(); 
}

2.转账案例

2.1创建工程 

2.2添加转账业务

 2.2.1mapper 

package com.by.mapper;

import com.by.pojo.User;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {
    public void addUser(User user);

    void updateUserOutMoney(@Param("source") String source, @Param("money")float money);

    void updateUserInMoney(@Param("target") String target, @Param("money")float money);
}

xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.by.mapper.UserMapper">
    <insert id="addUser" parameterType="com.by.pojo.User">
        insert into t_user(name,money) values(#{name},#{money})
    </insert>
    
    <update id="updateUserOutMoney">
        update t_user set money=money-#{money} where name=#{source}
    </update>
    <update id="updateUserInMoney" >
        update t_user set money=money+#{money} where name=#{target}
    </update>
</mapper>

2.2.2service

package com.by.service;

import com.by.mapper.UserMapper;
import com.by.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;
  @Override
    public void addUser(User user){
        userMapper.addUser(user);
    }
  @Override
    public void updateUser(String source, String target, Float money) {
 userMapper.updateUserOutMoney(source,money);
userMapper.updateUserInMoney(target,money);
    }
    }

 2.2.3测试

package com.by.web;

import com.by.pojo.User;
import com.by.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)//加载配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {

@Autowired
//    @Resource(name = "proxyUserService")
//@Qualifier("proxyUserService")
    private UserService userService;

@Test
    public void addUser(){
   userService.addUser(new User("小龙女",4000F));
   userService.addUser(new User ("李莫愁",2000F));
}

@Test
public void testUpdateUser(){
    userService.updateUser("张三丰","宋远桥",1F);
}
}

2.2.4结果

1.此时我们观察数据表里面的变化情况:  

转账是成功的,但是涉及到业务的问题,如果业务层实现类有其中一个环节出问题,都会导致灾难。

2.我们先把数据恢复到转账前。

现在我们故意模拟转账业务出现问题

 @Override
    public void updateUser(String source, String target, Float money) {
//转出
            userMapper.updateUserOutMoney(source,money);
 //制造转账异常
            int a=6/0;
            //转入
            userMapper.updateUserInMoney(target,money);
}

 再来测试:

业务执行出错,但是!

这是因为:不满足事务的一致性(减钱的事务提交了,加钱的事务没有提交,甚至都没有执行到)。

3.Spring中事务控制的API介绍

  • 说明:

    • JavaEE体系进行分层开发,事务处理位于业务层,Spring提供了分层设计业务层的事务处理解决方案。

    • Spring框架为我们提供了一组事务控制的接口。具体在后面的小节介绍。这组接口是在spring-tx.RELEASE.jar中。

    • spring的事务控制都是基于AOP的,它既可以使用编程的方式实现,也可以使用配置的方式实现。我们学习的重点是使用配置的方式实现。

3.1PlatformTransactionManager

 此接口是spring的事务管理器,它里面提供了我们常用的操作事务的方法,源代码如下:

public interface PlatformTransactionManager { 
    
  	//开启事务  
    TransactionStatus getTransaction(TransactionDefinition definition) 
        						throws TransactionException; 
    //提交事务
    void commit(TransactionStatus status) throws TransactionException; 
    
  	//回滚事务
    void rollback(TransactionStatus status) throws TransactionException;   
}

 真正管理事务的对象:

Spring为不同的orm框架提供了不同的PlatformTransactionManager接口实现类:

  • DataSourceTransactionManager:使用Spring JDBC或iBatis 进行持久化数据时使用

  • HibernateTransactionManager:使用Hibernate版本进行持久化数据时使用

3.2TransactionDefinition

 TransactionDefinition接口包含与事务属性相关的方法,源代码如下:

public interface TransactionDefinition {
    int PROPAGATION_REQUIRED = 0;
    int PROPAGATION_SUPPORTS = 1;
    int PROPAGATION_MANDATORY = 2;
    int PROPAGATION_REQUIRES_NEW = 3;
    int PROPAGATION_NOT_SUPPORTED = 4;
    int PROPAGATION_NEVER = 5;
    int PROPAGATION_NESTED = 6;
    int ISOLATION_DEFAULT = -1;
    int ISOLATION_READ_UNCOMMITTED = 1;
    int ISOLATION_READ_COMMITTED = 2;
    int ISOLATION_REPEATABLE_READ = 4;
    int ISOLATION_SERIALIZABLE = 8;
    int TIMEOUT_DEFAULT = -1;
    
    //传播行为
    int getPropagationBehavior();
	//隔离级别
    int getIsolationLevel();
   //事务超时
    int getTimeout();
   //是否只读
    boolean isReadOnly();
}

 TransactionDefinition 接口定义的事务规则包括:事务隔离级别、事务传播行为、事务超时、事务的只读、回滚规则属性,同时,Spring 还为我们提供了一个默认的实现类:DefaultTransactionDefinition,该类适用于大多数情况。如果该类不能满足需求,可以通过实现 TransactionDefinition 接口来实现自己的事务定义。

3.2.1事务隔离级别

 事务并发时的安全问题

  • 问题描述隔离级别
    脏读一个事务读取到另一个事务还未提交的数据read-commited
    不可重复读一个事务内多次读取一行数据的内容,其结果不一致repeatable-read
    幻读一个事务内多次读取一张表中的内容,其结果不一致serialized-read
  • Spring事务隔离级别(比数据库事务隔离级别多一个default)由低到高为:
  • 隔离级别
    ISOLATION_DEFAULT这是一个platfromtransactionmanager默认的隔离级别,使用数据库默认的事务隔离级别。
    ISOLATION_READ_UNCOMMITTED这是事务最低的隔离级别,会产生脏读,不可重复读和幻像读。
    ISOLATION_READ_COMMITTED这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。 Oracle数据库默认的隔离级别。
    ISOLATION_REPEATABLE_READ这种事务隔离级别可以防止脏读、不可重复读,但是可能出现幻像读。MySQL数据库默认的隔离级别。
    ISOLATION_SERIALIZABLE这是花费最高代价但是最可靠的事务隔离级别,事务被处理为顺序执行。除了防止脏读、不可重复读外,还避免了幻像读。

    3.2.2事务的传播行为

  • 什么是事务传播行为?

    事务传播行为(propagation behavior)指的就是当一个事务方法被另一个事务方法调用时,这个事务方法应该如何进行。 例如:methodA事务方法调用methodB事务方法时,methodB是继续在调用者methodA的事务中运行呢,还是为自己开启一个新事务运行,这就是由methodB的事务传播行为决定的。

  • Spring定义了七种传播行为:

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

3.2.3事务超时 

  • timeout事务超时时间: 当前事务所需操作的数据被其他事务占用,则等待。

    • 100:自定义等待时间100(秒)。

    • -1:由数据库指定等待时间,默认值。(建议)

3.2.4读写性 

  • readonly 读写性

    • true:只读,可提高查询效率,适合查询

    • false:可读可写,适合增删改

3.2.5回滚规则 

  • TransactionAttribute
  • TransactionAttribute 的默认实现类是DefaultTransactionAttribute ,它同时继承了DefaultTransactionDefinition。在DefaultTransactionDefinition 的基础上增加了rollbackOn的实现,DefaultTransactionAttribute的实现指定了,当异常类型为unchecked exception 的情况下将回滚事务。
  • rollbackOn 回滚规则,可省略或设置 rollbackOn="Exception"

    • 如果事务中抛出 RuntimeException,则自动回滚

    • 如果事务中抛出 CheckException,不会自动回滚

3.3TransactionStatus

 PlatformTransactionManager.getTransaction(…) 方法返回一个 TransactionStatus 对象,该对象代表一个新的或已经存在的事务,源代码如下

public  interface TransactionStatus{
   boolean isNewTransaction();
   void setRollbackOnly();
   boolean isRollbackOnly();
}

 4.改造转账案例

4.1applicationContext.xml

<!--    事务管理器DataSourceTransactionManager:负责开启、提交、回滚事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
     </bean>
<--  事务属性:DefaultTransactionDefinition:配置事务的属性-->
    <bean id="transactionDefinition" class="org.springframework.transaction.support.DefaultTransactionDefinition">
<!--     isolationLevel:控制事务的隔离性,有默认值,默认值为DEFAULT
         propagationBehavior:事务的传播行为,有默认值,默认值为REQUIRED
         readOnly:只读性,有默认值,默认值为false
         timeout:事务超时,有默认值,默认永不超时
           -->
       <property name="isolationLevelName" value="DEFAULT"></property>
       <property name="propagationBehaviorName" value="REQUIRED"></property>
       <property name="readOnly" value="false"></property>
       <property name="timeout" value="-1"></property>
    </bean>

 4.2service

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TransactionDefinition txDefinition;
    @Autowired
    private PlatformTransactionManager txManager;
    /**
     * 转账
     * @param source
     * @param target
     * @param money
     */
    @Override
    public void updateUser(String source, String target, Float money) {
        // 获取一个事务
        TransactionStatus txStatus = txManager.getTransaction(txDefinition);
        try {

            userMapper.updateUserOfSub(source, money);
            int a = 6/0;
            userMapper.updateUserOfAdd(target, money);
            //提交事务
            txManager.commit(txStatus);
        }catch (Exception e){
            //回滚事务
            txManager.rollback(txStatus);
            e.printStackTrace();
        }
    }
}

 4.3测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
public class ServiceTest {
    @Autowired
    private UserService userService;
    /**
     * 转账业务
     */
    @Test
    public void testUpdate(){
        userService.updateUser("张三丰","宋远桥",1F);
    }
}

 事务回滚:

满足执行:

  • 我们现在虽然实现了事务控制,但是代码非常的臃肿,我们可以使用动态代理简化代码

5.动态代理控制事务

 5.1factory 

我们创建一个工厂,专门用来给 Service 创建代理对象,如下:  

package com.by.factory;

import com.by.service.UserService;
import com.by.service.UserServiceImpl;
import org.hamcrest.Factory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * bean工厂
 */
@Component
public class BeanFactory {
    @Autowired
    private UserService userService;
    @Autowired
    private TransactionDefinition txDefinition;
    @Autowired
    private PlatformTransactionManager txManager;

    /**
     * 获得UserServiceImpl对象
     *
     * @return
     */
    public UserService getUserService() {
        return (UserService) Proxy.newProxyInstance(
            userService.getClass().getClassLoader(),
            userService.getClass().getInterfaces(),
            new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args)
                        											throws Throwable {
                        //开启事务
                        TransactionStatus txStatus = 
                            txManager.getTransaction(txDefinition);
                        try {
                            method.invoke(userService, args);
                            //提交事务
                            txManager.commit(txStatus);
                        } catch (Exception e) {
                            //回滚事务
                            txManager.rollback(txStatus);
                            e.printStackTrace();
                        }
                        return null;
                    }
                });
    }
}

 5.2applicationContext.xml

<!--    注入工厂生成代理UserService-->
   <bean id="proxyUserService" factory-bean="beanFactory" factory-method="getUserService"></bean>

 5.3service

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 转账
     * @param source
     * @param target
     * @param money
     */
    @Override
    public void updateUser(String source, String target, Float money) {
        userMapper.updateUserOfSub(source, money);
        int a = 6/0;
        userMapper.updateUserOfAdd(target, money);
    }
}

 5.4测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class ServiceTest {
    @Autowired
    @Qualifier("proxyService")//注入代理对象
    private UserService userService;

    @Test
    public void testUpdate(){
        userService.updateUser("张三丰","宋远桥",1F);
    }
}
  • 事务回滚:

 6.Spring AOP控制事务

6.1导入schema约束

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

 6.2配置增强、切点、切面

<!--    增强:要做的事情。eg:日志、事务-->
    <tx:advice id="txManager" transaction-manager="transactionManager">
        <!--        定义属性-->
        <tx:attributes>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>


    <aop:config>
        <!--    切点:实际要增强的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.by.service.*.*(..))"/>
        <!--    切面:将增强作用与切点上-->
        <aop:advisor advice-ref="txManager" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>

 6.3测试

package com.by.web;

import com.by.pojo.User;
import com.by.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)//加载配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {

@Autowired
//    @Resource(name = "proxyUserService")
//@Qualifier("proxyUserService")
    private UserService userService;

@Test
    public void addUser(){
   userService.addUser(new User("小龙女",4000F));
   userService.addUser(new User ("李莫愁",2000F));
}

@Test
public void testUpdateUser(){
    userService.updateUser("张三丰","宋远桥",1F);
}
}

 事务回滚:

7.基于注解的AOP控制事务

7.1applicationContext.xml

<!-- 开启spring对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/> 

 7.2service

package com.by.service;

import com.by.mapper.UserMapper;
import com.by.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public void addUser(User user){
        userMapper.addUser(user);
    }

    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,timeout = -1,readOnly = false,
    rollbackFor = Exception.class)
    @Override
    public void updateUser(String source, String target, Float money) {

            userMapper.updateUserOutMoney(source,money);
            //制造转账异常
            int a=6/0;
            //转入
            userMapper.updateUserInMoney(target,money);

        }
    }

7.3测试

package com.by.web;

import com.by.pojo.User;
import com.by.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)//加载配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {

@Autowired
//    @Resource(name = "proxyUserService")
//@Qualifier("proxyUserService")
    private UserService userService;

@Test
    public void addUser(){
   userService.addUser(new User("小龙女",4000F));
   userService.addUser(new User ("李莫愁",2000F));
}

@Test
public void testUpdateUser(){
    userService.updateUser("张三丰","宋远桥",1F);
}
}

事务回滚:

8.总结 

spring事务控制
    1、事务介绍
        1)什么是事务?
            执行多条sql,要么全部执行,要么全部回滚
        2)事务的特点
            原子性:事务是最小执行单位
            一致性:事务前后数据都是正确的
            隔离性:事务之间看不到彼此的中间状态
            持久性:事务一旦提交不可再回滚
        3)mysql控制事务
            START TRANSACTION;
                sql1;
                sql2;
            COMMIT 或 ROLLBACK;
        4)jdbc控制事务

            try{
                conn.setAutoCommit(false);
                ....
                conn.commit();
            }catch(Exception e){
                conn.rollback();
            }

 spring事务控制的api
    1、PlatformTransactionManager
        作用:是一个事务管理器,负责开启、提交或回滚事务
        实现类:DataSourceTransactionManager(sqlSession)
    2、TransactionDefinition
        作用:定义事务的属性
        实现类:DefaultTransactionDefinition
        属性:
            1)隔离级别【有默认值】
                DEFAULT:默认值,等价于REPEATABLE_READ
                READ_UNCOMMITTED    x         x              x
                READ_COMMITTED      脏读       x              x
                REPEATABLE_READ     脏读      不可重复度
                SERIALIZABLE        脏读      不可重复度       幻读
            2)事务的传播行为【有默认值】
                REQUIRED:默认值,methodB()会加入到methodA()事务中
            3)事务超时【有默认值】
                默认-1:永不超时
            4)是否只读【有默认值】
                readOnly = false:默认值,适用于增删改
                readOnly = true:不记录log(效率快),适用于查询
            5)回滚规则
                可省略:运行时异常回滚,编译时异常不回滚
    3、TransactionStatus
        作用:代表一个事务

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值