How to verify my ehcache configuration is working properly?

admin

Administrator
Staff member
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. <a href="https://balamaci.wordpress.com/2009/12/07/caching-with-ehcache-part-i/" rel="nofollow">https://balamaci.wordpress.com/2009/12/07/caching-with-ehcache-part-i/</a>

Code:
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:

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

        &lt;prop key="hibernate.jdbc.batch_size"&gt;20&lt;/prop&gt;
        &lt;prop key="hibernate.jdbc.fetch_size"&gt;25&lt;/prop&gt;
        &lt;prop key="hibernate.order_inserts"&gt;true&lt;/prop&gt;
        &lt;prop key="hibernate.order_updates"&gt;true&lt;/prop&gt;
        &lt;prop key="hibernate.jdbc.batch_versioned_data"&gt;true&lt;/prop&gt;
        &lt;prop key="hibernate.hbm2ddl.auto"&gt;none&lt;/prop&gt;

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

ehcache.xml:

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

    &lt;diskStore path="user.dir/ehcache" /&gt;

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

    &lt;cache name="blockiscsi" maxEntriesLocalHeap="10000" eternal="false"
        timeToIdleSeconds="5" timeToLiveSeconds="10"&gt;
        &lt;persistence strategy="localTempSwap" /&gt;
    &lt;/cache&gt;

    &lt;cache name="org.hibernate.cache.internal.StandardQueryCache"
        maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120"&gt;
        &lt;persistence strategy="localTempSwap" /&gt;
    &lt;/cache&gt;

    &lt;cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
        maxEntriesLocalHeap="5000" eternal="true"&gt;
        &lt;persistence strategy="localTempSwap" /&gt;
    &lt;/cache&gt;
&lt;/ehcache&gt;

DAO:

Code:
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;
}