Junior — Middle
Can you explain the concept of the first-level cache and its role in the system?
sobes.tech AI
Answer from AI
The first-level cache (L1 Cache) in the context of Java Persistence API (JPA) is associated with a specific EntityManager instance (or session in Hibernate). It stores objects loaded within a single transaction or session.
Role of L1 cache:
- Improves performance by reusing already loaded entities without querying the database again.
- Ensures data consistency within a session — if an object was loaded, all changes to it are visible within that session.
- Is automatically cleared when the EntityManager is closed or the transaction ends.
Example: if within a session you request the same entity twice by ID, the second time the object is retrieved from the L1 cache, not from the database.