I've been looking for the answer for few days now, wasting my weekend on this, and the fact that deep digging in google didn't find any answers means that this is probably something stupid on my part.
I have secend level cache enabled in Hibernate but it always goes to the database to load the object.
Cache debug shows entries as follows, for any transaction, not just the ones that involve the objects marked for caching:
Code:
16:28:33,767 DEBUG SessionImpl:422 - after transaction completion
16:28:33,845 DEBUG SessionFactoryImpl:830 - evicting second-level cache: com.dummy.data.Network
16:28:33,845 DEBUG SessionFactoryImpl:830 - evicting second-level cache: com.dummy.data.CardStatus
16:28:33,845 DEBUG SessionImpl:273 - closing session
When loading the object (via session.load()) EhCache shows:
Code:
16:32:42,931 DEBUG EhCache:68 - key: com.dummy.data.Network#3
16:32:42,931 DEBUG EhCache:77 - Element for com.dummy.data.Network#3 is null
--> loads data from DB here
Code:
16:32:42,931 DEBUG ReadOnlyCache:58 - Caching: com.dummy.data.Network#3
This happens every time, ie it looks like the object is always put to the cache but never found in the cache.
My theory is that Hibernate evicts my objects from second level cache after each session is closed, basically clearing the cache. But then what's the point of the second level cache? It then just works like first level cache. I've grepped all the source code I have and didn't find any calls to Evict anywhere.
I've changed the EhCache to HashTable provider and I've got the same results.
The Network object is very simple, I am using it as a test case. I've also cut down all the code to the minimum usable and still get this behavior.
Here is the relevant Hibernate config:
Code:
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">true</property>
<property name="generate_statistics">false</property>
<property name="max_fetch_depth">2</property>
<property name="default_batch_fetch_size">16</property>
<property name="use_streams_for_binary">true</property>
<property name="connection.isolation">2</property>
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">300</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<mapping resource="com/dummy/data/Network.hbm.xml" />
<class-cache class="com.dummy.data.Network" usage="read-only"/>
</session-factory>
</hibernate-configuration>
Network:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.dummy.data.Network" table="network" mutable="false">
<id name="networkId" type="int">
<generator class="assigned" />
</id>
<property name="timeZoneName" type="string" length="50" not-null="true" />
<property name="cutoffHour" type="int" not-null="true" />
<property name="cutoffMinute" type="int" not-null="true" />
<property name="name" type="string" not-null="true" unique="true" />
</class>
</hibernate-mapping>
And finally the test code I am using:
Code:
Transaction tx=session.beginTransaction();
Network net = (Network)session.load(Network.class, 3);
info("net loaded"+net.getName());
tx.commit();
session.close();
And finally the ehcache config:
Code:
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
/>
<cache name="com.dummy.data.Network"
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="600"
timeToLiveSeconds="1200"
overflowToDisk="false"
/>
</ehcache>
Any help would be welcome, I am going crazy in here :-)