【问题标题】:What is the use of coordinationType in JPA?JPA 中协调类型的用途是什么?
【发布时间】:2014-02-10 07:38:22
【问题描述】:

我是 JPA 的新手,我开始使用 JPA 应用程序。在做的时候我发现了

@Cache(coordinationType=CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS)

还有更多类型的协调。我不理解 JPA 中协调类型的使用。谁能告诉我什么时候使用和使用什么协调类型。

【问题讨论】:

标签: java jpa eclipselink


【解决方案1】:

JPA 2.0 使用两个缓存:第一个是您的实体管理器。它在 JPA 1.0 中可用,并在其生命周期内缓存其对象。由于您应该将实体管理器用于单个工作单元,因此此生命周期通常很短。 JPA 2.0 引入了二级缓存,其中对象通常存在更长的时间。虽然这通常会带来性能优势,但可能会导致缓存不一致,尤其是在多节点环境中。 CacheCoordinationType 是一个用于控制缓存复制的 eclipselink 扩展。对于可移植应用程序,您可能希望使用 JPA 缓存 API。哪种策略最好在很大程度上取决于您的应用程序对一致性和性能的需求。如果您想要最大的性能并且您可以忍受一些过时的答案,请不要传播更改,而是限制缓存中对象的生命周期。否则通知更改并注意事项:

  1. 在多节点中,您必须正确设置缓存复制。
  2. 您必须注意可能会更改数据的其他应用程序。这通常意味着对可能从其他应用程序更改的表禁用缓存。

【讨论】:

    【解决方案2】:

    根据javadoc的CacheCoordinationType:

    /** 
     * An enum that is used within the Cache annotation.
     * 
     * @see org.eclipse.persistence.annotations.Cache
     * @author Guy Pelletier
     * @since Oracle TopLink 11.1.1.0.0 
     */ 
    public enum CacheCoordinationType {
        /**
         * Sends a list of changed objects including data about the changes. 
         * This data is merged into the receiving cache.
         */
        SEND_OBJECT_CHANGES,
    
        /**
         * Sends a list of the identities of the objects that have changed. The 
         * receiving cache invalidates the objects (rather than changing any of the 
         * data)
         */
        INVALIDATE_CHANGED_OBJECTS,
    
        /**
         * Same as SEND_OBJECT_CHANGES except it also includes any newly created 
         * objects from the transaction.
         */
        SEND_NEW_OBJECTS_WITH_CHANGES,
    
        /**
         * Does no cache coordination.
         */
        NONE
    }
    

    以下是一些可能有帮助的资源: @Example 2-10 使用@Cache 注解 https://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_cache.htm

    优化 #3 - 查询缓存 http://java-persistence-performance.blogspot.com/2011/06/how-to-improve-jpa-performance-by-1825.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-07
      • 2012-08-17
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      相关资源
      最近更新 更多