当前位置:网站首页>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();
}
边栏推荐
- [combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
- First knowledge of database
- "Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
- 相同切入点的抽取
- Break through 1million, sword finger 2million!
- 程序猿如何快速成长
- [redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
- 几种常见IO模型的原理
- 用同花顺炒股开户安全吗?
- Unreal_DataTable 实现Id自增与设置RowName
猜你喜欢
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Microservice - fuse hystrix
突破100万,剑指200万!
Mixlab编辑团队招募队友啦~~
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
Microservice - Nacos registration center and configuration center
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
App mobile terminal test [4] APK operation
Microservice API gateway
MB10M-ASEMI整流桥MB10M
随机推荐
Go语言自学系列 | golang中的if else if语句
Page dynamics [2]keyframes
远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
nifi从入门到实战(保姆级教程)——flow
分布式事务(Seata) 四大模式详解
Myopia: take off or match glasses? These problems must be understood clearly first
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
一些事情的反思
How to use AAB to APK and APK to AAB of Google play apps on the shelves
June to - -------
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
利用MySQL中的乐观锁和悲观锁实现分布式锁
探索Cassandra的去中心化分布式架构
Microservice sentinel flow control degradation
Stm32f103c8t6 firmware library lighting
疫情常态化大背景下,关于远程办公的思考|社区征文
Go language self-study series | if else statement in golang
[web security] - [SQL injection] - error detection injection
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage