Note that there are some explanatory texts on larger screens.

plurals
  1. POCaching not working with Spring 3.0, Hibernate 3.6, and EhCache 2.6.6
    text
    copied!<p>We're trying to get caching working in our app and are having a very hard time getting it to actually work. We don't have a very complex configuration, at least for this part of the application, so it's really frustrating and I finally have to throw myself on the tender mercies of the StackOverflow community.</p> <p>First, for now, we're stuck at Spring 3.0 and Hibernate 3.6, so using the new Spring 3.1 @Cacheable annotations and all that other stuff isn't an option for us.</p> <p>I've put the versions of our stack in the subject and what we have is a very typical configuration for that stack:</p> <ul> <li>Our Hibernate entities are just defined with @Entity, DAOs with @Repository, and services with @Service</li> <li>Transactional boundaries are set with @Transactional</li> <li>I've added in caching by adding ehcache-core to the pom.xml and adding the following properties to the Hibernate properties map:</li> </ul> <pre class="lang-xml prettyprint-override"><code>&lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.cache.region.factory_class"&gt;net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory&lt;/prop&gt; </code></pre> <p>I then added the <strong>@Cache</strong> annotation to my entity classes, something like this:</p> <pre class="lang-java prettyprint-override"><code>@Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "config") public class Configuration extends AbstractHibernateEntity implements Serializable { </code></pre> <p>And to be honest, I thought I was done. Yay me for configuring caching so easily! And you could see it working, there were lots of messages in the logs as caching came online. That's great!</p> <p>Only it turns out that caching has come online and nothing is getting cached. I've now been pounding on this for two days and have no clue why my objects are not getting cached. I've tried added <code>&lt;cache name="..."/&gt;</code> into my <strong>ehcache.xml</strong>, specifying regions on my <strong>@Cache</strong> annotations, checking the cache statistics, etc. I've added debug logging for both the <strong>net.sf.ehcache</strong> and <strong>org.hibernate.cache</strong> packages. I simply don't see what's keeping this from saving objects out to the cache and accessing them there.</p> <p>After we realized our web application wasn't caching, I went back into the dependent libraries and realized that they weren't caching either (I added caching to the unit tests to make sure that caching didn't break the unit tests; it doesn't but maybe that's just because it's not caching). So I've been working on one of our lower-level dependencies, figuring if I could figure out how to make it work at that level, I could pull that up to the web app level. That's a great theory except that I can't even make it work at the lower level!</p> <p>In my logs, I'm getting lots of messages. So here's a sample of the debug output from <strong>net.sf.ehcache</strong>:</p> <pre><code>14:03:20,795 INFO net.sf.ehcache: 284 - CACHE HITS: 0 14:03:20,796 INFO net.sf.ehcache: 284 - IN-MEM HITS: 0 14:03:20,796 INFO net.sf.ehcache: 284 - CACHE MISS: 0 14:03:20,796 INFO net.sf.ehcache: 284 - IN-MEM MISS: 0 14:03:20,796 INFO net.sf.ehcache: 284 - EVICTIONS: 0 14:03:20,797 INFO net.sf.ehcache: 284 - MEM OBJ CT: 0 14:03:20,886 DEBUG bernate.regions.EhcacheGeneralDataRegion: 211 - key: xhbm_configuration_data value: 5643835231789056 14:03:20,890 DEBUG bernate.regions.EhcacheGeneralDataRegion: 211 - key: xhbm_configuration value: 5643835231805440 14:03:20,891 DEBUG bernate.regions.EhcacheGeneralDataRegion: 211 - key: xhbm_configuration_data value: 5643834986045441 14:03:20,891 DEBUG bernate.regions.EhcacheGeneralDataRegion: 211 - key: xhbm_configuration value: 5643834986045441 14:03:20,897 DEBUG bernate.regions.EhcacheGeneralDataRegion: 211 - key: xhbm_configuration_data value: 5643835231834112 14:03:20,898 DEBUG bernate.regions.EhcacheGeneralDataRegion: 211 - key: xhbm_configuration value: 5643835231838208 14:03:20,898 DEBUG bernate.regions.EhcacheGeneralDataRegion: 211 - key: xhbm_configuration_data value: 5643834986078209 14:03:20,899 DEBUG bernate.regions.EhcacheGeneralDataRegion: 211 - key: xhbm_configuration value: 5643834986078209 </code></pre> <p>And in the <strong>org.hibernate.cache</strong> log, I get lots of stuff like this:</p> <pre><code>14:47:26,077 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Pre-invalidating space [xhbm_configuration_data] 14:47:26,081 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Pre-invalidating space [xhbm_configuration] 14:47:26,082 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Invalidating space [xhbm_configuration_data], timestamp: 5643845820751872 14:47:26,082 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Invalidating space [xhbm_configuration], timestamp: 5643845820751872 14:47:26,089 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Pre-invalidating space [xhbm_configuration_data] 14:47:26,091 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Pre-invalidating space [xhbm_configuration] 14:47:26,092 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Invalidating space [xhbm_configuration_data], timestamp: 5643845820792832 14:47:26,092 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Invalidating space [xhbm_configuration], timestamp: 5643845820792832 14:47:26,125 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Pre-invalidating space [xhbm_configuration_data] 14:47:26,130 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Pre-invalidating space [xhbm_configuration] 14:47:26,131 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Invalidating space [xhbm_configuration_data], timestamp: 5643845820952576 14:47:26,132 DEBUG rg.hibernate.cache.UpdateTimestampsCache: 168 - Invalidating space [xhbm_configuration], timestamp: 5643845820952576 </code></pre> <p>I really don't know what else to say about it from here. The full code is available both from my Bitbucket repo (it's Mercurial, not git) at <a href="https://bitbucket.org/rherrick/nrg_config" rel="nofollow">https://bitbucket.org/rherrick/nrg_config</a> or as a zip from Dropbox at <a href="https://dl.dropboxusercontent.com/u/42711610/nrg_config.zip" rel="nofollow">https://dl.dropboxusercontent.com/u/42711610/nrg_config.zip</a>. You should just be able to run the unit tests from Maven with:</p> <pre><code>mvn clean install </code></pre> <p>If anyone could help me figure out what the hell's going wrong here, I'd be ecstatically happy! And don't you want me to be ecstatically happy? I know I do :)</p> <p>Seriously, many thanks in advance and many thanks will follow afterwards for any help with this issue.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload