Java Reference
In-Depth Information
in the O/R mapping. For example, we would configure process-level caching for
the Restaurant class as follows:
<class name="Restaurant"
table="RESTAURANT">
<cache usage="read-write"/>
</class>
The usage="read-write" attribute specifies that instances of this class are some-
times updated by the application. As we saw in chapter 4, cached classes that are
updated by the application should almost always use optimistic locking in order to
prevent the application from updating the database with stale data. To ensure that
the application uses the cache, you must also enable lazy loading for relationships
that are from objects that are not cached to objects that are. For example, you
must arrange for the PendingOrder - Restaurant relationship to use lazy loading to
ensure that restaurants are loaded from the cache.
In this example, by caching the restaurants and menu items and by configur-
ing only the PendingOrder - PendingOrderLineItem to be eagerly loaded, the appli-
cation will load the pending order, its restaurant, its line items, and its menu items
using a single SQL SELECT statement.
6.5.3
Using a 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 con-
sider improving the performance of the query that finds the available restaurants.
By default, executing a Hibernate query causes the execution of a SQL SELECT
statement even if the application uses a process-level cache. Some applications
can benefit from the Hibernate query cache, which caches the results of a query
and eliminates the need to access the database. To enable the query cache, the
application must set the property hibernate.cache.use_query_cache to true ; to
cache a particular query, the application must call Query.setCacheable(true) .
Caching a query only improves performance if it is executed frequently, and
the application rarely updates the tables referenced by the query because that
causes Hibernate to remove the query from the cache. Caching the query that
finds available restaurants might improve performance. However, because there
are potentially many combinations of values for the query's parameters—ZIP
code and delivery time—it is unclear whether there would be any advantage, and
we would have to analyze the running application to determine that.
 
 
 
Search WWH ::




Custom Search