http://www.devx.com | Printed from http://www.devx.com/dbzone/Article/29685 |
Speed Up Your Hibernate Applications with Second-Level Caching
Newer Hibernate developers sometimes don't understand Hibernate caching and use it poorly as a result. However, when used correctly, it can be one of the most powerful ways to accelerate Hibernate applications.
by John Ferguson Smart
|
||||||||||||||||||||||||||
![]()
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.
|
Working with Cached AssociationsSuppose you need to display the list of employees (with employee names, languages spoken, etc.) for a given country. The following is the Hibernate mapping of the Employee class: Suppose you really need to load the languages spoken by an employee every time you use the Employee object. To force Hibernate to automatically load the languages set, you set the lazy attribute to false. (This is just for the sake of this example. In general, deactivating lazy loading is not a good idea. Do it only when absolutely necessary.) You will also need a DAO class to fetch the employees. The following one would do the trick: Next, write some simple unit tests to see how it performs. As in the previous example, you should see how it performs when called repeatedly: If you run a test using the above configuration, you should get something like the following: So loading the 50 or so employees assigned to each country takes about a second each time. That's way too slow. This is typical of the N+1 query problem. If you activate the SQL logs, you will see one query on the EMPLOYEE table, followed by literally hundreds of queries on the LANGUAGE table: whenever Hibernate retrieves an employee object from the cache, it reloads all the associated languages. So how can you improve on this? The first thing to do is activate read/write caching on the Employee class as follows: You should also activate caching on the Language class. Read-only caching should do here: Then, you will need to configure the cache rules by adding the following entries to the ehcache.xml file: This is fine, but it doesn't solve the N+1 query problem: 50 or so extra queries will still be executed whenever you load an Employee. This is a case where you need to activate caching on the language association in the Employee.hbm.xml mapping file, as follows: In this configuration, you should get near-optimal performance:
|
Using Query CachesIn certain cases, it is useful to cache the exact results of a query, not just certain objects. For example, the getCountries() method probably should return exactly the same country list each time it is called. So, in addition to caching the Country class, you could also cache the query results themselves.To do this, you need to set the hibernate.cache.use_query_cache property in the hibernate.cfg.xml file to true, as follows: Then, you use the setCacheable() method as follows on any query you wish to cache: To guarantee the non-staleness of cache results, Hibernate expires the query cache results whenever cached data is modified in the application. However, it cannot anticipate any changes made by other applications directly in the database. So you should not use any second-level caching (or configure a short expiration timeout for class- and collection-cache regions) if your data has to be up-to-date all the time.
Proper Hibernate CachingCaching is a powerful technique, and Hibernate provides a powerful, flexible, and unobtrusive way of implementing it. Even the default configuration can provide substantial performance improvements in many simple cases. However, like any powerful tool, Hibernate needs some thought and fine-tuning to obtain optimal results, and caching—like any other optimization technique—should be implemented using an incremental, test-driven approach. When done correctly, a small amount of well executed caching can boost your applications to their maximum capacities.
John Ferguson Smart has worked on many large-scale J2EE projects involving international and offshore teams for government and business entities. His specialties are J2EE architecture and development and IT project management. He also has a broad experience with open source Java technologies. Check out his technical blog at www.jroller.com/page/wakaleo.
|
DevX is a division of Jupitermedia Corporation © Copyright 2005 Jupitermedia Corporation. All Rights Reserved. Legal Notices |