禁用持久性单元二级高速缓存

OpenJPA 在缺省情况下禁用了二级高速缓存,而 EclipseLink 在缺省情况下则启用了二级高速缓存。 如果要迁移不使用高速缓存的应用程序,可通过在 V2.0 或 V2.1 的 persistence.xml 文件中设置以下元素来禁用 EcipseLink 中的高速缓存:

   <shared-cache-mode>无</shared-cache-mode>

对于 V1.0 的 persistence.xml 文件,可通过添加以下属性来禁用 EclipseLink 中的高速缓存:

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

此规则可检测 <persistence-unit> 配置中的 META-INF/persistence.xml 文件中的配置,其中当前缓存配置会导致 EclipseLink 中不同的默认缓存行为。 它还可以帮助您清理 openjpa.DataCache 属性,这些属性会被 EclipseLink 忽略。

为确定是否需要进行配置更改,此规则会检查以下设置的组合:

如果未指定 openjpa.DataCache 属性或将其设置为 false,并且已忽略共享高速缓存方式配置或将其设置为 UNSPECIFIED,那么在 EclipseLink 中,必须更改配置以禁用高速缓存。 由于在缺省情况下 EclipseLink 启用了高速缓存,因此在 OpenJPA 中将 openjpa.DataCache 属性设置为 true 等同于 EclipseLink 中无共享高速缓存方式设置或将 shared-cache-mode 设置为 ALL。 如果您手动移除 openjpa.DataCache 配置,请勿继续运行此规则,因为它不会将任何配置检测为行为更改。

该规则的自动修复程序为共享缓存模式设置了一个显式值,这样该规则就不会继续标记持久性单元。 这样规则就不会继续标记持久化单元。 自动修复会评估 openjpa.DataCache 设置并修改现有的或创建新的共享缓存模式配置或 javax.persistence.sharedCache.mode 属性,以便您的应用程序在 EclipseLink 中具有与在 OpenJPA 中相同的缓存模式行为。 在 OpenJPA 中,当设置了由规范定义的共享高速缓存值和 openjpa.DataCache 属性时,除非它是 UNSPECIFIED,否则将使用由规范定义的值。 如果该值是 UNSPECIFIED,那么将使用 openjpa.DataCache 属性来确定共享高速缓存方式的正确值。

例如,在以下 persistence.xml 文件中、 规则标记了所有 <persistence-unit> 元素,因为它们需要更改缓存配置才能保持相同。 这些元素需要更改缓存配置,以保持 中的缓存行为与 EclipseLink OpenJPA 缓存行为。

OpenJPA 的源 persistence.xml 文件 自动修复 persistence.xml 文件中的更改 EclipseLink
<persistence> <persistence-unit name=" example1 ">...
    <shared-cache-mode>未知</shared-cache-mode>... </persistence-unit> <persistence-unit name=" example2 ">... <properties> <property name="openjpa.DataCache" value="true"/> </properties>... </persistence-unit> <persistence-unit name=" example3 ">... <properties><-- 未设置缓存属性 --></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>无</shared-cache-mode>... </persistence-unit> <persistence-unit name="example2"> ...
     <shared-cache-mode>所有</shared-cache-mode> <properties></properties>... </persistence-unit> <persistence-unit name="example3"> ...
     <shared-cache-mode>无</shared-cache-mode> <properties><-- 未设置缓存属性 --></properties>... </persistence-unit> <persistence-unit name="example4">... <properties> <property name="javax.persistence.sharedCache.mode" 
                  value="ALL"/> </properties>...</persistence-unit> </persistence>

自动修复会删除 openjpa.DataCache 属性的 truefalse 值,则自动删除该属性。 如果 openjpa.DataCache 属性配置有附加参数 如下面的示例,自动修复会保留该属性,以便您确定 是否需要其他手动更改。 评估 openjpa.DataCache 属性以进行其他迁移,然后移除此属性(因为 EclipseLink 不使用此属性)。

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

如果在同一个 persistence-unit 中同时定义了 shared-cache-modejavax.persistence.sharedCache.mode ,则自动修复会在需要时更新 shared-cache-mode 值,并删除 javax.persistence.sharedCache.mode 属性。

对于 JPA 1.0 persistence.xml 文件,<shared-cache-mode> 元素无效。 元素无效。 自动修复会评估 openjpa.DataCachejavax.persistence.sharedCache.mode 持久性属性的值,并在需要时添加一个 持久性属性。 并在需要时添加 eclipselink.cache.shared.default 持久性属性。

定制配置复制到应用程序构建文件以启用修订自动化。

有关迁移特定于供应商的 JPA 配置属性的更多信息,请参阅以下资源: