Mybatis 缓存失效的几种情况

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 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,没有使用到缓存
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
缓存 Java 数据库连接
MyBatis的缓存
MyBatis的缓存
|
16天前
|
XML 缓存 Java
MyBatis二级缓存解密:深入探究缓存机制与应用场景
MyBatis二级缓存解密:深入探究缓存机制与应用场景
50 2
MyBatis二级缓存解密:深入探究缓存机制与应用场景
|
1月前
|
存储 缓存 Java
什么!?实战项目竟然撞到阿里面试的原题!???关于MyBatis Plus的缓存机制
什么!?实战项目竟然撞到阿里面试的原题!???关于MyBatis Plus的缓存机制
|
1月前
|
缓存 Java 数据库连接
mybatis 数据库缓存的原理
MyBatis 是一个流行的 Java 持久层框架,它封装了 JDBC,使数据库交互变得更简单、直观。MyBatis 支持两级缓存:一级缓存(Local Cache)和二级缓存(Global Cache),通过这两级缓存可以有效地减少数据库的访问次数,提高应用性能。
282 1
|
1月前
|
存储 缓存 Java
【MyBaits】4、延迟加载、MyBatis 的缓存
【MyBaits】4、延迟加载、MyBatis 的缓存
22 0
|
2月前
|
SQL 缓存 Java
mybatis缓存详解
mybatis缓存详解
23 0
|
3月前
|
缓存 Java 数据库连接
mybatis的缓存内容(下)
mybatis的缓存内容
30 0
|
3月前
|
SQL 缓存 Java
mybatis的缓存内容(上)
mybatis的缓存内容
31 0
|
3月前
|
缓存 Java 数据库连接
|
3月前
|
缓存 Java 数据库连接
MyBatis支持的缓存刷新模式
MyBatis支持的缓存刷新模式
217 1