停用持續性單元第二級快取

依預設,OpenJPA 會停用第二級快取,但 EclipseLink 會啟用第二級快取。 如果要移轉的應用程式不使用快取,則可透過在 2.0 或 2.1 版 persistence.xml 檔中設定下列元素來停用 EclipseLink 中的快取:

   <shared-cache-mode>無</shared-cache-mode>

對於 1.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 配置內容的相關資訊,請參閱下列資源: