-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with results when using different sessions
PostPosted: Tue Oct 28, 2008 4:05 pm 
Newbie

Joined: Tue Sep 23, 2008 1:39 pm
Posts: 8
Hi,
I'm using per-thread hibernate session (ThreadLocal) but additionally i'm saving the session to http-session so what i am actually getting is a per-http-session hibernate session.

I've the hibernate session's flush mode set to FlushMode.COMMIT so every time i perform any operation it is immediately updated to the database (after the transaction commit). I can see the updated records, so the flushing is working.

In spite of this, when i query records from another session i get them out of date. It seems like the session does not get rid of the changes made by another session so it gets the records from cache.

Do anybody know what is happening? How can i synchronize my sessions?
Any help will be welcome,
Thanks

_________________
Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 6:00 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Session has a cache, so changes made in a session are not known by the others (That's the normal working way, and I don't know anyway to share info between sessions). The only way to refresh data, so that it is read again from the database, is using the refresh() function of the session, but you may only refresh a single object each time.

session.refresh(myBean)

Where myBean is an entity you saved or read from the database, and is still in session.

Another option, would be to delete the whole session cache, using clear(), or a single entity using evict

Anyway, Is there any reason why you are using a per-http-session hibernate session?. If you wish to cache your data in a consistent way to avoid unnecesary database access, it is much better to use a second level cache, and an hibernate session per http request. That way, you will have an small cache per each request (hibernate session cache), and a bigger cache, common to all users.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 6:27 pm 
Newbie

Joined: Tue Sep 23, 2008 1:39 pm
Posts: 8
Thanks for your reply.

In fact i would like to keep a single hibernate session for all the requests if that's possible without collision between the requests.

But maybe the way you describe is better, i don't know anything about second level cache, can you give me some help on implementing it?

Thanks

_________________
Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 6:44 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
I never used one, but chapter 19.2 of the documentation tells you how to use it (seems not difficult).

http://www.hibernate.org/hib_docs/v3/reference/en-US/html_single/#performance-cache

You don't have to implement a second level cache, just download and use one of the supported ones. For example EHCache:

http://ehcache.sourceforge.net/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 6:55 pm 
Newbie

Joined: Tue Sep 23, 2008 1:39 pm
Posts: 8
Ok, thanks for the links.

I will read and try to solve it in the simplest way i find, cause i am new with hibernate and there are still many aspects i don't know.

I will let you know when i find a solution and how i did it.
Thanks.

_________________
Sebastian


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.