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