|
|||||||||||||||||||||||||||||||||||
igh-volume database traffic is a frequent cause of performance problems in Web applications. Hibernate is a high-performance, object/relational persistence and query service, but it won't solve all your performance issues without a little help. In many cases, second-level caching can be just what Hibernate needs to realize its full performance-handling potential. This article examines Hibernate's caching functionalities and shows how you can use them to significantly boost application performance.
An Introduction to CachingCaching is widely used for optimizing database applications. A cache is designed to reduce traffic between your application and the database by conserving data already loaded from the database. Database access is necessary only when retrieving data that is not currently available in the cache. The application may need to empty (invalidate) the cache from time to time if the database is updated or modified in some way, because it has no way of knowing whether the cache is up to date.
Hibernate CachingHibernate uses two different caches for objects: first-level cache and second-level cache. First-level cache is associated with the Session object, while second-level cache is associated with the Session Factory object. By default, Hibernate uses first-level cache on a per-transaction basis. Hibernate uses this cache mainly to reduce the number of SQL queries it needs to generate within a given transaction. For example, if an object is modified several times within the same transaction, Hibernate will generate only one SQL UPDATE statement at the end of the transaction, containing all the modifications. This article focuses on second-level cache. To reduce database traffic, second-level cache keeps loaded objects at the Session Factory level between transactions. These objects are available to the whole application, not just to the user running the query. This way, each time a query returns an object that is already loaded in the cache, one or more database transactions potentially are avoided.In addition, you can use a query-level cache if you need to cache actual query results, rather than just persistent objects.
Cache ImplementationsCaches are complicated pieces of software, and the market offers quite a number of choices, both open source and commercial. Hibernate supports the following open-source cache implementations out-of-the-box:
Each cache provides different capacities in terms of performance, memory use, and configuration possibilities:
Another cache implementation worth mentioning is the commercial Tangosol Coherence cache.
Caching StrategiesOnce you have chosen your cache implementation, you need to specify your access strategies. The following four caching strategies are available:
Support for these strategies is not identical for every cache implementation. Table 1 shows the options available for the different cache implementations.
The remainder of the article demonstrates single-JVM caching using EHCache.
|
|||||||||||||||||||||||||||||||||||
|