-->
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: Problem introspecting second level cache
PostPosted: Thu May 26, 2005 8:53 pm 
Newbie

Joined: Wed Apr 27, 2005 10:22 pm
Posts: 8
I tried submitting this as a bug but it was rejected with an explanation that I was doing something wrong, so now I'm turning to the forums in the hope that someone can help me figure out a little bit more about what I might be doing wrong.

The issue is this: I'm trying to get the contents of a second level cache.
Here's my code:
Code:
      Map cacheEntries = sf.getStatistics()
            .getSecondLevelCacheStatistics("ACCESS_CONTROL")
            .getEntries();


Inside SecondLevelCacheStatistics.java, the following method is being called:
Code:
public Map getEntries() {
  Map map = new HashMap();
  Iterator iter = cache.toMap().entrySet().iterator();
  while ( iter.hasNext() ) {
    Map.Entry me = (Map.Entry) iter.next();
    map.put( ( (CacheKey) me.getKey() ).getKey(), me.getValue() ); // ClassCastException!
  }
  return map;
}

I'm receiving a ClassCastException on line 50 because, in addition to the keys in the map that are of type CacheKey, there are keys in the map that are of type QueryKey.

The comment that was added to my bug when I posted it said "Don't try to cache queries in a second-level cache region." The thing is, I'm not trying to cache queries in a second-level cache region. They're clearly there, but I haven't intentionally done anything to put them there. Does anyone have any idea how this might happen?

A little more info:
I have hibernate.cache.use_query_cache set to true in my hibernate cfg file. If I set it to false, the issue goes away. I'm using EHCache and I have the usage of the cache for my object set to read-only. I have not explicitly set any queries to use this cache region. I have only set use_query_cache to true and added the cache tag to my class in the hibernate mapping file.

Any thoughts about how I am misusing the second level cache would be greatly appreciated.

Thanks,
Max

Hibernate version:
3.0.4
Mapping documents:
<hibernate-configuration>
<session-factory name="HibernateSessionFactory">
<property name="show_sql">false</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_structure_entries">false</property>
<property name="hibernate.generate_statistics">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/max</property>
<property name="connection.username">max_user</property>
<property name="connection.password">max</property>
<property name="connection.isolation">4</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
</session-factory>
</hibernate-configuration>


From my hibernate mapping file:
<class name="GoblRoleHb" table="GoblRole">
<cache usage="read-only" region="ACCESS_CONTROL" />
<id name="goblRoleId" column="GoblRoleId" type="string">
<generator class="assigned"/>
</id>
</class>

Code between sessionFactory.openSession() and session.close():
My code is not executing inside a session, but I'm trying to access a second level cache so I shouldn't need to be.

Full stack trace of any exception that occurs:
java.lang.ClassCastException: org.hibernate.cache.QueryKey
at org.hibernate.stat.SecondLevelCacheStatistics.getEntries(SecondLevelCacheStatistics.java:50)
Name and version of the database you are using:
MySql 4.1.10


Top
 Profile  
 
 Post subject: Additional Information
PostPosted: Thu May 26, 2005 9:20 pm 
Newbie

Joined: Wed Apr 27, 2005 10:22 pm
Posts: 8
I've been stepping through the Hibernate code, trying to figure exactly how the QueryKey is making its way into my second level cache, and it's not sneaky at all. From Loader.java, lines 1506 through 1527
Code:
         QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() );
         Set filterKeys = FilterKey.createFilterKeys( session.getEnabledFilters(), session.getEntityMode() );
         QueryKey key = new QueryKey(
               getSQLString(),
               queryParameters,
               filterKeys,
               session.getEntityMode()
            );
         List result = null;

         if ( /*!queryParameters.isForceCacheRefresh() &&*/ session.getCacheMode().isGetEnabled() ) {
            result = queryCache.get( key, resultTypes, querySpaces, session );

            if (queryStatisticsEnabled) {
               if (result==null) {
                  factory.getStatisticsImplementor().queryCacheMiss( getQueryIdentifier(), queryCache.getRegionName() );
               }
               else {
                  factory.getStatisticsImplementor().queryCacheHit( getQueryIdentifier(), queryCache.getRegionName() );
               }
            }
         }


Unless I'm misreading, if the object is associated with a cache region and the cache is enabled, we get the cache from the session factory and, once the query has been executed, store the QueryKey in the cache. Am I somehow getting the wrong type of cache on line 1506?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 2:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Like I said in the JIRA issue, don't cache queries in the same region as you cache an entity!

I mean, this is trivial to avoid; you have to have explicity type Query.setCacheRegion(...) somewhere in your code to cause this problem to happen!


Top
 Profile  
 
 Post subject: yup
PostPosted: Fri May 27, 2005 7:38 pm 
Newbie

Joined: Wed Apr 27, 2005 10:22 pm
Posts: 8
Yup, trivial indeed. Another team member was setting the cache region of a query and I wasn't aware of it. Now it all makes sense. Sorry for the trouble, and thanks for the response.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 7:08 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
no, that does not fix it. There appears to be a problem here.


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.