Hello,
I have a problem with using Query Cache in my program, it puts queries into cache but always misses:
SecondLevelCacheHitCount: 100
QueryCacheHitCount: 0
QueryCacheMissCount: 11
QueryCachePutCount: 11
QueryExecutionCount: 11
My code looks like this:
Code:
part of unit test setup:
AnnotationConfiguration cfg = new AnnotationConfiguration();
factory = cfg.configure("hibernate-test.cfg.xml").buildSessionFactory();
part of test:
@Test
public void testGetAllCarMarks() {
populateCarMarks();
CarMarkDAO dao = new CarMarkDAO();
dao.setSessionFactory(factory);
//without container let us start DAO manually
try {
dao.initDao();
} catch (Exception e) {
e.printStackTrace();
fail();
}
List<CarMark> carMarks = dao.getAllCarMarks();
assertNotNull(carMarks);
assertEquals(10, carMarks.size());
for(int i = 0; i < 10; i++) {
dao.getAllCarMarks();
}
dao.getStatistics();
}
hibernate.cfg.xml:
<session-factory>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:mem:openidserver_db</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="cache.provider_configuration_file_resource_path">ehcache-test.xml</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
<!--
<property name="cache.use_structured_entries">true</property>
-->
<property name="generate_statistics">true</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="test.car.entities.CarMark" />
</session-factory>
ehcache config:
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false" />
</ehcache>
DAO code which is tested is based on Spring, I do not launch container but initDAO() with:
public class CarMarkDAO extends HibernateDaoSupport {
@Override
protected void initDao() throws Exception {
super.initDao();
getHibernateTemplate().setCacheQueries(true);
}
public List<CarMark> getAllCarMarks() {
List<CarMark> carMarks = getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(CarMark.class));
//List<CarMark> carMarks = getHibernateTemplate().find("from CarMark");
return carMarks;
}
}
I have also tried this in pure Hibernate also get misses so I think it's some bug with configuration or ... I don't know exactly.
I use hibernate 2.5.0-ga and ehcache 1.2.4 from Maven repositories.
Anybody have similar problem with put / miss on cache query and solved it?
Thx in advance,
Adr