3

I am trying to verify if my ehcache configuration is working properly. So I put this code before and after the select. And my app execute the select all times for the same row... I did all my configuration using this tutorial. https://balamaci.wordpress.com/2009/12/07/caching-with-ehcache-part-i/

String msg = "select blockIscsi: " + storage.getStorageIndex();
System.out.println(msg);
blockIscsi = blockIscsiDAO.getByKey(storage.getStorageIndex(), Long.valueOf(storage.getPartitionId()));
System.out.println("done!");


select blockIscsi: 757
Hibernate: select this_.STORAGEINDEX as STORAGEI1_32_0_, this_.PARTITIONID as PARTITIO2_32_0_, this_.BLOCK_STATUS as BLOCK3_32_0_, this_.BLOCK_TYPE as BLOCK4_32_0_, this_.USTORE_ID as USTORE5_32_0_ from blocks this_ where this_.STORAGEINDEX=? and this_.PARTITIONID=?
done!
select blockIscsi: 757
Hibernate: select this_.STORAGEINDEX as STORAGEI1_32_0_, this_.PARTITIONID as PARTITIO2_32_0_, this_.BLOCK_STATUS as BLOCK3_32_0_, this_.BLOCK_TYPE as BLOCK4_32_0_, this_.USTORE_ID as USTORE5_32_0_ from blocks this_ where this_.STORAGEINDEX=? and this_.PARTITIONID=?
done!
select blockIscsi: 757
Hibernate: select this_.STORAGEINDEX as STORAGEI1_32_0_, this_.PARTITIONID as PARTITIO2_32_0_, this_.BLOCK_STATUS as BLOCK3_32_0_, this_.BLOCK_TYPE as BLOCK4_32_0_, this_.USTORE_ID as USTORE5_32_0_ from blocks this_ where this_.STORAGEINDEX=? and this_.PARTITIONID=?
done!

EDIT: appContext.xml:

<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
        <prop key="hibernate.show_sql">false</prop>
        <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
        <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->
        <!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory 
            </prop> -->
        <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
        </prop>
        <!-- enable second level cache and query cache -->
        <prop key="hibernate.cache.use_second_level_cache">true</prop>
        <prop key="hibernate.cache.use_query_cache">false</prop>
        <prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>

        <prop key="hibernate.jdbc.batch_size">20</prop>
        <prop key="hibernate.jdbc.fetch_size">25</prop>
        <prop key="hibernate.order_inserts">true</prop>
        <prop key="hibernate.order_updates">true</prop>
        <prop key="hibernate.jdbc.batch_versioned_data">true</prop>
        <prop key="hibernate.hbm2ddl.auto">none</prop>

        <prop key="hibernate.c3p0.min_size">2</prop>
        <prop key="hibernate.c3p0.max_size">100</prop>
        <prop key="hibernate.c3p0.timeout">100</prop>
        <prop key="hibernate.c3p0.max_statements">0</prop>
        <prop key="hibernate.c3p0.maxIdle">-1</prop>
        <prop key="hibernate.c3p0.idle_test_period">100</prop>
        <prop key="hibernate.c3p0.acquire_increment">1</prop>
        <prop key="hibernate.c3p0.unreturnedConnectionTimeout">30</prop>
        <prop key="hibernate.c3p0.debugUnreturnedConnectionStackTraces">false</prop>
    </props>
</property>

ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">

    <diskStore path="user.dir/ehcache" />

    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
        timeToIdleSeconds="0" timeToLiveSeconds="1800" diskSpoolBufferSizeMB="80"
        maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>

    <cache name="blockiscsi" maxEntriesLocalHeap="10000" eternal="false"
        timeToIdleSeconds="5" timeToLiveSeconds="10">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache name="org.hibernate.cache.internal.StandardQueryCache"
        maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
        maxEntriesLocalHeap="5000" eternal="true">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache>

DAO:

public BlockIscsi getByKey(Long id, Long partitionId) {
Session session = null;
BlockIscsi blockIscsi = null;
try {
    session = currentSession();

    Criteria criteria = session.createCriteria(BlockIscsi.class);
    criteria.add(Restrictions.eq("id", id));
    criteria.add(Restrictions.eq("partitionId", partitionId));
    criteria.setCacheable(true);
    criteria.setCacheRegion("query.blockiscsi");

    blockIscsi = (BlockIscsi) criteria.uniqueResult();

} catch (GenericJDBCException e) {
    e.printStackTrace();
} catch (NullPointerException e) {
    e.printStackTrace();
} finally {
    session.close();
}
return blockIscsi;
}
Felipe Gutierrez
  • 525
  • 1
  • 9
  • 20
  • 1
    Are you sure you have set ehcache logging specifically? http://stackoverflow.com/a/10705998/16959 – Jason Sperske Jan 05 '15 at 18:18
  • I add this and I got it on my log: 15:44:55 DEBUG main ehcache.Cache - Initialised cache: blockiscsi. Does it say ehcache is working for blociIscsi class? But why may queries keep executing for the same ID 757 in my example? – Felipe Gutierrez Jan 05 '15 at 18:46
  • I think the Hibernate select statement logs are just showing the referenced prepared statement, the actual database is not necessarily being hit. The only way to really know for sure that I can think of is to look at a log for your database. – Jason Sperske Jan 05 '15 at 19:02
  • so... I enable de mysql log and I can see all the selects for the same ID, as showing at hibernate log. There is a problem at my ehcache configuration. – Felipe Gutierrez Jan 05 '15 at 20:30
  • That is an interesting question then :) still looking into it – Jason Sperske Jan 05 '15 at 22:12
  • Please provide the configuration you are using and the versions of Ehcache and Hibernate. – Louis Jacomet Jan 08 '15 at 08:25

1 Answers1

0

You have disabled query caching:

<prop key="hibernate.cache.use_query_cache">false</prop>

This means individual entities get cached (since your second level cache is on), but the result of your custom query (since you are doing a Criteria query, and not a Session.get()) is not cached. For that reason Hibernate has to go to the database and execute the query.

You need to enable both the query cache (to remember the identifiers of the entities returned by the query) and the second level cache (to remember the entities themselves).

See the Hibernate docs for more information: https://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch06.html

MikeN
  • 887
  • 5
  • 18
  • no, I changed this hibernate.cache.use_query_cache to true and still not working. I cant understand why it still execute the query. Maybe it is something at my setCacheRegion – Felipe Gutierrez Jan 08 '15 at 18:58
  • You can just remove the setCacheRegion, and only keep the defaultCache in the ehcache.xml. It should then automatically create the necessary caches from the default config. You can also set up some logging (Hibernate4 uses JBoss logging) to see what's going on. – MikeN Jan 08 '15 at 20:02