-->
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.  [ 5 posts ] 
Author Message
 Post subject: Second-level cache and proxies
PostPosted: Wed Nov 05, 2008 3:21 pm 
Beginner
Beginner

Joined: Fri May 06, 2005 2:37 pm
Posts: 39
Using Hibernate 3.2.6, Ehcache 1.5.

I appear to have a problem where proxies retrieved via load() are being serialized into the second-level cache, corrupting it.

Code:
ERROR http-8080-17 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/].[spring] - Servlet.service() for servlet spring threw exception
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
        at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
        at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
        at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
        at citysearch.core.taxonomy.domain.Tag$$EnhancerByCGLIB$$c33d283f.equals(<generated>)
        at org.apache.commons.lang.builder.EqualsBuilder.append(EqualsBuilder.java:384)
        at citysearch.core.taxonomy.domain.Tag.equals(Tag.java:364)
        at org.hibernate.util.EqualsHelper.equals(EqualsHelper.java:10)
        at org.hibernate.type.AbstractType.isEqual(AbstractType.java:108)
        at org.hibernate.engine.TypedValue.equals(TypedValue.java:51)
        at java.util.AbstractMap.equals(AbstractMap.java:427)
        at org.hibernate.util.EqualsHelper.equals(EqualsHelper.java:10)
        at org.hibernate.cache.QueryKey.equals(QueryKey.java:70)
        at java.util.HashMap.getEntry(HashMap.java:349)
        at java.util.LinkedHashMap.get(LinkedHashMap.java:280)
        at net.sf.ehcache.store.MemoryStore.get(MemoryStore.java:130)
        at net.sf.ehcache.Cache.searchInMemoryStore(Cache.java:1172)
        at net.sf.ehcache.Cache.get(Cache.java:801)
        at net.sf.ehcache.hibernate.EhCache.get(EhCache.java:82)
        at org.hibernate.cache.StandardQueryCache.get(StandardQueryCache.java:105)


Basically, an object is being retrieved from the second-level cache, but it's a proxy (obtained from load()), and it cannot be initialized because it was from an earlier session. This appears to be a bug, but it is hard to replicate.

Has anyone else seen this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 3:57 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think the problem is with the Tag.equals() method. It should not have any sideeffects. Eg. The equals() method should not make any calls that may require initialization of proxies. There was a similar problem related to hash code posted some time ago: http://forum.hibernate.org/viewtopic.php?t=991432


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 4:24 pm 
Beginner
Beginner

Joined: Fri May 06, 2005 2:37 pm
Posts: 39
Ah, ok.

The equals() method is walking a tree of (parent) Tag objects, which would require their own proxies.

I would strongly prefer to keep that... what are my options at this point?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 5:41 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It should be safe to use the ID of the parent in equals(), since accessing the ID doesn't require proxy initialization. Eg. use getParent().getId().equals(....) instead of getParent().equals(...)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 5:54 pm 
Beginner
Beginner

Joined: Fri May 06, 2005 2:37 pm
Posts: 39
Sure, that's the short-term solution. Of course, that's not a business-relevant equals(), which I always prefer. I guess my only other option is to turn off lazy-loading of the parent relationship, although that has obvious performance questions.

Appreciate all the help. I was a little worried that this question wouldn't get noticed. Credit has been given.


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