当前位置:网站首页>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();
}
边栏推荐
- 高等数学(第七版)同济大学 习题2-1 个人解答
- Reading notes of "micro service design" (Part 2)
- VC下Unicode和ANSI互转,CStringW和std::string互转
- Break through 1million, sword finger 2million!
- Go语言自学系列 | golang中的if else语句
- From the 18th line to the first line, the new story of the network security industry
- Embedded development: seven reasons to avoid open source software
- First!! Is lancet hungry? Official documents
- Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
- Redis installation under windows and Linux systems
猜你喜欢
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
WinDbg analysis dump file
Reading notes of "micro service design" (Part 2)
子类隐藏父类的同名函数
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
几种常见IO模型的原理
App移动端测试【4】apk的操纵
Create gradle project
Detailed pointer advanced 1
随机推荐
First!! Is lancet hungry? Official documents
Introduction series of software reverse cracking (1) - common configurations and function windows of xdbg32/64
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
Microservice API gateway
Go语言自学系列 | golang switch语句
Effect of ARP package on FTP dump under vxworks-6.6 system
Mixlab编辑团队招募队友啦~~
Unityshader - materialcapture material capture effect (Emerald axe)
请求头不同国家和语言的表示
无心剑中译泰戈尔《漂鸟集(1~10)》
Persisting in output requires continuous learning
[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
C language brush questions ~leetcode and simple questions of niuke.com
Go language self-study series | if else if statement in golang
Driver and application communication
Detailed explanation of string function and string function with unlimited length
"Remake Apple product UI with Android" (3) - elegant statistical chart
突破100万,剑指200万!