Disable the persistence unit second-level cache

OpenJPA disables the second-level cache by default, whereas EclipseLink enables the second-level cache by default. If you are migrating an application that does not use the cache, you can disable the cache in EclipseLink by setting the following element in a version 2.0 or 2.1 persistence.xml file:

   <shared-cache-mode>NONE</shared-cache-mode>

For a version 1.0 persistence.xml file, you can disable the cache in EclipseLink adding the following property:

   <property name="eclipselink.cache.shared.default" value="false"/>

This rule detects <persistence-unit> configurations in the META-INF/persistence.xml file where the current caching configuration results in a different default caching behavior in EclipseLink. It also helps you clean up openjpa.DataCache properties, which are ignored by EclipseLink.

To determine whether a configuration change is required, the rule examines combinations of the following settings:

If the openjpa.DataCache property is not specified or is set to false and the shared cache mode configuration is omitted or set to UNSPECIFIED, in EclipseLink you must change the configuration to disable the cache. Because EclipseLink enables caching by default, an openjpa.DataCache property set to true in OpenJPA is equivalent to having no shared cache mode setting in EclipseLink or to setting shared-cache-mode to ALL. If you manually remove the openjpa.DataCache configuration, do not continue to run this rule because it will detect no configuration as a behavior change.

The automated fix for this rule sets an explicit value for the shared cache mode so that the rule does not continue to flag the persistence-unit. The automated fix evaluates the openjpa.DataCache setting and modifies existing or creates new shared-cache-mode configuration or javax.persistence.sharedCache.mode properties so that your applications have the same cache mode behavior in EclipseLink as they did in OpenJPA. In OpenJPA, when both a specification-defined shared cache value and the openjpa.DataCache property are set, the specification-defined value is used unless it is UNSPECIFIED. If the value is UNSPECIFIED, the openjpa.DataCache property is used to determine the correct value of the shared cache mode.

For example, in the following persistence.xml file, the rule flags all of the <persistence-unit> elements because they require changes to the caching configuration to maintain the same caching behavior in EclipseLink as it was in OpenJPA.

Source persistence.xml file for OpenJPA Automated fix changes to persistence.xml file for EclipseLink
<persistence>

  <persistence-unit name="example1">
    ...
    <shared-cache-mode>UNSPECIFIED</shared-cache-mode>
    ...
  </persistence-unit>

  <persistence-unit name="example2">
     ...
     <properties>
        <property name="openjpa.DataCache" value="true"/>
     </properties>
     ...
  </persistence-unit>

  <persistence-unit name="example3">
     ...
     <properties>
        <-- no cache properties set -->
     </properties>
     ...
  </persistence-unit>


  <persistence-unit name="example4">
     ...
     <properties>
        <property name="openjpa.DataCache" 
                  value="false"/>
        <property name="javax.persistence.sharedCache.mode" 
                  value="ALL"/>
     </properties>
     ...
  </persistence-unit>

</persistence>
<persistence>

  <persistence-unit name="example1">
    ...
    <shared-cache-mode>NONE</shared-cache-mode>
    ...
  </persistence-unit>

  <persistence-unit name="example2">
     ...
     <shared-cache-mode>ALL</shared-cache-mode>
     <properties>
     </properties>
     ...
  </persistence-unit>

  <persistence-unit name="example3">
     ...
     <shared-cache-mode>NONE</shared-cache-mode>
     <properties>
        <-- no cache properties set -->
     </properties>
     ...
  </persistence-unit>

  <persistence-unit name="example4">
     ...
     <properties>
        <property name="javax.persistence.sharedCache.mode" 
                  value="ALL"/>
     </properties>
     ...
  </persistence-unit>

</persistence>

The automated fix deletes the openjpa.DataCache property if it has a simple true or false value. If the openjpa.DataCache property configuration has additional parameters such as in the following example, the automated fix leaves the property so you can determine if any additional manual changes are needed. Evaluate the openjpa.DataCache property for additional migration, and then remove the property because it is not used by EclipseLink.

    <property name="openjpa.DataCache" value="true(Types=com.test.Person;com.test.Employee)"/>

If both shared-cache-mode and javax.persistence.sharedCache.mode are defined in the same persistence-unit, the automated fix updates the shared-cache-mode value if needed and deletes the javax.persistence.sharedCache.mode property.

For JPA 1.0 persistence.xml files, the <shared-cache-mode> element is not valid. The automated fix evaluates the values of openjpa.DataCache and javax.persistence.sharedCache.mode persistence properties and adds an eclipselink.cache.shared.default persistence property when needed.

Copy the custom configuration to your application build file to enable the fix automation.

For more information on migrating vendor-specific JPA configuration properties, see the following resources: