JPA 2.0 <shared-cache-mode> vs. javax.persistence.sharedCache.mode

Nayan Hajratwala picture Nayan Hajratwala · Mar 20, 2013 · Viewed 11.6k times · Source

Using Spring 3.2.0, Eclipselink 2.5.0-M9

When persistence.xml contains:

<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

Then if I examine the EntityManagerFactory during runtime, via emf.getProperties(), this property is not set.

However, if I put it in my Spring entityManagerFactory configuration instead:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  ...
  <property name="jpaPropertyMap">
    <map>
      <entry key="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" />
    </map>
  </property>
</bean>

Then I can retrieve the property from emf.getProperties()

Also, it appears that when using the spring config, the shared cache is not actually enabled. this leads me to believe that I'm not setting it correctly in the spring container.

Ideas?

Answer

James picture James · Mar 21, 2013
<shared-cache-mode>

Is not a persistence unit property, but an element in the persistence.xml. I'm not sure what setting "javax.persistence.sharedCache.mode" as a property does, but my guess is it is just set as a persistence unit property, and ignored.

But default, EclipseLink enables the shared cache, so you do not need to configure it.

If you are not seeing caching being used, it could be because of the Spring bug, https://jira.springsource.org/browse/SPR-7753, in which case there is a workaround using setLazyDatabaseTransaction() option in the EclipseLinkJpaDialect.

ENABLE_SELECTIVE I think mean only enable caching for Entities that have @Cacheable(true), so that may not be what you want.