Java Reference
In-Depth Information
eager loading depend on the persistence framework, and I will describe the pro-
cess in chapters 5 and 6.
Eager loading is only one of the ways to optimize the performance of a
JDO
or
Hibernate application. Another way to improve performance is to reduce data-
base accesses by using a process-level cache.
4.6.3
Using a process-level cache
Eager loading improves performance by loading related objects with a single
SELECT
statement. In contrast, process-level caching improves performance by
eliminating
SELECT
statements entirely by retrieving objects from the process-level
cache instead of the database. To use a process-level cache, we must first determine
which objects to keep in the cache. In this example, the restaurant-related classes—
Restaurant
,
MenuItem
, and
TimeRange
—are rarely updated and thus are good can-
didates for process-level caching. We would then configure the persistence frame-
work to cache those objects, which I describe how to do in chapters 5 and 6.
Once we have decided which objects to cache, the next step is to optimize
object loading by configuring eager loading as described in the previous section.
When doing this, however, it is important to lazily load relationships from non-
cached objects to cached objects. Otherwise, the application would bypass the
cache and load the objects from the database.
For example, when handling the update payment information request only
the
PendingOrder
-
PendingOrderLineItem
relationship should be loaded eagerly.
The other relationships, such as
PendingOrder
-
Restaurant
and
PendingOrderLi-
neItem
-
MenuItem
, must be loaded lazily to ensure that the restaurants and menu
items are retrieved from the process-level cache. The persistence framework
would use a
SQL
statement that does a join between the
PENDING_ORDER
and
PENDING_ORDER_LINE_ITEM
tables:
select *
from FTGO_PENDING_ORDER po
left outer join FTGO_PENDING_ORDER_LINE_ITEM li
on po.PENDING_ORDER_ID=li.PENDING_ORDER_ID
where po.PENDING_ORDER_ID=?
The other objects would be retrieved from the process-level cache when the appli-
cation navigates to them.
4.6.4
Using the query cache
So far, we have optimized the loading of a pending order and its related objects
by caching the restaurants and using queries with fetch joins. We also need to
Search WWH ::
Custom Search