SQL Zone is brought to you in partnership with:

Mohammed Amine TAZI is a Sun Certified Enterprise Architect and J2EE developer. He's passionate about J2EE technology and learning foreign languages. Mohammed Amine is a DZone MVB and is not an employee of DZone and has posted 4 posts at DZone. View Full User Profile

Enjoy Hibernate Cache

12.21.2010
| 7904 views |
  • submit to reddit

Hibernate is a well-know persistence framework in the  Java world, it comes with a wonderful API to ease application communication with the database.

Caching is a mechanism that stores data so that future requests for that data can be served faster. It’s a good point for performance when there are a lot of requests for the same stuff . Hibernate comes with 3 caching mechanisms to resolve this issue.

First Level Cache

A hibernate session is a unit of work that’s corresponding to a DB transaction. When doing operations on entities, these latters are not stored immediately in DB but wait until the session commits. The hibernate session is hence representing the first level cache and it’s enabled by default.

Second Level Cache

A hibernate SessionFactory aims to create sessions, initializes JDBC connections and pool them. The second level cache lives in the SessionFactory level so all session can share it. This is why it’s called a process scoped cache.

The second level cache is not enabled by default. One has to configure the cache strategy for hibernate entities and the cache provider that will be a third party caching API like EHCache.

Query Cache

This is related to second level cache and is used for caching queries with parameters. It has to be configured like the second level cache.

To know if the application is hitting the cache, you can configure it in the session factory configuration file and begin your unit tests to get an idea of performance.

Enjoy hibernate cache and get to work !!

 

From http://www.misterj2ee.com/2010/12/enjoy-hibernate-cache/

Published at DZone with permission of Mohammed Amine Tazi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Sadsds Dsadsadsa replied on Tue, 2010/12/21 - 4:54am

Not a very useful article. I could have got this info from the online Hibernate docs. Why don't you add some useful info like what its like to use the different caches - good points, bad points, things to watch out for, how to write the unit tests, etc.

Mohammed Amine Tazi replied on Tue, 2010/12/21 - 5:27am

Got right but most people are afraid to read from official specs.

Derek Smith replied on Tue, 2010/12/21 - 8:14am

I just inherited a project from a pair of developers who sold management on Hibernate by telling them that SQL is slow/evil and Hibernate is fast/good. The first issue they had me fix was a dialog that was taking too long. Well, I turned on Hibernates SQL trace and also monitored the tcp/ip port. The process was taking 4 minutes and transmitting 32 megs of data for a relatively simple query. I counted 70+ SQL calls to the database before the console logging the calls ran out of room to record. Throwing away the Hibernate crap, and using a single SQL call reduced the performance to 8 seconds and 18k of data. Maybe their is a way that Hibernate could have been tuned to do this faster as well, I am not a Hibernate expert. My point is this, dont assume that just by using "Hibernate" your application will run faster.

Amin Mansuri replied on Tue, 2010/12/21 - 2:20pm

The advantage that tools like Hibernate gives you is some 3rd party tools. You cannot just load up and expect everything to work better than raw SQL. You need to configure it for your needs. But once you have hibernate, using a tool like JBoss Cache or Infinispan can make a big difference in your app. It also helps with JTA transactions if you're using several databases. But you need to get it right. For example, to cache things in the second level cache you need to get them with load() and not assume that an arbitrary query will cache your objects.

Sadsds Dsadsadsa replied on Wed, 2010/12/22 - 6:17pm in response to: Mohammed Amine Tazi

what? Afraid?

 If someone wanted to know about Hibernate caches, I don't think their first port of call will be your blog because they are scared of the official Hibernate docs(!).

Comment viewing options

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