jakarta-jcs-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aaron Smuts <aasm...@wisc.edu>
Subject RE: CacheElement hashCode problem
Date Thu, 13 Nov 2003 20:02:55 GMT
I'm having a hell of a time figuring out what is going on here.  Is
hibernate storing the QueryKey object as the key in the cache, the naem
by which the value is referenced?  It looks like QueryKey has a ton of
data in it. 

The QueryKey class override the default equals method, which the
hashtable or map implementation in java uses.  

They should be calling object.class.isInstance() before casting, but
that's not the main problem.  

Is the region in which the QueryKey objects are put home to any other
types of objects?  If so then that would account for the failure to
cast.  I need to look into the hashtable implementation further.

Can you remove any of these hibernate objects?  I suspect not.

The expiration removal should work exactly like other kinds of remove
requests.

Every object in the cache is wrapped in a CacheElement.  That's not a
problem.  The CacheElement keeps all the information about an element.
If the QueryKey is an object, then it is wrapped.  No big deal.  

The problem is probably in that hibernate is putting a big object that
overrides the equals method into the cache as the key.  This is fine but
potentially inefficient.  I can say much about this since I don't know
exactly how hiberate uses JCS.  I may be wrong about the use of QueryKey
as a key.  If I'm right, I'd like to know what the object looks like.
Please tell me if you know.

The problem is probably in overriding the equals method and then
expecting that the object will only be put in a collection with similar
objects.  This is a good way to sneak in bugs.  

Hibernate should just add a check to see if the object is of the same
type, to see if it isInsatnce().  Then if it is not, it should return
false.  Otherwise it is a faulty implementation of equals.

Modify the hibernate class and test.  Start the equals method in
QueryKey with something like this:

  public boolean equals(Object other) {
    boolean typeCheck = other.class.isInstance( QueryKey );
    if ( !typeCheck ) {
      return false;
    }
		QueryKey that = (QueryKey) other;

	. . . .

If that works, tell the hibernate folks to patch that class.

Cheers,

Aaron

> -----Original Message-----
> From: Ryan Breidenbach [mailto:breidenr@michaels.com]
> Sent: Thursday, November 13, 2003 1:20 PM
> To: Turbine JCS Users List
> Subject: Re: CacheElement hashCode problem
> 
> Here is the portion of the stacktrace you are probably most interested
> in:
> 
> [ERROR] 13:13:14 org.apache.jcs.engine.control.CompositeCache.get -
> java.lang.ClassCastException
>         at net.sf.hibernate.cache.QueryKey.equals(QueryKey.java:35)
>         at java.util.Hashtable.remove(Hashtable.java:434)
>         at
>
org.apache.jcs.engine.memory.lru.LRUMemoryCache.remove(LRUMemoryCache.ja
va
> :274)
>         at
>
org.apache.jcs.engine.control.CompositeCache.remove(CompositeCache.java:
72
> 7)
>         at
>
org.apache.jcs.engine.control.CompositeCache.remove(CompositeCache.java:
69
> 0)
>         at
>
org.apache.jcs.engine.control.CompositeCache.get(CompositeCache.java:490
)
>         at
>
org.apache.jcs.engine.control.CompositeCache.get(CompositeCache.java:440
)
>         at org.apache.jcs.access.CacheAccess.get(CacheAccess.java:236)
>         at net.sf.hibernate.cache.JCSCache.get(JCSCache.java:30)
>         at net.sf.hibernate.cache.QueryCache.get(QueryCache.java:61)
>         at
> net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:964)
>         at
net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1483)
> 
> 
> Here is my cache.ccf file:
> 
> jcs.default=
>
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttribut
es
> jcs.default.cacheattributes.MaxObjects=100
>
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory
.l
> ru.LRUMemoryCache
> jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
> jcs.default.elementattributes.IsEternal=false
> jcs.default.elementattributes.MaxLifeSeconds=15
> 
> 
> Here is a snippet of code that is using Hibernate with caching:
> 
>     public List getAllActiveContainerTypes() {
>         final String queryString =
>                 "from ContainerType containerType where
> containerType.status = 'ACTIVE'";
>         return getHibernateTemplate().executeFind(new
> HibernateCallback() {
>             public Object doInHibernate(Session session) throws
> HibernateException, SQLException {
>                 Query query = session.createQuery(queryString);
>                 query.setCacheable(true);
>                 return query.list();
>             }
>         });
>     }
> 
> 
> Here is where the QueryKey class is breaking:
> 
> 	public boolean equals(Object other) {
> 		QueryKey that = (QueryKey) other;
> 		...
> 
> This object passed in is a CacheElement. Where is is cast to a
QueryKey,
> the exception is thrown. I have attached the code for the entire
class,
> but I don't think it will be useful.
> 
> Thanks for the help.
> 
> Ryan
> 
> 
> Aaron Smuts wrote:
> >
> > Can you send me a stack trace also.
> >
> > > -----Original Message-----
> > > From: Ryan Breidenbach [mailto:breidenr@michaels.com]
> > > Sent: Thursday, November 13, 2003 11:51 AM
> > > To: turbine-jcs-user@jakarta.apache.org
> > > Subject: CacheElement hashCode problem
> > >
> > > I am using JCS with Hibernate for caching, and I came across a
> > problem.
> > > When JCS encounters an object whose MaxLifeSeconds has been
exceeded,
> > I
> > > am getting a ClassCastException. This is actually being thrown by
the
> > > net.sf.hibernate.cache.QueryKey, but I think the root of the
problem
> > is
> > > the hashCode method of the CacheElement class:
> > >
> > >     public int hashCode()
> > >     {
> > >         return key.hashCode();
> > >     }
> > >
> > > Why does it do this? The problem happens when there is a QueryKey
> > object
> > > *and* and CacheElement object that wraps a QueryKey object both in
the
> > > cache (Hashtable). This doesn't seem right. Any thoughts?
> > >
> > > Ryan
> > >
> > >
---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > turbine-jcs-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail:
> > turbine-jcs-user-help@jakarta.apache.org
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
turbine-jcs-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: turbine-jcs-user-
> help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-user-help@jakarta.apache.org


Mime
View raw message