-->
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: HTTPSession LazyInitializationException or Illegal attempt..
PostPosted: Fri Nov 10, 2006 3:28 pm 
Newbie

Joined: Tue Nov 07, 2006 5:34 pm
Posts: 3
Hi,

Following the one-session-per-request pattern I have a ServletFilter which closes the session after a request. The session factory uses threadlocal.

I have trouble retreiving a persisted object from HTTPSession. The first exception I encoutered was the LazyInitializationException. Okay, storing the object in one session (i.e. first request) and retreiving its collection in another (i.e. second request) doesn't work.

I need to update the object first. And this is the problem. Calling session.saveOrUpdate(instance) as well as session.lock(instance, LockMode.NONE); results in the following error, if I click on the link multiple times (i.e. having more the one thread accessing the object):

Code:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
   org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
   org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:40)
   org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
   org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
   org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
   org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:267)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:216)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
   org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
   org.hibernate.impl.SessionImpl.saveOrUpdate...

Further reading the documentation and understanding that I can't load two intances of the same object in one session, I figured that I need to call merge() instead. However, doing this brings the LazyInitializationException back into place.

What ist the best strategy to store/load persisted objects in HttpSession?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 4:22 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It is better to store object id in httpsession scope, load object and put it to request scope. If reloading is too slow then use global cache. You can make it transparent for application using httpsession wrapper. It helps to avoid many issues.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 4:52 pm 
Newbie

Joined: Tue Nov 07, 2006 5:34 pm
Posts: 3
I thought about storing the object id and reloading before. I didn't like the idea of just getting an id form httpsession instead of an object (like i used to).

However, I didn't consider writing a wrapper. I think that + caching will fit my needs nicely, thanks!


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.