-->
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: Lazy loading with a disconnected session
PostPosted: Fri May 28, 2010 12:01 pm 
Newbie

Joined: Fri May 28, 2010 10:53 am
Posts: 2
Hey all,

As you all know, lazy loading requires an open and connected session. On the other hand, I don't want to keep open sessions more than I must and I close them as soon as I finish my transactions. This however causes problems with lazy loading.

I have several solutions but I can't understand why is this a problem in the first place. The method ThrowLazyInitializationExceptionIfNotConnected (in AbstractPersistentCollection) only checks to see if the session is disconnected, why does it not try to reconnect?

Or we could make this configurable...

Thanks,
Vadim.


Top
 Profile  
 
 Post subject: Re: Lazy loading with a disconnected session
PostPosted: Fri May 28, 2010 3:15 pm 
Regular
Regular

Joined: Tue May 11, 2010 5:50 pm
Posts: 54
Location: Norman, Ok, U.S.A
Try instead of opening a new session every time, call sessionFactory.getCurrentSession() and open a new transaction instead of a new session. Close the session after you think you have no use for an open session or at the time of the user closes the application and leave the session open until needed. If you want to manage connection and sessions to the database, look into C3P0.


Top
 Profile  
 
 Post subject: Re: Lazy loading with a disconnected session
PostPosted: Sat May 29, 2010 6:26 am 
Newbie

Joined: Fri May 28, 2010 10:53 am
Posts: 2
Hey.

Thanks for the reply. However, I think that you've missed the point.
I don't have a problem with creating too many sessions, I have a problem with keeping even one connected when it's not needed.

My point is this - why don't NHibernate sessions have the ability to reconnect on demand? You could configure how much idle time a session has before it will disconnect and each request will reconnect it. You could configure it according to your needs - a session to a local SQLite db may not be closed at all (idle time is infinity) while a session to a very loaded server could close immediately after each transaction (idle time is zero).

The best method I can see for implementing this is by creating a session proxy of a sort. A ReconnectingSession that will implement ISession and have some session implementation as a datamember that will do the actual work. Each check on whether the ReconnectingSession is open\connected will make it try to reconnect if needed and then report it's connected.
Only when it'll fail to reconnect will it say it is not connected. The ReconnectingSession could then schedule a disconnection.

I simply don't think this should be something the programmer has to worry about. What do you think? Am I missing something? Is there a better way to implement this? Is this already implemented?

Thank,
Vadim.


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.