Hibernate Performance Tuning
Cache Concurrency Strategies
There are four kinds of built-in cache concurrency strategies provided by Hibernate. Chosing a right concurrency strategy for your hibernate implementation is the key to cache performance optimization. Besides to ensure data consistency and transaction integrity it is indispensable to master these strategies.
read-only
If your application needs to read but never modify instances of a persistent class, a read-only cache may be used. This is the simplest and best performing strategy. It's even perfectly safe for use in a cluster.
nonstrict-read-write
If the application only occasionally needs to update data (For example, if it is extremely unlikely that two transactions would try to update the same item simultaneously) and strict transaction isolation is not required, a nonstrict-read-write cache might be appropriate.
read-write
If the application needs to update data, a read-write cache might be appropriate. This cache strategy should never be used if serializable transaction isolation level is required.
transactional
If the application seldom needs to update data and at the same time, application also needs to avoid “dirty read” and “repeatable read”, this kind of concurrency strategy can be employed. The transactional cache strategy provides support for fully transactional cache providers such as JBoss TreeCache.
The following table lists cache concurrency strategy supported by various cache providers.
|
Cache |
Read-only |
Nonstrict-read-write |
Read-write |
Transactional |
|
Hashtable |
YES |
YES |
YES |
N/A |
|
EHCache |
YES |
YES |
YES |
N/A |
|
OSCache |
YES |
YES |
YES |
N/A |
|
SwarmCache |
YES |
YES |
N/A |
N/A |
|
JBoss TreeCache |
YES |
N/A |
N/A |
YES |
Cluster Cache (in different JVMs)
Hibernate also supports cluster caching in disparate JVMs. At present, both SwarmCache and JBoss TreeCache support cluster caching across multiple JVMs. In some situations, especially at the level of enterprise, certain application has to support the concurrency accessing of thousands of users, at that time, cluster cache can help you because the cluster can provide failover and load balancing which improve the performance of application.
Points to Note
When employing one of the four cache strategies above, pay close attention to the following situation:
- Data cached almost immutable
If data you want to cache is almost constant, you can use data caching which can improve the performance of the application. On the contrary, if the caching data are quiet volatile, Hibernate have to maintain and update the caching over time which extremely leads to performance hit. - Data sizes in reasonable range
If the size of data you is caching is massive, Hibernate will occupy the most memories of system, which causes the long waiting time of the whole application. - Low frequency of data updating
If data you are caching needs to be modified frequently, Hibernate have to take an array of time to update and modify the data in caching, which impacts the performance of the application as well. - High frequency of data querying
If data you are caching is steady, which means that most of the operations are querying, searching, no updating and modifying, making the most use of caching will be affording huge performance improvement. - None crucial data
Because of existing some incongruities when keeping the data in caching, so if the data you are caching is fairly crucial, do not use caching. By contrast, if the data in caching is insignificant, just use it without any vacillation.
Summary
Actually, after employing SQL Optimization, Session Management, Data Caching, we will obtain great battalions of performance gains, which make applications achieve acceptable waiting time for the final customers.
External Links for Further Study
http://www.hibernate.org/hib_docs/reference/en/html/performance.html
http://blogs.jboss.com/blog/acoliver/2006/01/23/Hibernate_EJB3_Tuning.txt
About Author
I am Wings Jiang from BCM China. I have mainly focused on J2EE technologies in recent years and worked in several projects involving Struts/Tapestry, Spring, Hibernate, WebLogic, Websphere, Oracle, DB2 etc. I have experience in design and code of several Java applications. Hibernate performance is one of the areas I pay close heed to in my current working.
- Login or register to post comments
- 15053 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)











Comments
grabnerandi replied on Wed, 2009/06/10 - 5:47pm
Andi
sendon1982 replied on Thu, 2009/06/11 - 1:28am
Marlet replied on Thu, 2009/07/30 - 7:01pm
C Jones replied on Wed, 2009/10/07 - 3:53pm
Great artilce but it really annoys me how the whole Hibernate stacks are not versioned consistently? Surely Hibernate Core 3.4.0 release should bounce all sister projects to version 3.4.0, do you not think? It would be the best ensurance of compatibility. In the meantime, point releases 3.4.x of individual projects could be developed independently.
Chris Jones, London, UK
My Projects:
Free Slim PS3 | Free Gadgets
jason272 replied on Wed, 2009/10/21 - 3:19am
I love hibernate. We use it quite a lot.
e cigarette | e cigarettes
opastaja replied on Tue, 2009/10/27 - 1:46pm
niabroad replied on Tue, 2009/11/10 - 11:28pm