-->
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.  [ 10 posts ] 
Author Message
 Post subject: Illegally attempted to associate a proxy with two open Sessi
PostPosted: Thu Jan 17, 2008 5:32 pm 
Newbie

Joined: Thu Jun 14, 2007 3:46 am
Posts: 17
How Do I Solve this kind of exception: Illegally attempted to associate a proxy with two open Sessions.

For each thread i have a own session. Must i go work with versioning?


Top
 Profile  
 
 Post subject: Re: Illegally attempted to associate a proxy with two open S
PostPosted: Fri Feb 01, 2008 2:39 am 
Newbie

Joined: Fri Feb 01, 2008 2:36 am
Posts: 1
DaveSmits wrote:
How Do I Solve this kind of exception: Illegally attempted to associate a proxy with two open Sessions.


Great question - I'm having the same problem. My unit tests are failing with this error, but it doesn't always happen consistently.

I'm using the Castle.Facilities.NHibernateIntegration.NHibernateFacility to manage sessions.

-dave


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 25, 2008 3:45 pm 
Newbie

Joined: Wed Oct 04, 2006 2:29 pm
Posts: 4
Did you figure out a good way to solve this? I am having the same problem!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 25, 2008 6:44 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
If I'm not mistaken, this exception is caused if you try to pass an entity that is persistent in one session to another session associated with the same SessionFactory.

If you have a separate session per thread then it sounds like you are passing entities between your threads. If you need to do this you should evict the object from the first session before passing it to the second session.

Cheers,

Symon.

_________________
Symon Rottem
http://blog.symbiotic-development.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 11:30 am 
Newbie

Joined: Wed Oct 04, 2006 2:29 pm
Posts: 4
Thank you for you answer!

Actually my problem is the following. I have a multi threaded application. Therefore concurrent threads need access to the NHibernate Sessions. I tried using one Session, reconnecting and disconnecting but this caused the threads to wait for access to the single Session.

The solution was to create a Session for every transaction and release it right after the end of the transaction.

The problem with this is that I can't use lazy load, since an entity is never associated with a Session.

What is the best way to create Sessions in an application like this?

Thanks again!

Rogerio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 6:39 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
ISessions are explicitly not thread safe. There are loads of discussions about the way to manage sessions on the web and this forum; personally I prefer very short session lifetimes and specifying what you want up front. So one approach would be to use very short session lifetimes and specify what you want before releasing the session. Obviously it kind of depends on whether true lazy loading is useful to you; in our case we're generally able to specify up front which bits of the model we're likely to need in a given case.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 11:02 am 
Newbie

Joined: Wed Oct 04, 2006 2:29 pm
Posts: 4
Thanks Steve,

Do you have any reference of where to read more about how to manage sessions?!

Thanks again!

Rogério


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 1:56 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
No; I've pieced things together from Google - do a search for 'manage hibernate sessions' or something similar. There are a load of blog entries on how different people have gone about doing this. It seems to be the biggest single question people ask so perhaps at some point there'll be some official documentation on it.

I think many peoples' usage is slightly different from others. Creating sessions isn't expensive, so the simplest way to solve your problem would be to create a new one whenever you need one. If you need them to stick around (maybe keep a session per thread over the lifetime of the application, and connect/disconnect as you've already tried), how about (sort of psuedo code):

Code:
IDictionary<int, ISession> sessions;
ISession SessionForCurrentThread
{
   get
   {
        if (!sessions.ContainsKey(CurrentThread.ManagedThreadId))
        {
           sessions[CurrentThread.ManagedThreadId] = sessionFactory.CreateSession....
        }
        return sessions[CurrentThread.ManagedThreadId];
   }

}


You'd probably want to add some thread safety to that. HTH.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 12:11 pm 
Newbie

Joined: Wed Oct 04, 2006 2:29 pm
Posts: 4
Hi Steve,

I had tried something like what you suggested. But then I get into the problem of having one entity associated with more than one ISession.
I guess the ISession management depends too much on how the application is designed.

Thank you for your quick answers!

Rogério


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 1:24 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
That sounds really dangerous - editing the same thing from multiple threads is always going to cause you problems, even without Hibernate. Without really knowing what you're trying to achieve I don't know if I can help much more. If the threading is because you have CPU intensive processing tasks I'd suggest doing the DB access in one thread to simplify things. you can't use one ISession from multiple threads (without locking) and you can't use the same entity in multiple ISessions so you're kind of between a rock and a hard place.


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