-->
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: 2.1 & JCS & Session.load()
PostPosted: Wed Oct 08, 2003 6:07 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
Hi,

So I'm using the common ThreadLocal pattern in Tomcat. Each request gets its own Session, which is closed when the request terminates.

My application stores a persistent instance that was loaded with Session.load() on the Tomcat session. At the beginning of each request the instance is re-associated with the current request's session using:

Code:
session.load(entity, entity.getId());


This was working perfectly well until I introduced a JCS JVM-level cache. Now I get the error:

Code:
Caused by: net.sf.hibernate.HibernateException: Another object was associated with this id (the object with the given id was already loaded): [my.Class#100]
        at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1348)
        at net.sf.hibernate.impl.SessionImpl.update(SessionImpl.java:1247)


So, how do I check for this condition? And if it's already associated with the session, how do I get the instance that is associated with the session? I was previously checking

Code:
if (session.contains(instance))
skip ...


but this doesn't prevent the exception anymore.

Thanks,


Top
 Profile  
 
 Post subject: Okay
PostPosted: Thu Oct 09, 2003 11:40 am 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
In case anyone is interested this was the problem:

Of course Hibernate requires that there be only one java instance of a persistent entity per Hibernate session. Session.load(instance) will not always work well with this requirement because it is trying to bind another java instance to the persistent entity.

Before I added the cache, the order of events was:
1. re-associated the instance saved on the HTTP session with the persistent entity using Session.load(instance).
2. accessed the same entity as one member of a one-to-many lazy persistent collection

Since the Hibernate session controls the contents of a lazy persistent collection, this order was not creating another java instance of the same entity, it was only just returning the instance that had originally been on the HTTP session.

After I added the cache, the entity owning the lazy collection was saved on the cache and the object graph containing this entity happened to be accessed before I was re-associating the instance from the HTTP session with the current Hibernate session. Thus the order was:
1. accessed the same entity as one member of a one-to-many lazy persistent collection (from object graph stored in cache)
2. re-associated the instance saved on the HTTP session with the persistent entity using Session.load(instance)

This ordering is problematic because step 2 tries to associate another java instance with the persistent entity, which is illegal if there is a pre-existing java instance already so associated.

So, the solution I came up with is to re-associate the instance from the HTTP session this way:

Code:
instance = session.load(instance.getClass(), instance.getId());


which solved the previous problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 4:36 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
Just wondering, why are you using .load() instead of .update()? I thought update was the preferred method of reassociating an object with a session???


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 11, 2003 5:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You can use update or in 2.1 use the lock to associate without accessing the db.


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.