当前位置:网站首页>Caching mechanism of Hibernate / session level caching mechanism
Caching mechanism of Hibernate / session level caching mechanism
2022-07-03 16:15:00 【liaowenxiong】
Hibernate Two levels of cache are maintained in . The first level cache is created by Session Instance maintenance , The cache holds the data associated with the Session The data of all entity objects associated with the instance , Also known as internal caching . And the second level cache exists in SessionFactory In the example , By the SessionFactory All obtained by the instance Session The instance Shared .
L2 cache needs to be configured and enabled to be effective , L2 cache is stored between memory and disk , When it needs to be used, enter memory , There is no need to use the existing disk , This will not occupy memory .
Hibernate The role of caching mechanism :
1. If session Be inquired of ( For example, to perform session.load()/session.get()),session First, you will go to the first level cache to find whether there are queried objects , If found, the object will be returned directly ; If it cannot be found, look in the L2 cache , If found, it will be returned directly to the object ; If it still cannot be found, query the database
2. If the state of the object in the cache changes , Will be marked ( That is, maintain the state of the object ), When executed session.flush() Changes will be synchronized to the database
When an entity object is in the cache, the object is in a persistent state ; When calling session.evict(Object obj) when , The object will be obj from session Clear from cache , Object obj It is in a free state , Waiting to be destroyed .
Test code :
/** * session First level cache test ( Session level caching ) * * @param * @return * @throws * @author liaowenxiong * @date 2022/7/2 20:38 */
@Test
public void testSessionLevelCache() {
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
// Get... For the first time id=1 Of Employee object , stay Session The object is not in the L1 cache , So I will query the database , Because being Session
// The processed object will be in a persistent state , So this object (e1 The object that is pointed to ) It's persistent , Will be cached in session in
Employee e1 = session.get(Employee.class, 3);
// Get... For the second time id=1 Of Employee object , Will go to session Search in cache , If it exists, the object will be returned directly
Employee e2 = session.get(Employee.class, 3);
// therefore e1 and e2 Is the same object
System.out.println(e1 == e2); // The result is true
e1.setNickname("liaowenxiong123");
session.flush();// The object will be e1 The updates of are synchronized to the database
session.evict(e1); // Put the object e1 Clear from cache , At this point the object e1 In a free state
e1.setNickname("liaowenxiong234");// e1 And session Out of the relationship , Therefore, even if the transaction is committed later flush() Nor will the object e1 The updates of are synchronized to the database
tx.commit();
session.close();
}
边栏推荐
- Pyinstaller is not an internal or external command, nor is it a runnable program or batch file
- nifi从入门到实战(保姆级教程)——flow
- 疫情常态化大背景下,关于远程办公的思考|社区征文
- 用通达信炒股开户安全吗?
- 几种常见IO模型的原理
- Initial test of scikit learn Library
- TCP擁塞控制詳解 | 3. 設計空間
- 请求头不同国家和语言的表示
- Microservices Seata distributed transactions
- Colab works with Google cloud disk
猜你喜欢

How can technology managers quickly improve leadership?

远程文件包含实操

"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
![[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display](/img/46/c7f566f8fd46d383b055582d680bb7.png)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display

关于网页中的文本选择以及统计选中文本长度

Embedded development: seven reasons to avoid open source software

请做好3年内随时失业的准备?

“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画

About text selection in web pages and counting the length of selected text
![[系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解](/img/cd/00954b9c592c253d42e6a3b8298999.jpg)
[系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解
随机推荐
半监督学习
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
[statement] about searching sogk1997 and finding many web crawler results
Intelij idea efficient skills (III)
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
TCP拥塞控制详解 | 3. 设计空间
Qt插件之自定义插件构建和使用
[system safety] 43 PowerShell malicious code detection series (5) automatic extraction of ten thousand words from abstract syntax tree
"Everyday Mathematics" serial 56: February 25
《天天数学》连载56:二月二十五日
近视:摘镜or配镜?这些问题必须先了解清楚
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Please be prepared to lose your job at any time within 3 years?
Microservice API gateway zuul
Microservice sentinel flow control degradation
【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
Is it safe to open an account with flush?
Approval process design
Go language self-study series | if else if statement in golang
Microservice API gateway