BigMemory 4.3.9 | Product Documentation | BigMemory Go Integrations | Using BigMemory Go with Hibernate | Enabling Second-Level Cache and Query Cache Settings
 
Enabling Second-Level Cache and Query Cache Settings
In addition to configuring the second-level cache provider setting, you will need to turn on the second-level cache (by default it is configured to off - 'false' - by Hibernate). To do this, set the following property in your Hibernate config:
<property name="hibernate.cache.use_second_level_cache">true</property>
You might also want to turn on the Hibernate query cache. To do this, set the following property in your Hibernate config:
<property name="hibernate.cache.use_query_cache">true</property>
Setting the ConfigurationResourceName Property
You can optionally set the ConfigurationResourceName property to specify the location of the Ehcache configuration file to use with the given Hibernate instance and cache provider/region-factory. The resource is searched for in the root of the classpath. It is used to support multiple CacheManagers in the same VM. It tells Hibernate which configuration to use. An example might be "ehcache-2.xml."
When using multiple Hibernate instances, it is recommended to use multiple non-singleton providers or region factories, each with a dedicated Ehcache configuration resource.
net.sf.ehcache.configurationResourceName=/name_of_ehcache.xml
Setting the Hibernate Cache Provider Programmatically
You can optionally specify the provider programmatically in Hibernate by adding necessary Hibernate property settings to the configuration before creating the SessionFactory:
Configuration.setProperty("hibernate.cache.region.factory_class",
"net.sf.ehcache.hibernate.EhCacheRegionFactory")
For Hibernate 4, use org.hibernate.cache.ehcache.EhCacheRegionFactory instead of net.sf.ehcache.hibernate.EhCacheRegionFactory.
Putting it all Together
If you are enabling both second-level caching and query caching, then your Hibernate config file should contain the following:
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
An equivalent Spring configuration file would contain:
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
For Hibernate 4, use org.hibernate.cache.ehcache.EhCacheRegionFactory instead of net.sf.ehcache.hibernate.EhCacheRegionFactory in both samples given above.