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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

0
Average: 3 (3 votes)

(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

Getting insight into frameworks like Hibernate is essential to correctly implement solutions on top of those frameworks. Alois Reitbauer - Technology Strategist from dynaTrace - wrote 3 great articles dedicated to the internals of Hibernate Caches - its worth reading them as they give insight into the internals of the cahces:
  • Understanding Caching in Hibernate - Part One : The Session Cache
  • Understanding Caching in Hibernate - Part Two : The Query Cache
  • Understanding Caching in Hibernate - Part Three : The Second Level Cache

    Andi
  • sendon1982 replied on Thu, 2009/06/11 - 1:28am

    Hibernate is quite popular in current IT world.

    Marlet replied on Thu, 2009/07/30 - 7:01pm

    I have to agree,I use it all the time Free Macbook | Free iPhone 3GS

    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

    Hibernate also provides data query and retrieval facilities. Hibernate generates the SQL calls and relieves the developer from manual result set handling and object conversion, keeping the application portable to all supported SQL databases, with database portability delivered at very little performance overhead. piilolinssit ja ongelmatilanteet

    niabroad replied on Tue, 2009/11/10 - 11:28pm

    Nice article. I agree with what you said. Thanks

    Comment viewing options

    Select your preferred way to display the comments and click "Save settings" to activate your changes.