Understanding First-Level and Second-Level Caches
First-Level Cache First-level cache (L1 cache) is a cache that exists within the persistence context. When an entity is retrieved, the persistence context stores the entity in the cache. If the same entity is retrieved later on, the persistence context finds the entity in the cache and returns it. Hence, it is only valid within a transaction, and when the transaction ends, the first-level cache is also terminated. Dirty Checking Dirty checking is a way of tracking changes to entities using the first-level cache. When an entity is retrieved, the persistence context stores the initial state of the entity. If the state of the entity changes later on, the persistence context tracks the changes and reflects them in the database. ...