-->
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: Collection associated with 2 open sessions - Best practice?
PostPosted: Tue Jan 18, 2005 12:36 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
I have a command that access an object and calls another command that accesses the same object. Each command has its own open session. This causes an "Illegal attempt to associate a collection with two open sessions" exception to be thrown for abvious reasons.

One way to solve this problem it to evict the object from the first session. I am trying to avoid evicting objects from sessions.

I tried to do only scalar queries in the first command, but I am still getting the exception. It seems that even scalar queries associate objects to the session.

Do the queries such as the following load the corresponding object:
"select count(*) from Object o where o.id=?" or
"select field from Object o where o.id=?"

What is the best solution to this exception without evicting the objects from the first session?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 1:54 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
The first question is why do you need two Hibernate Sessions? Both commands should use the same Session. I can't necessarily tell you what the best practice is in your case, but I can most assuredly tell you that using two Hibernate Sessions in the same user transaction is a "worst practice!" :)

If you find yourself needing a session in multiple service methods, you may want to investigate the "Thread Local" pattern or the "Open Session in View" pattern. These patterns make it very easy to just use one Session for a given interaction.

Here's some links:
http://www.hibernate.org/43.html
http://www.hibernate.org/42.html

Also, as you have found out, you cannot have a hibernate entity associated with more than one session. You also cannot have two instances of one entity associated with a single Hibernate session either. You have to evict the object before associating it with the session.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 2:19 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
Each command/session is associated with one transaction. The first command/session needs only to get some info about the object (this why I tried scalar queries) to decide whether to call the second command. If it needs to do so, then the second command loads and updates the object. After the second command commits the transaction, the first command reads the object and update it in a different transaction.

My question is do scalar queries associate the corresponding object with the current session. If so is there a way to a read of a field on the object without loading and associating the whole object with the current session. As I mentionned in my first post, evicting the object solves the problem. Is there any other way?


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.