2010/03/15

Improvement on Hibernate Query Cache

Cache improves performance by transparently storing data such that future requests for that data can be served faster. Hibernate offers a session scope transactional cache which spans the scope of the session. Hibernate also offers a configurable secondary level cache which spans across multiple sessions. Clustered/Distributed Caches can be configured so that it can be accessed across the entire application server cluster.

Hibernate secondary cache offers pretty good entity cache. Entity cache speeds up entity loading by its primary key. For instance:

select c from Cat c where c.id = :id

It supports query cache too, to enable query cache, you'll need to specify the hibernate property:

hibernate.cache.use_query_cache true

Query cache caches Query result sets, so the cache can provide you an answer for the queries like:

select p from Person p where p.sin = :sin

Let's say, the SIN of a person is never gonna change, so cache the above query will be useful.

Hibernate provides 'natural id' based on queries which can effectively solve the above problem. However, there are many other queries that hibernate cannot handle effectively. For instance, if we want to find all contact information (phone numbers only) for a person:

select pn from PhoneNumbers pn where pn.person.id = :pid 


The primary problem for Query caches, is that hibernate will completely invalidated by any modification to the underlying table, even on entities that's totally unrelated to the query. In the above example, any insertion/update/deletion to the telephone table, on any record, will completely wipe out the entire cache for the above query.

There needs to be a 'smart query cache' to solve this problem. Fortunately, this can be done by providing custom query cache, and we can then refreshes them by listening on hibernate 'entity modification' events. The listener will allow us to intercept any insertion/update/deletion to any telephone record, and then we can determine to only refresh the affected query cache.

11 comments:

web designing company said...

Thanks for this nice informative information..really wonderful insights are shared.

Unknown said...


I was totally amazed when i saw this website Best Java Online Training first time i thought this is what i am looking for from a long time i am very thankful to you for helping not only me but to all those guys who are new to this IT SECTOR and who wants to make a career ih this sector.

sappractices said...

For Free IT Study Materials,IT Certification Materials,Interview Questions and Job references.

sappractices@gmail.com

For All SAP Modules.
Testing Tools
Bigdata Technologies
Oracle Technologies
IBM Technologies
BI Tools

Thanks

deeksha said...

your cache concept is really nice and it is very much interesting too , i got more knowledge about your concept thus it is very much awesome and nice too.

ccna Training in Chennai

Shalini said...

Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.

digital marketing company in chennai

Durga IT Solutions said...
This comment has been removed by the author.
sara jenifer said...

BCOM 1st Year TimeTable 2020
BCOM 2nd Year TimeTable 2020
BCOM 3rd Year TimeTable 2020
Agra BCom Time Table 2020
Animachi is really hard to recognize since you took over the website - what you made of it and I really appreciate your commitment to it

TIC Academy said...

Excellent Post
Manual Testing Training in Chennai
QTP Training in Chennai
Selenium Training in Chennai
SoapUI Training in Chennai
Software Testing Training in Chennai

Parul Pathak said...

Check Latest Updates about your Universities Results. Timetable-Result.com This Site helps to clear your all query.
BA Exam result Part 1st, 2nd, Final Year
BCom Exam result 1st, 2nd, 3rd semester

BA Final Year Exam result 2020
BCom 3rd semester Exam result 2020

Sunita Agarwal said...

I think this is one of the most significant info for me. And I'm glad reading your article.

b.sc time table
BSc 1st year date sheet | B.Sc 2nd Year Time Table | B Sc 3rd year exam routine.

Golden Satta Matka said...

Thank you for sharing this useful information, I will regularly follow your blog.

Rajdhani Matka
Tara Matka
DP Boss

Well well... why another J2EE blog? I benefited from other people's technical blogs, and guess what, it's a good idea to contribute some of my works too. Hope it's helpful and useful, to all of your folks.