Java Reference
In-Depth Information
QUICK SUMMARY
1. JPA can perform several optimizations to limit (or increase) the amount of data
read in a single operation.
2. Large fields (e.g., BLOBs) that are not frequently used should be loaded lazily in
a JPA entity.
3. When a relationship exists between JPA entities, the data for the related items can
be loaded eagerly or lazily. The choice depends on the needs of the application.
4. When eagerly loading relationships, named queries can be used to issue a single
SQL statement using a JOIN statement. Be aware that this affects the JPA cache; it
is not always the best idea (as the next section discusses).
5. Reading data via named queries will often be faster than a regular query, since it
is easier for the JPA implementation to use a PreparedStatement for named
queries.
JPA Caching
One of the canonical performance-related use cases for Java is to supply a middle tier that
caches data from backend database resources. The Java tier performs a number of architec-
turally useful functions (such as preventing clients from directly accessing the database).
From a performance perspective, caching frequently used data in the Java tier can greatly
speed up response times for the clients.
JPA is designed with that architecture in mind. There are two kinds of caches in JPA. Each
entity manager instance is its own cache: it will locally cache data that it has retrieved during
a transaction. It will also locally cache data that is written during a transaction; the data is
sent to the database only when the transaction commits. A program may have many different
entity manager instances, each executing a different transaction, and each with its own local
cache. (In particular, the entity managers injected into Java EE applications are distinct in-
stances.)
When an entity manager commits a transaction, all data in the local cache can be merged into
a global cache. The global cache is shared among all entity managers in the application. The
global cache is also known as the Level 2 (L2) cache or the second-level cache; the cache in
the entity manager is known as the Level 1, L1, or first-level cache.
Search WWH ::




Custom Search