-->
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.  [ 6 posts ] 
Author Message
 Post subject: Threading issue when using GetCurrentSession()
PostPosted: Wed Nov 15, 2006 5:35 pm 
Newbie

Joined: Fri Jul 28, 2006 10:53 am
Posts: 15
In my Hibernate 3.1 web application, I'm allowing the user to save to the database in one session while a background thread is reading data from the database in a different session. I'm using hibernate.current_session_context_class="thread" and GetCurrentSession() to manage my sessions, guaranteeing that each thread has its own session. In theory I thought that would work fine, but I'm encountering strange problems. The saving works, but right after it's done then the background thread tries to update the same objects that were just saved by the saving thread (as evidenced by duplicate HQL statements in the log), even though it never even touches those objects! I've also gotten some NonUniqueObjectException and StaleObjectExceptions that are probably related to that problem too. How can this be, and how can I fix it? It doesn't make any sense...

Any help would be much appreciated! I'm at my wit's end :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 4:26 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Hi,

The first thing I'd do would be to display the session memory address to check if you're right thinking your threads are sharing the same session.

If so, then you will certainly have to recheck your conf to see why two different threads get the same session.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 1:50 pm 
Newbie

Joined: Fri Jul 28, 2006 10:53 am
Posts: 15
It looks like both sessions have different memory addresses, so that's not the problem.

Any other ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 4:07 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You're not using second level cache ? What is doing your background thread exactly ? How long do you keep your session objects ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 4:32 pm 
Newbie

Joined: Fri Jul 28, 2006 10:53 am
Posts: 15
No second-level caching is enabled. The background thread just calls GetCurrentSession, begins a transaction, runs some queries to get some data, commits the transaction, and ends. I don't explicitly manage my Session objects, I just call GetCurrentSession whenever I need one.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 4:56 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, I never got NonUniqueObjectException without running in the same session :-/. It was always due to the creation of objects with same id from the proxied objects.

Did you try and flush and clear your session before commiting, so as to be sure there's no more data in it.

When calling getCurrentSession() after doing a transaction, I suppose you get a new Session ? Maybe you should check it too?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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