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.如果列类型是字符串, ...
随机推荐
- Oracle 强制中止正在执行的SQL语句
-- 1 查询正在执行的sql语句 select b.sid, b.username, b.serial#, a.spid, b.paddr, c.sql_text, b.machine from v ...
- java核心API学习
1:java.lang (Object.String.StringBuffer.Thread.System.ClassLoader.Class.Runtime.包装类等)
- 转《JavaScript中的图片处理与合成》
引言: 本系列现在构思成以下4个部分: 基础类型图片处理技术之缩放.裁剪与旋转(传送门): 基础类型图片处理技术之图片合成(传送门): 基础类型图片处理技术之文字合成(传送门): 算法类型图片处理技术 ...
- java学习之—递归实现二分查找法
/** * 递归实现二分查找法 * Create by Administrator * 2018/6/21 0021 * 上午 11:25 **/ class OrdArray{ private lo ...
- B站弹幕姬(🐔)分析与开发(上篇)
辞职之后 休息了一段时间,最近准备开始恢复去工作的状态了,所以搞点事情来练练手.由于沉迷b站女妆大佬想做个收集弹幕的然后根据弹幕自动回复一些弹幕的东西.网上搜了一下有个c#的版本,感觉还做得不错,于是 ...
- ssh 登陆服务器原理
这里分两种情况,这两种情况都涉及到公钥加密的概念. 由于公钥加密概念作为基础就不在本文进行讨论了. 使用ssh对远程服务器进行密码登录发生了什么: 客户端通过ssh连接服务器 1. 首先服务器把自己的 ...
- Springmvc架构
框架结构如下图: 架构流程: 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器. 3.处理器映 ...
- 在python中定义二维数组
发表于 http://liamchzh.0fees.net/?p=234&i=1 一次偶然的机会,发现python中list非常有意思. 先看一段代码 [py]array = [0, 0, 0 ...
- Fabric运维从入门到精通
1. fabric的安装 在windows下的python3中安装fabric: 在python安装根目录下使用pip install fabric 安装 如图: fabric只支持python2不支 ...
- do not track
privacy.trackingprotection.enabled