Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling Hibernate second-level cache with JPA on JBoss 4.2

What are the steps required to enable Hibernate's second-level cache, when using the Java Persistence API (annotated entities)? How do I check that it's working? I'm using JBoss 4.2.2.GA.

From the Hibernate documentation, it seems that I need to enable the cache and specify a cache provider in persistence.xml, like:

<property name="hibernate.cache.use_second_level_cache"
          value="true" /> 
<property name="hibernate.cache.provider_class" 
          value="org.hibernate.cache.HashtableCacheProvider" /> 

What else is required? Do I need to add @Cache annotations to my JPA entities?

How can I tell if the cache is working? I have tried accessing cache statistics after running a Query, but Statistics.getSecondLevelCacheStatistics returns null, perhaps because I don't know what 'region' name to use.

like image 673
Peter Hilton Avatar asked Sep 10 '08 07:09

Peter Hilton


People also ask

How does JPA implement second level cache?

To enable second-level cache we have to use <shared-cache-mode> element in the persistence. xml, with one of the values as defined in SharedCacheMode enum. In following example we will use shared-cache-mode=ALL which causes all entities and entity-related state and data to be cached.

How do I enable the second level cache in Hibernate spring boot?

For Enabling the second level of cache we have to made following change to hibernate configuration file. 1. Cache Strategy using with Annotation as some changes made to the Model class also. @Cacheable @Cache(usage = CacheConcurrencyStrategy.

What is L2 cache in JPA?

The dynamic cache service plugs in as a level 2 cache provider to JPA. The L2 cache boosts the performance of your JPA application and you can configure and monitor the dynamic cache service for your JPA application in the WebSphere Application Server environment.


1 Answers

Follow-up: in the end, after adding annotations, I have it working with EhCache, i.e.

<property name="hibernate.cache.provider_class" 
          value="net.sf.ehcache.hibernate.EhCacheProvider" />
like image 199
Peter Hilton Avatar answered Sep 20 '22 20:09

Peter Hilton