-->
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.  [ 4 posts ] 
Author Message
 Post subject: Failed to lazily initialize collection - no session
PostPosted: Fri Jul 29, 2005 1:05 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I am using ASP.net with the session-per-request pattern.

I found the need to implement lazy loading, which isn't a problem for most of my app except for in 2 places where I have a wizard-style interface. (multi-page transaction). The lazy load fails here because I need the same session that the original object was created on, but in this case it's on a separate page so the session is gone.

What I did was add a method to by base class to allow to mark an object as "Floating" which basically puts the current ISession into HttpSession. The session-per-request pattern is now bypassed and the site works on session-per-httpsession pattern (until I tell it to stop). This part works when lazy loading is turned off... the site behaves exactly as before.

When I turn lazy loading back on, I get the same error, only this time I KNOW that the current ISession is the same as the one that the object was loaded on.

Can anyone help me figure out what is wrong? Without lazy loading my entire object graph is loaded into memory, which will be a problem in production.

TIA.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 1:57 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
just a thought -- will a lazy loaded collection RECONNECT a session that is open but disconnected?

I know it won't work if the session is closed, but I am disconnecting the session after a save/update/delete, so this may be the cause of the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 2:39 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 3:22 pm
Posts: 38
Location: Menlo Park, CA
I think you'll run into threading issues if you leave the ISession in the HttpSession, even if you Reconnect() and Disconnect().

A couple of suggestions:

1) You can always hack around the lazy-load issue if you know ahead of time what parts you're going to need to load by accessing them at the time you first load the object.

Parent p = session.Get(typeof(Parent), parentID);
int throwaway = p.Children.Count; // force lazy-load of Children collection.

2) If you're not editing the object that is giving you the lazy-load issue in your multi-page transaction, you can just do a session.Refresh() on the object in question. This will reload its state from the DB and reassosciate it with the session.

3) If you're dealing with a bi-directional assosciation, you can work with the child objects alone and then just refresh the Parent object at the end.

i.e. instead of...
Parent p = session.Get(typeof(Parent), parentID);
// next request
p.AddChild(newChild); // this would give you the lazy-load exception

you do...
Parent p = session.Get(typof(Parent), parentID);
// next request
Child newChild = new Child();
newChild.Parent = p;
// do not add newChild to p.Children
// next request
session.Save(newChild);
session.Refresh(p); // p.Children now includes newChild

4) Look into ISession.SaveOrUpdateCopy(). I've never used it and I'm not sure precisely what it does, but it might have something to do with a solution to your problem.

OT: Are you from Ars?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 3:12 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Threading issues? why?

(I'm not really worried about this because it's an admin interface not a public page... only 1 user at a given time).

As of right now I'm getting the no session error if I try what you suggest at # 1). I'm not sure why. When I access the collection property, I check the current session and IsConnected is true, and IsOpen is false... this is quite odd!

2) I am editing the parent object, so I don't think this will work for me.

Thanks for the reply.

OT: what's ars?


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