当前位置:网站首页>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();
}
边栏推荐
- 坚持输出需要不断学习
- Microservices Seata distributed transactions
- [200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
- Principles of several common IO models
- Microservice sentinel flow control degradation
- [web security] - [SQL injection] - error detection injection
- Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
- 近视:摘镜or配镜?这些问题必须先了解清楚
- Microservice - fuse hystrix
- Uploads labs range (with source code analysis) (under update)
猜你喜欢
Colab works with Google cloud disk
Brush questions -- sword finger offer
"Remake Apple product UI with Android" (3) - elegant statistical chart
“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
Three dimensional reconstruction of deep learning
Uploads labs range (with source code analysis) (under update)
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
Introduction series of software reverse cracking (1) - common configurations and function windows of xdbg32/64
"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
随机推荐
Myopia: take off or match glasses? These problems must be understood clearly first
Go language self-study series | if else statement in golang
First!! Is lancet hungry? Official documents
突破100万,剑指200万!
Wechat payment -jsapi: code implementation (payment asynchronous callback, Chinese parameter solution)
切入点表达式
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
找映射关系
Unity功能——Unity离线文档下载及使用
Pychart error updating package list: connect timed out
App mobile terminal test [5] file writing and reading
坚持输出需要不断学习
uploads-labs靶场(附源码分析)(更新中)
利用MySQL中的乐观锁和悲观锁实现分布式锁
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
Embedded development: seven reasons to avoid open source software
Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
WinDbg analysis dump file
A Fei's expectation