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. ...

June 8, 2024 · 3 min · 453 words · In-Jun Hwang

Understanding the Persistence Context

What is a Persistence Context The Persistence Context in Java Persistence API (JPA) refers to an environment that manages entities. It keeps track of entities and their state changes in relation to the database. The Persistence Context is handled by an Entity Manager. Key Functions of a Persistence Context Entity Management: An EntityManager manages entities. The Persistence Context stores the initial state of an entity. Transaction Association: The Persistence Context’s lifecycle is tied to a transaction. When a transaction is committed, changes made to entities managed by the Persistence Context are reflected in the database. Dirty Checking: ...

June 8, 2024 · 2 min · 286 words · In-Jun Hwang

Understanding Dirty Checking

What is Dirty Checking? Dirty Checking is a mechanism in JPA (Java Persistence API) that automatically detects changes made to an entity and propagates those changes to the database. With Dirty Checking, developers can modify the state of an object without having to explicitly write database update queries. It’s important to note that Dirty Checking only applies to entities managed by the Persistence Context. How Dirty Checking Works Entity Management: Entities are managed by an EntityManager. The Persistence Context stores the initial state of the entities. Change Detection: Before a transaction is committed, JPA compares the current state of an entity with its initial state. Applying Changes: If any changes are detected, JPA automatically generates and executes database update queries. When the transaction is committed, the changes are propagated to the database. Example 1 2 3 4 5 6 7 8 9 10 11 12 13 // Saving the entity Member member = new Member("Alice"); memberRepository.save(member); // Retrieving the entity Member findMember = memberRepository.findById(member.getId()).get(); // Modifying the entity findMember.setName("Bob"); // Change detection // Dirty Checking kicks in and automatically generates and executes a database update query. // UPDATE member SET name = 'Bob' WHERE id = 1; Advantages Convenience: Developers only need to modify the state of the object, without having to write database update queries. Consistency: Data consistency is maintained as changes are automatically propagated to the database. Productivity: It improves productivity as developers can focus on business logic.

June 8, 2024 · 2 min · 240 words · In-Jun Hwang

Understanding the N+1 Problem

What is the N+1 Problem? The N+1 problem is a common performance issue in Object-Relational Mapping (ORM) where N additional queries are executed to retrieve associated entities for each associated entity when querying. As a result, the number of queries becomes N+1. A high number of queries can lead to increased communication with the database and can result in degraded performance. Therefore, the N+1 problem is a concern that requires attention when optimizing performance. ...

June 8, 2024 · 2 min · 304 words · In-Jun Hwang

What is ORM (Object-Relational Mapping)?

Developers dealing with databases will definitely have heard of ORM at least once. But many are unsure about what it is exactly and why it is used. This will help you learn about everything from ORM concepts to real-world applications. Table of Contents ORM concepts and definitions How ORM works Major ORM frameworks Advantages and Disadvantages of ORM Real-world use cases of ORM Frequently Asked Questions (FAQ) 1. ORM concepts and definitions ORM (Object-Relational Mapping) is a technology that bridges object-oriented programming and relational databases. Simply put, it is a tool that automatically maps objects used in programming languages to database tables. ...

May 15, 2024 · 3 min · 493 words · In-Jun Hwang