-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: EHcache in Hibernate
PostPosted: Thu Feb 09, 2006 5:27 pm 
Newbie

Joined: Wed Nov 02, 2005 4:29 am
Posts: 7
Hi,

I am wondering if it is possible to use the query cache for Hibernate when there are different user, with different sessions, that should get different results depending on user.

My application retrieves a list from the database, and this result is hardly ever changed during the user's session. So it should be nice to cache the result of the query. However, the list that is the result is always different between users, and therefore I do not want the cache to be shared between users and sessions.

Regards

Pichdude


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 8:08 pm 
Beginner
Beginner

Joined: Wed Feb 08, 2006 5:45 pm
Posts: 23
Location: Phoenix, AZ
From what I understand, when you execute a query Hibernate automatically checks the Session's cache (first-level) to see if it is there. If there is no match then Hibernate checks the second-level cache (EHCache) which contains instances (if it's enabled for the particular Class) from all Sessions that originated from a particular SessionFactory.

Therefore, the first thing that comes to mind is to give each User their own Session and use that Session for all queries from the User. That way, if a User is requesting the List, the Session will check and see if it's cached before it goes to the database. And since each User has their own Session, they should only have a reference to their List and not anyone elses.

I noticed that your post said that Users are using different Sessions but in order for you to properly utilize the caching you might want to see if you can change that. The only other thing I can think about is creating a SessionFactory for each User, as then the second-level cache can be used per User, but that way is horribly inefficient and will eat up a lot more of your resources than the Session-per-User way which doesn't even require using a second-level cache.

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 11, 2006 2:22 pm 
Newbie

Joined: Sat Feb 11, 2006 1:31 pm
Posts: 3
Location: New York, NY
Is the query exactly the same for two different users, or is there a parameter difference?

Say the query is "from AccountTransaction where user=:user", and you call setEntity("user", currentUser). In that case, the query will be placed in the second-level cache separately for each different currentUser, and there's no risk of one user getting another's account history.

Just make sure you cache this query correctly and that you've configured the AccountTransaction entity (or whatever it is you are actually returning) to cache, too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.