-->
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.  [ 13 posts ] 
Author Message
 Post subject: could not initialize proxy even with session being open
PostPosted: Mon Dec 12, 2005 6:40 pm 
Beginner
Beginner

Joined: Tue Dec 28, 2004 11:30 am
Posts: 32
Hey guys I have the following Object graph

Under what conditions would an object in a list not initialize and throw the follwoing

2005-12-12 16:45:31,204 [http-8080-Processor23] ERROR org.hibernate.LazyInitializationException.<init>: 19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed

I do the following :
0. I have a detached Object
1. Open a new hibernate session
2. reatach the detached object with the session by doing this

s.refresh( object );
s.lock( object, LockMode.NONE );

3. and then try to navigate the graph to get to an object in a list

So session is open the main object is in the session and yet i cannot
get to some areas in the object graph ??

Any thoughts ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 3:57 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Makw sure this stuff is thread safe.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 9:49 am 
Beginner
Beginner

Joined: Tue Dec 28, 2004 11:30 am
Posts: 32
THank you for the response.

I think i am already doing that . I use the thread local to store my session
the way that is implemeted is pretty much the same way as described in Hibernate in Action book page 301 .

I do use a Filter too , My application uses struts + hibernate and deployed
under tomcat 5.5.12 Java 5.


protected static ThreadLocal sessionHolder = new ThreadLocal();
....
public static Session getCurrentDBSession() throws ScapeException
{
Session sess = (Session) sessionHolder.get();
try
{
if ( sess == null )
{
sess = getSessionFactory().openSession();
sessionHolder.set( sess );
}
}
catch ( HibernateException e )
{
}
return sess;
}

public static SessionFactory getSessionFactory()
{
ApplicationProperties props = ApplicationProperties.getInstance();
SessionFactory factory =
(SessionFactory) props.getApplicationProperty( HIBERNATE_FACTORY );
return factory;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 10:09 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Yes, it must be thread safe. It looks like you are misusing "refesh"

Quote:
public void refresh(Object object)
throws HibernateException

Re-read the state of the given instance from the underlying database. It is inadvisable to use this to implement long-running sessions that span many business tasks. This method is, however, useful in certain special circumstances. For example
where a database trigger alters the object state upon insert or update
after executing direct SQL (eg. a mass update) in the same session
after inserting a Blob or Clo


Try to relaod object by ID (implement some interface for all persistent classes to obtain it), it must work without problems.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 10:18 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
BTW I do not see how you detache objects, if you store it in httpsession then you must serialize access to httpsession anyway (it happens if user refreshes page before to recieve response)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 6:06 pm 
Beginner
Beginner

Joined: Tue Dec 28, 2004 11:30 am
Posts: 32
Thank you baliukas for your responses .

BTW I do not see how you detache objects, if you store it in httpsession then you must serialize access to httpsession anyway (it happens if user refreshes page before to recieve response)

I use the Filter mechanism So everytime the response is returned i close the session. So now i have a detached object and yes i do store the object in the Http Session.
then if in the next request response loop i need to use the object i call on it
s.refresh( object );
s.lock( object, LockMode.NONE );

where i know that the object hasn't changed so i refresh it and then just put it in the session. So yes i will try removing the call to refresh and see what happens

I do not undrestand what u mean by you must serialize access to httpsession ?


try to relaod object by ID (implement some interface for all persistent classes to obtain it), it must work without problems.

Do you mean use Session.load(Class theClass, Serializable id)
instead of
s.refresh( object );
s.lock( object, LockMode.NONE );


Again thanks for the help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 2:39 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Yes, just try to remove "s.refresh( object ); " first as I understand documentation it is not designed for detached instances and it does not cascade.
Next problem is persistent objects in http session, it must throw "Illegal attemt to associate object with two open sessions" (two concurrent threads attemt to attach object ) Workaround is to use some session scope attribute to detect concurrent session access serialization/synchronization is application specific, but application designed this way is a DOS attack victim anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 3:09 pm 
Beginner
Beginner

Joined: Tue Dec 28, 2004 11:30 am
Posts: 32
Thank you Baliukas for your suggestions .

But doing only this
session.lock( object, LockMode.NONE );

resulted in the following exception
org.hibernate.HibernateException: reassociated object has dirty collection reference (or an array)

Although the object which is stored in a POJO is not being modified. We do the following

-open session
- execute the query to get the objects
- object loads with couple lists lazilly initialized
- We display some of these lists on jsp.
-close session

-open session
-try to initialize a single object with the new session
session.lock( object, LockMode.NONE );
and hence the exception.

It seems that i am doing something awfully wrong here cause this shouldn't be this tough

Reading the Session.lock(Object object,LockMode lockMode)
simply reassociate a transient instance with a session (LockMode.NONE)

Now according to my undrestanding
- Transient Object : means the object isn't associated with any database table row; object was instantiated using the new operator.

- Persistent Object : a persistance instance is an instance with a database identity; persistent instances are always associated with a Session and are transactional

-Detached Object : Is a persistant instance that has lost association with the persistence manager( session)

Now the above said the lock definition doesn't seem to make sense when we are talking about detached objects is that true ?

THank you again


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 2:51 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It must be better to store object id in http session (or url parameter if possible), "Detached Object " doe's not make sence if you need to refresh before to attach. It must help to avoid many problems.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 2:30 pm 
Beginner
Beginner

Joined: Tue Dec 28, 2004 11:30 am
Posts: 32
Well the following seems to have solved my problem

instead of calling
session.refresh(object);
session.lock(object, lockmode.NONE) ;

on the object which was resulting in exceptions I call

so = (ScapeObject)s.merge(object);
and use the new object as my persistant object and get rid of
the old one.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 2:32 pm 
Beginner
Beginner

Joined: Tue Dec 28, 2004 11:30 am
Posts: 32
Well i guess my open question would be why doesn't

session.lock(object, lockmode.NONE); work


Thank you for the help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 4:03 pm 
Beginner
Beginner

Joined: Thu Jul 08, 2004 2:21 pm
Posts: 20
Location: Toronto
kkrikor wrote:
Well i guess my open question would be why doesn't

session.lock(object, lockmode.NONE); work

It is a legimate question indeed.
And according to API docs:
Code:
public void lock(Object object,
                 LockMode lockMode)
          throws HibernateException

Obtain the specified lock level upon the given object. This may be used to perform a version check (LockMode.READ), to upgrade to a pessimistic lock (LockMode.UPGRADE), or to simply reassociate a transient instance with a session (LockMode.NONE). This operation cascades to associated instances if the association is mapped with cascade="lock".


As you can see doc's state that operation cascades to associated instances, but I can't find how to map association with cascade="lock" ...

I belive that it is a bug that lock() does not associate object tree with given session.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 11:00 pm 
Beginner
Beginner

Joined: Tue Dec 28, 2004 11:30 am
Posts: 32
Revisiting this ,

Well as you point out the definition in the api. I think you are not supposed to use this method to associate a detached object with the session.

they say
simply reassociate a transient instance with a session
and according to the documentation there is a big difference betweeen a
detached object and a transient object.

What i do now with the detached objects is the following,
0. session opens
1. A query gets me a list of objects
2. session closes
3. i choose the object i want to use
4. open session 2
5. and call Session.get on that object to reload its state
6....


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