Mybatis 缓存失效的几种情况

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS AI 助手,专业版
简介: 1 不在同一个sqlSession对象中下面比较下载同一个sqlSession和不在同一sqlSession下面的两种情况:同一sqlSession:@Test public final void testQueryClazzById() { SqlSession session = sqlSessionFactory.

1 不在同一个sqlSession对象中

下面比较下载同一个sqlSession和不在同一sqlSession下面的两种情况:

同一sqlSession:

@Test
    public final void testQueryClazzById() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
            Clazz clazz1 = clazzMapper.queryClazzById(1);
            System.out.println("clazz1 = "+clazz1);
            
            Clazz clazz2 = clazzMapper.queryClazzById(1);
            System.out.println("clazz2 = "+clazz2);
        } finally {
            session.close();
        }

如下sql执行了一次,第二次queryClazzById没有执行sql,直接从缓存里面获取。

DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz1 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
clazz2 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]

不在同一sqlSession:

@Test
    public final void testQueryClazzById() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
            Clazz clazz1 = clazzMapper.queryClazzById(1);
            System.out.println("clazz1 = "+clazz1);
        } finally {
            session.close();
        }
        //緩存失效的四種情況
        //不在同一個對象中
        SqlSession session2 = sqlSessionFactory.openSession();
        try {
            ClazzMapper clazzMapper2 = session2.getMapper(ClazzMapper.class);
            Clazz clazz2 = clazzMapper2.queryClazzById(1);
            System.out.println("clazz2 = "+clazz2);
        } finally {
            session2.close();
        }
    }
  看下结果:
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz1 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - Returned connection 1754638213 to pool.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Checked out connection 1754638213 from pool.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz2 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - Returned connection 1754638213 to pool. 
分别调用两次sql,没用使用缓存。
 

2    执行语句的参数不同。缓存中也不存在数据

@Test
    public void testfirstCacheFail2() {
        //一级缓存必须存在于同一个SqlSession中
        SqlSession session = sqlSessionFactory.openSession();
        ClazzMapper clazzMapper2 = session.getMapper(ClazzMapper.class);
        Clazz user1 = clazzMapper2.queryClazzById(1);
        System.out.println(user1);
        
        Clazz user2 = clazzMapper2.queryClazzById(2);
        System.out.println( user2 );
        session.close();
    }
看结果:
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 2
Clazz [id=2, name=javaEE20170325, stus=[Student [id=4, name=stu0325_马云, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]
分别调用两次sql,没有使用到缓存。
 

3、执行增,删,改,语句,会清空掉缓存

 @Test
    public void testfirstCacheFail3() {
        //一级缓存必须存在于同一个SqlSession中
        SqlSession session = sqlSessionFactory.openSession();
        ClazzMapper clazzMapper2 = session.getMapper(ClazzMapper.class);
        Clazz user1 = clazzMapper2.queryClazzById(2);
        System.out.println(user1);
        
        Clazz clazz = new Clazz();
        clazz.setId(2);
        clazz.setName("電影放映班");
        clazzMapper2.updateClazz(clazz);
        session.commit();
        
        System.out.println(clazzMapper2.queryClazzById(2));
        session.close();
    }

看结果:

DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 2
Clazz [id=2, name=javaEE20170325, stus=[Student [id=4, name=stu0325_马云, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]
DEBUG [main] - ==>  Preparing: update t_clazz set name = ? where id = ? 
DEBUG [main] - ==> Parameters: 電影放映班(String), 2(Integer)
DEBUG [main] - <==    Updates: 1
DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 2
Clazz [id=2, name=電影放映班, stus=[Student [id=4, name=stu0325_马云, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]
查询执行两次,更新执行一次,也没使用到缓存

4、手动清空缓存数据

@Test
    public  void testfirstCacheFail4() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
            Clazz clazz1 = clazzMapper.queryClazzById(1);
            System.out.println("clazz1 = "+clazz1);
            //清空緩存
            session.clearCache();
            Clazz clazz2 = clazzMapper.queryClazzById(1);
            System.out.println("clazz2 = "+clazz2);
        } finally {
            session.close();
        }
    }

看结果:

DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz1 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz2 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
执行了两次sql,没有使用到缓存

1    不在同一个sqlSession对象中

下面比较下载同一个sqlSession和不在同一sqlSession下面的两种情况
<wiz_code_mirror>
 
 
1
@Test
2
    public final void testQueryClazzById() {
3
        SqlSession session = sqlSessionFactory.openSession();
4
        try {
5
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
6
            Clazz clazz1 = clazzMapper.queryClazzById(1);
7
            System.out.println("clazz1 = "+clazz1);
8
            
9
            Clazz clazz2 = clazzMapper.queryClazzById(1);
10
            System.out.println("clazz2 = "+clazz2);
11
        } finally {
12
            session.close();
13
        }
 
 
如下sql执行了一次,第二次queryClazzById没有执行sql,直接从缓存里面获取。
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz1 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
clazz2 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
 
<wiz_code_mirror>
 
 
1
@Test
2
    public final void testQueryClazzById() {
3
        SqlSession session = sqlSessionFactory.openSession();
4
        try {
5
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
6
            Clazz clazz1 = clazzMapper.queryClazzById(1);
7
            System.out.println("clazz1 = "+clazz1);
8
        } finally {
9
            session.close();
10
        }
11
        //緩存失效的四種情況
12
        //不在同一個對象中
13
        SqlSession session2 = sqlSessionFactory.openSession();
14
        try {
15
            ClazzMapper clazzMapper2 = session2.getMapper(ClazzMapper.class);
16
            Clazz clazz2 = clazzMapper2.queryClazzById(1);
17
            System.out.println("clazz2 = "+clazz2);
18
        } finally {
19
            session2.close();
20
        }
21
    }
 
 
  看下结果:
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz1 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - Returned connection 1754638213 to pool.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Checked out connection 1754638213 from pool.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz2 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - Returned connection 1754638213 to pool. 
分别调用两次sql,没用使用缓存

2    执行语句的参数不同。缓存中也不存在数据

<wiz_code_mirror>
 
 
1
@Test
2
    public void testfirstCacheFail2() {
3
        //一级缓存必须存在于同一个SqlSession中
4
        SqlSession session = sqlSessionFactory.openSession();
5
        ClazzMapper clazzMapper2 = session.getMapper(ClazzMapper.class);
6
        Clazz user1 = clazzMapper2.queryClazzById(1);
7
        System.out.println(user1);
8
        
9
        Clazz user2 = clazzMapper2.queryClazzById(2);
10
        System.out.println( user2 );
11
        session.close();
12
    }
 
 
看结果:
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 2
Clazz [id=2, name=javaEE20170325, stus=[Student [id=4, name=stu0325_马云, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]
分别调用两次sql,没有使用到缓存

3、执行增,删,改,语句,会清空掉缓存

   
<wiz_code_mirror>
 
 
1
 @Test
2
    public void testfirstCacheFail3() {
3
        //一级缓存必须存在于同一个SqlSession中
4
        SqlSession session = sqlSessionFactory.openSession();
5
        ClazzMapper clazzMapper2 = session.getMapper(ClazzMapper.class);
6
        Clazz user1 = clazzMapper2.queryClazzById(2);
7
        System.out.println(user1);
8
        
9
        Clazz clazz = new Clazz();
10
        clazz.setId(2);
11
        clazz.setName("電影放映班");
12
        clazzMapper2.updateClazz(clazz);
13
        session.commit();
14
        
15
        System.out.println(clazzMapper2.queryClazzById(2));
16
        session.close();
17
    }
 
 
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 2
Clazz [id=2, name=javaEE20170325, stus=[Student [id=4, name=stu0325_马云, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]
DEBUG [main] - ==>  Preparing: update t_clazz set name = ? where id = ? 
DEBUG [main] - ==> Parameters: 電影放映班(String), 2(Integer)
DEBUG [main] - <==    Updates: 1
DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 2
Clazz [id=2, name=電影放映班, stus=[Student [id=4, name=stu0325_马云, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]
查询执行两次,更新执行一次,也没使用到缓存
4 、手动清空缓存数据
<wiz_code_mirror>
 
 
1
@Test
2
    public  void testfirstCacheFail4() {
3
        SqlSession session = sqlSessionFactory.openSession();
4
        try {
5
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
6
            Clazz clazz1 = clazzMapper.queryClazzById(1);
7
            System.out.println("clazz1 = "+clazz1);
8
            //清空緩存
9
            session.clearCache();
10
            Clazz clazz2 = clazzMapper.queryClazzById(1);
11
            System.out.println("clazz2 = "+clazz2);
12
        } finally {
13
            session.close();
14
        }
15
    }
 
 
 
看下结果:
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz1 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 3
clazz2 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]
执行了两次sql,没有使用到缓存
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
7月前
|
存储 缓存 NoSQL
mybatisplus一二级缓存
MyBatis-Plus 继承并优化了 MyBatis 的一级与二级缓存机制。一级缓存默认开启,作用于 SqlSession,适用于单次会话内的重复查询;二级缓存需手动开启,跨 SqlSession 共享,适合提升多用户并发性能。支持集成 Redis 等外部存储,增强缓存能力。
|
缓存 Java 数据库连接
mybatis复习05,mybatis的缓存机制(一级缓存和二级缓存及第三方缓存)
文章介绍了MyBatis的缓存机制,包括一级缓存和二级缓存的配置和使用,以及如何整合第三方缓存EHCache。详细解释了一级缓存的生命周期、二级缓存的开启条件和配置属性,以及如何通过ehcache.xml配置文件和logback.xml日志配置文件来实现EHCache的整合。
mybatis复习05,mybatis的缓存机制(一级缓存和二级缓存及第三方缓存)
|
9月前
|
缓存 Java 数据库连接
Mybatis一级缓存详解
Mybatis一级缓存为开发者提供跨数据库操作的一致性保证,有效减轻数据库负担,提高系统性能。在使用过程中,需要结合实际业务场景选择性地启用一级缓存,以充分发挥其优势。同时,开发者需注意其局限性,并做好事务和并发控制,以确保系统的稳定性和数据的一致性。
320 20
|
11月前
|
缓存 Java 数据库连接
Mybatis一级缓存、二级缓存详讲
本文介绍了MyBatis中的查询缓存机制,包括一级缓存和二级缓存。一级缓存基于同一个SqlSession对象,重复查询相同数据时可直接从缓存中获取,减少数据库访问。执行`commit`操作会清空SqlSession缓存。二级缓存作用于同一namespace下的Mapper对象,支持数据共享,需手动开启并实现序列化接口。二级缓存通过将数据存储到硬盘文件中实现持久化,为优化性能,通常在关闭Session时批量写入缓存。文章还说明了缓存的使用场景及注意事项。
431 7
Mybatis一级缓存、二级缓存详讲
|
12月前
|
缓存 Java 数据库连接
十、MyBatis的缓存
十、MyBatis的缓存
259 6
|
缓存 NoSQL Java
Mybatis学习:Mybatis缓存配置
MyBatis缓存配置包括一级缓存(事务级)、二级缓存(应用级)和三级缓存(如Redis,跨JVM)。一级缓存自动启用,二级缓存需在`mybatis-config.xml`中开启并配置映射文件或注解。集成Redis缓存时,需添加依赖、配置Redis参数并在映射文件中指定缓存类型。适用于查询为主的场景,减少增删改操作,适合单表操作且表间关联较少的业务。
267 6
|
SQL 缓存 Java
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
|
缓存 Java 数据库连接
深入探讨:Spring与MyBatis中的连接池与缓存机制
Spring 与 MyBatis 提供了强大的连接池和缓存机制,通过合理配置和使用这些机制,可以显著提升应用的性能和可扩展性。连接池通过复用数据库连接减少了连接创建和销毁的开销,而 MyBatis 的一级缓存和二级缓存则通过缓存查询结果减少了数据库访问次数。在实际应用中,结合具体的业务需求和系统架构,优化连接池和缓存的配置,是提升系统性能的重要手段。
520 4
|
缓存 Java 数据库连接
MyBatis缓存机制
MyBatis提供两级缓存机制:一级缓存(Local Cache)默认开启,作用范围为SqlSession,重复查询时直接从缓存读取;二级缓存(Second Level Cache)需手动开启,作用于Mapper级别,支持跨SqlSession共享数据,减少数据库访问,提升性能。
274 1
|
SQL 缓存 Java
MyBatis如何关闭一级缓存(分注解和xml两种方式)
MyBatis如何关闭一级缓存(分注解和xml两种方式)
641 5