当前位置:网站首页>Hibernate的缓存机制/会话级缓存机制
Hibernate的缓存机制/会话级缓存机制
2022-07-03 16:02:00 【liaowenxiong】
Hibernate 中维持了两级缓存。第一级缓存由 Session 实例维护,该缓存区保存着与该 Session 实例有关联的所有实体对象的数据,也称为内部缓存。而第二级缓存则存在于 SessionFactory 实例中,由通过该 SessionFactory 实例获取的所有 Session 实例共享。
二级缓存需要配置启用才有效,二级缓存是在内存与磁盘间存储,当需要使用进入内存,不需要使用存在磁盘,这样就不占用内存。
Hibernate 的缓存机制的作用:
1.如果 session 被查询(例如执行 session.load()/session.get()),session 将先到一级缓存中查找是否有被查询的对象,找到则直接返回该对象;如果找不到则到二级缓存区查找,找到则直接返回给对象;如果还是找不到则查询数据库
2.缓存中的对象如果状态发生改变,会被标记下来(即维护对象的状态),当执行 session.flush() 时会将改变同步到数据库中
当实体对象在缓存区时则该对象处于持久态;当调用 session.evict(Object obj) 时,会将对象 obj 从 session 缓存中清除掉,则对象 obj 就处于游离态,等待被销毁。
测试代码:
/** * session一级缓存测试(会话级缓存) * * @param * @return * @throws * @author liaowenxiong * @date 2022/7/2 20:38 */
@Test
public void testSessionLevelCache() {
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
// 首次获取id=1的Employee对象,在Session一级缓存中没有该对象,于是会去查询数据库,因为被Session
// 处理过的对象会处于持久态,于是这个对象(e1所指向的对象)就是持久态的,会缓存在session中
Employee e1 = session.get(Employee.class, 3);
// 第二次获取id=1的Employee对象,会去session缓存中查找,存在则直接返回该对象
Employee e2 = session.get(Employee.class, 3);
// 所以e1和e2是同一个对象
System.out.println(e1 == e2); // 结果为true
e1.setNickname("liaowenxiong123");
session.flush();// 会将对象e1的更新同步到数据库中
session.evict(e1); // 将对象e1从缓存中清除,此时对象e1处于游离态
e1.setNickname("liaowenxiong234");// e1与session脱离了关系,因此后面提交事务时即使执行了flush()也不会将对象e1的更新同步到数据库中
tx.commit();
session.close();
}
边栏推荐
- [combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
- Reflection on some things
- 通过进程PID获取可执行文件路径(QueryFullProcessImageName)
- 切入点表达式
- Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
- Nifi from introduction to practice (nanny level tutorial) - flow
- [redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
- Under VC, Unicode and ANSI are converted to each other, cstringw and std:: string are converted to each other
- 使用AUR下载并安装常用程序
- 深度学习之三维重建
猜你喜欢

Low level version of drawing interface (explain each step in detail)
![SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]](/img/3b/7523eca5bbcdbba29d9b7f6e4791a5.jpg)
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]

Microservice sentinel flow control degradation

WinDbg analysis dump file

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)

WinDbg分析dump文件

Microservice - declarative interface call openfeign

"Remake Apple product UI with Android" (3) - elegant statistical chart

Microservice - Nacos registration center and configuration center

Mb10m-asemi rectifier bridge mb10m
随机推荐
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
The difference between calling by value and simulating calling by reference
CString中使用百分号
Unity功能——Unity离线文档下载及使用
相同切入点的抽取
Mongodb installation and basic operation
Jmeter线程组功能介绍
Redis高可用与持久化
Unityshader - materialcapture material capture effect (Emerald axe)
About text selection in web pages and counting the length of selected text
App移动端测试【4】apk的操纵
Distributed task scheduling XXL job
记一次jar包冲突解决过程
"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
June to - -------
大csv拆分和合并
Mb10m-asemi rectifier bridge mb10m
Microservice API gateway
Reflection on some things