美文网首页
【不可重复读】Spring事务的并发问题(脏读,不可重复读、幻读

【不可重复读】Spring事务的并发问题(脏读,不可重复读、幻读

作者: 凡哥爱丽姐 | 来源:发表于2020-11-12 14:22 被阅读0次

不可重复读

这是由于查询时系统中其他事务修改的提交而引起的。比如事务T1读取某一数据,事务T2读取并修改了该数据,T1为了对读取值进行检验而再次读取该数据,便得到了不同的结果。

一种更易理解的说法是:在一个事务内,多次读同一个数据。在这个事务还没有结束时,另一个事务也访问该同一数据并修改数据。那么,在第一个事务的两次读数据之间。由于另一个事务的修改,那么第一个事务两次读到的数据可能不一样,这样就发生了在一个事务内两次读到的数据是不一样的,因此称为不可重复读,即原始读取不可重复。

不可重复读

示例:

1、UserDao接口

public interface UserDao {
    //不可重复读
    public List find2();

    public void update2();
}

2、UserDaoImpl实现类

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
    //测试不可重复读
    //@Transactional(isolation =Isolation.READ_COMMITTED)
    //解决不可重复读
    @Transactional(isolation =Isolation.REPEATABLE_READ)
    public List find2() {
        System.out.println("查询进入");
        List before=getJdbcTemplate().queryForList("select * from user where id=1");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List after=getJdbcTemplate().queryForList("select * from user where id=1");

        List list2=new ArrayList();
        list2.add(before);
        list2.add(after);
        return list2;
    }

    public void update2() {
        String sql="update user set money=222 where id=1";
        getJdbcTemplate().update(sql);
    }

}

3、applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--1、指定数据源-->
    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/weifan"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg index="0" ref="datasource"></constructor-arg>
    </bean>

    <bean id="userDao" class="dao.impl.UserDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!--1、创建事务管理器对象-->
    <bean id="tx"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"/>
    </bean>

    <!--① 对标注@Transactional注解的Bean进行加工处理,以织入事物管理切面 -->
    <tx:annotation-driven transaction-manager="tx" />
</beans>

4、测试类

public class Test2 {
    ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
    class  A extends  Thread{
        public void run(){
            System.out.println("查询线程开始");
            UserDao dao=(UserDao) app.getBean("userDao");
            List list=dao.find2();
            System.out.println("before:"+list.get(0));
            System.out.println("after:"+list.get(1));
        }
    }
    class  B extends  Thread{
        public void run(){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("更新线程开始");
            UserDao dao=(UserDao) app.getBean("userDao");
            dao.update2();
        }
    }

    public static void main(String[] args) {
        Test2 test1=new Test2();
        Test2.A a=test1.new A();
        Test2.B b=test1.new B();
        a.start();
        b.start();
    }
}

测试结果

测试结果

解决不可重复读

 @Transactional(isolation =Isolation.REPEATABLE_READ)
解决不可重复读

相关文章

  • MySQL Innodb 事务隔离级别

    Reference 事务并发的可能问题与其解决方案脏读、幻读、不可重复读和丢失更新数据库并发事务存在的问题(脏读、...

  • SQL 多个事务并发时可能遇到的问题

    Reference 事务并发的可能问题与其解决方案脏读、幻读、不可重复读和丢失更新数据库并发事务存在的问题(脏读、...

  • mysql知识点

    mysql概览 一些基本问题 事务 事务相关基本问题 脏读 丢失修改 不可重复读 幻读 不可重复读 vs 幻读不可...

  • mysql数据mvcc版本控制原理

    事务并发执行遇到的问题 脏读(未提交读) 不可重复读(已提交读) 幻读(读出新纪录) 事务隔离级别 隔离级别脏读不...

  • 理解事务的隔离特性

    理解事务的隔离特性 脏读、不可重复读、幻读 脏读:读到别的事务还未提交的修改 不可重复读:读到别的事务已提交的修改...

  • Mysql 并发事务带来的问题

    并发事务带来的问题 针对 mysql InnoDB 编号问题描述1脏读2不可重复读3幻读4丢失更新两个事务同时修改...

  • 2018-02-07-3.spring事务管理

    事务 事务特性:acid 事务并发问题 1.脏读2.不可重复读3.幻读 事务的隔离级别 1 读未提交2 读已提交4...

  • 数据库事务隔离级别

    数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)

  • 数据库事务隔离级别

    脏读 不可重复读 幻读

  • Innodb事务隔离级别和锁之间的关系

    1.事务特性: 2.事务隔离级别: 1.读未提交。 脏读,不可重复读,幻读。2.读已提交。 不可重复读。3.可重复...

网友评论

      本文标题:【不可重复读】Spring事务的并发问题(脏读,不可重复读、幻读

      本文链接:https://www.haomeiwen.com/subject/ufozmktx.html