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>
public final void testQueryClazzById() {
SqlSession session = sqlSessionFactory.openSession();
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);
如下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>
public final void testQueryClazzById() {
SqlSession session = sqlSessionFactory.openSession();
ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
Clazz clazz1 = clazzMapper.queryClazzById(1);
System.out.println("clazz1 = "+clazz1);
SqlSession session2 = sqlSessionFactory.openSession();
ClazzMapper clazzMapper2 = session2.getMapper(ClazzMapper.class);
Clazz clazz2 = clazzMapper2.queryClazzById(1);
System.out.println("clazz2 = "+clazz2);
看下结果:
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>
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 );
看结果:
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>
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();
clazzMapper2.updateClazz(clazz);
System.out.println(clazzMapper2.queryClazzById(2));
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>
public void testfirstCacheFail4() {
SqlSession session = sqlSessionFactory.openSession();
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);
看下结果:
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,没有使用到缓存
- MySQL索引失效的几种情况
1.索引不存储null值 更准确的说,单列索引不存储null值,复合索引不存储全为null的值.索引不能存储Null,所以对这列采用is null条件时,因为索引上根本 没Null值,不能利用到索引, ...
- mysql索引总结(4)-MySQL索引失效的几种情况
mysql索引总结(1)-mysql 索引类型以及创建 mysql索引总结(2)-MySQL聚簇索引和非聚簇索引 mysql索引总结(3)-MySQL聚簇索引和非聚簇索引 mysql索引总结(4)-M ...
- C++迭代器失效的几种情况总结
一.序列式容器(数组式容器) 对于序列式容器(如vector,deque),序列式容器就是数组式容器,删除当前的iterator会使后面所有元素的iterator都失效.这是因为vetor,deque ...
- Spring事务失效的2种情况
使用默认的事务处理方式 因为在java的设计中,它认为不继承RuntimeException的异常是”checkException”或普通异常,如IOException,这些异常在java语法中是要求 ...
- Mysql索引会失效的几种情况分析(转)
出处:http://www.jb51.net/article/50649.htm 索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 1.如果条件中有or,即使其中有条件带索引也不会使用(这 ...
- oracle数据库中索引失效的几种情况
原文1:https://blog.csdn.net/u012255097/article/details/102792683 原文2:https://www.cnblogs.com/lanseyita ...
- vector迭代器失效的几种情况
在泛型编程还是STL的实际运用中,迭代器(iterator)无疑扮演者重要的角色.迭代器是一种类似于指针的对象(如可以内容提领,成员访问等),但他又不仅仅是一种普通的指针.关于迭代器失效,我们可以看下 ...
- sql 索引常见失效的几种情况
1. 对于联合索引,没有遵循左前缀原则 2. 索引的字段区分度不大,可能引起索引近乎全表扫描 3. 对于join操作,索引字段的编码不一致,导致使用索引失效 4.对于hash索引,范围查询失效,has ...
- Mysql索引会失效的几种情况
1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因): 2.对于多列索引,不是使用的第一部分,则不会使用索引: 3.like查询是以%开头: 4.如果列类型是字符串, ...
随机推荐
- spring初始化bean时执行某些方法完成特定的初始化操作
在项目中经常会在容器启动时,完成特定的初始化操作,如资源文件的加载等. 一 实现的方式有三种: 1.使用@PostConstruct注解,该注解作用于void方法上 2.在配置文件中配置init-me ...
- MRP设置自动执行
1.在计划向导中,找到调度运算,如下配置, 2.配置成功后自动生成执行计划
- 一个简单jpa例子
package com.example.demo.entity; import javax.persistence.*; /*使用jpa注解配置映射关系*/ /*告诉jpa这是一个实体类和(数据表映射 ...
- hive自定义函数
- 【转】解决Maxwell发送Kafka消息数据倾斜问题
最近用Maxwell解析MySQL的Binlog,发送到Kafka进行处理,测试的时候发现一个问题,就是Kafka的Offset严重倾斜,三个partition,其中一个的offset已经快200万了 ...
- Python turtle绘制阴阳太极图代码解析
本文详细分析如何使用Python turtle绘制阴阳太极图,先来分解这个图形,图片中有四种颜色,每条曲线上的箭头表示乌龟移动的方向,首先从中心画一个半圆(红线),以红线所示圆的直径作半径画一个校园, ...
- Java的HashMap数据结构
标题太大~~~自己做点笔记.别人写得太好了. https://www.cnblogs.com/liwei2222/p/8013367.html HashMap 1.6时代, 使用Entry[]数组, ...
- Logging - MVC Using Log4net Save to File and Database
第一步:创建Config文件夹和log4net.config 第二步:在log4net.confg黏贴以下配置 <?xml version="1.0" encoding=&q ...
- luogu1503
P1503 鬼子进村 题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. ...
- [TaskList] 省选前板子补完计划
省选前本子补完计划 [ ] 带权并查集 [ ] 树上莫队 - UOJ58 [WC2013]糖果公园 loj2485「CEOI2017」Chase