<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 7, comments - 3, trackbacks - 0, articles - 26

    JPA cache(轉載)

    Posted on 2010-07-22 14:38 delvin 閱讀(893) 評論(0)  編輯  收藏

    JPA Caching

    Posted by caroljmcdonald on August 21, 2009 at 12:58 PM PDT

    JPA Caching

    JPA Level 1 caching

    JPA has 2 levels of caching. The first level of caching is the persistence context.

    The JPA Entity Manager maintains a set of Managed Entities in the Persistence Context.

    The Entity Manager guarantees that within a single Persistence Context, for any particular database row, there will be only one object instance. However the same entity could be managed in another User's transaction, so you should use either optimistic or pessimistic locking  as explained in JPA 2.0 Concurrency and locking

    The code below shows that a find on a managed entity with the same id and class as another in the same persistence context , will return the same instance.

    @Stateless public ShoppingCartBean implements ShoppingCart {

     @PersistenceContext EntityManager entityManager;

     public OrderLine createOrderLine(Product product,Order order) {
            OrderLine orderLine = new OrderLine(order, product);
            entityManager.persist(orderLine);   //Managed
            OrderLine orderLine2 =entityManager.find(OrderLine,
                        orderLine.getId()));
            (orderLine == orderLine2  // TRUE
            return (orderLine);
        }

    }

    The diagram below shows the life cycle of an Entity in relation to the Persistent Context.

    The code below illustrates the life cycle of an Entity. A reference to a container managed EntityManager is injected using the persistence context annotation. A new order entity is created and the entity has the state of new. Persist is called, making this a managed entity. because it is a stateless session bean it is by default using container managed transactions , when this transaction commits , the order is made persistent in the database. When the orderline entity is returned at the end of the transaction it is a detached entity.

    The Persistence Context can be either Transaction Scoped-- the Persistence Context 'lives' for the length of the transaction, or Extended-- the Persistence Context spans multiple transactions. With a Transaction scoped Persistence Context, Entities are "Detached" at the end of a transaction.

    As shown below, to persist the changes on a detached entity, you call the EntityManager's merge() operation, which returns an updated managed entity, the entity updates will be persisted to the database at the end of the transaction.

    An Extended Persistence Context spans multiple transactions, and the set of Entities in the Persistence Context stay Managed. This can be useful in a work flow scenario where a "conversation" with a user spans multiple requests.

    The code below shows an example of a Stateful Session EJB with an Extended Persistence Context in a use case scenario to add line Items to an Order. After the Order is persisted in the createOrder method, it remains managed until the EJB remove method is called. In the addLineItem method , the Order Entity can be updated because it is managed, and the updates will be persisted at the end of the transaction.


    The example below contrasts updating the Order using a transaction scoped Persistence Context verses an extended Persistence context. With the transaction scoped persistence context, an Entity Manager find must be done to look up the Order, this returns a Managed Entity which can be updated. With the Extended Persistence Context the find is not necessary. The performance advantage of not doing a database read to look up the Entity, must be weighed against the disadvantages of memory consumption for caching, and the risk of cached entities being updated by another transaction.  Depending on the application and the risk of contention among concurrent transactions this may or may not give better performance / scalability.

    JPA second level (L2) caching

    JPA second level (L2) caching shares entity state across various persistence contexts.


    JPA 1.0 did not specify support of a second level cache, however, most of the persistence providers provided support for second level cache(s). JPA 2.0 specifies support for basic cache operations with the new Cache API, which is accessible from the EntityManagerFactory, shown below:


    If L2 caching is enabled, entities not found in persistence context, will be loaded from L2 cache, if found.

    The advantages of L2 caching are:
    • avoids database access for already loaded entities
    • faster for reading frequently accessed  unmodified entities
    The disadvantages of L2 caching are:
    • memory consumption for large amount of objects
    • Stale data for updated objects
    • Concurrency for write (optimistic lock exception, or pessimistic lock)
    • Bad scalability for frequent or concurrently updated entities

    You should configure L2 caching for entities that are:
    • read often
    • modified infrequently
    • Not critical if stale
    You should protect any data that can be concurrently modified with a locking strategy:
    • Must handle optimistic lock failures on flush/commit
    • configure expiration, refresh policy to minimize lock failures
    The Query cache is useful for queries that are run frequently with the same parameters, for not modified tables.

    The EclipseLink JPA persistence provider caching Architecture

    The  EclipseLink caching Architecture is shown below.


    Support for second level cache in EclipseLink is turned on by default, entities read are L2 cached. You can disable the L2 cache. EclipseLink caches entities in L2, Hibernate caches entity id and state in L2. You can configure caching by Entity type or Persistence Unit with the following configuration parameters:
    • Cache isolation, type, size, expiration, coordination, invalidation,refreshing
    • Coordination (cluster-messaging)
    • Messaging: JMS, RMI, RMI-IIOP, …
    • Mode: SYNC, SYNC+NEW, INVALIDATE, NONE
    The example below shows configuring the L2 cache for an entity using the @Cache annotation

    The Hibernate JPA persistence provider caching Architecture

    The Hibernate JPA persistence provider caching architecture is different than EclipseLink: it is not configured by default, it does not cache enities just id and state, and you can plug in different L2 caches. The diagram below shows the different L2 cache types that you can plug into Hibernate.

    The configuration of the cache depends on the type of caching plugged in. The example below shows configuring the hibernate L2 cache for an entity using the @Cache annotation

    For More Information:

    Introducing EclipseLink
    EclipseLink JPA User Guide
    Hibernate Second Level Cache
    Speed Up Your Hibernate Applications with Second-Level Caching
    Hibernate caching
    Java Persistence API 2.0: What's New ?
    Beginning Java™ EE 6 Platform with GlassFish™ 3
    Pro EJB 3: Java Persistence API (JPA 1.0)





    Related Topics >> Blogs      
    Comments
    Comments are listed in date ascending order (oldest first)

    Why gettting null EntityManager

    eclep.jpa.DataSource org.wadsworth.check.dto.FacDto public abstract class BaseDao { @PersistenceContext(unitName = "eclepJTA") private EntityManager entityContext; @PersistenceUnit(unitName = "eclepJTA") private EntityManagerFactory emf; public void check(){ System.out.println("<

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲欧美日韩自偷自拍| 久久精品国产亚洲AV麻豆网站| 亚洲a视频在线观看| 污视频在线观看免费| 中日韩亚洲人成无码网站| 亚洲无砖砖区免费| 免费一级毛片清高播放| 麻豆亚洲AV成人无码久久精品| 国产精品免费小视频| 香蕉视频免费在线播放| 国内精品久久久久久久亚洲| 91成人免费观看在线观看| 女人18毛片水最多免费观看| 亚洲国产精品一区二区第一页| 成全动漫视频在线观看免费高清版下载| 在线看片人成视频免费无遮挡| 亚洲精品中文字幕| 亚洲福利精品一区二区三区| japanese色国产在线看免费| 久久久久亚洲精品成人网小说| 久草视频在线免费| 亚洲变态另类一区二区三区| 成人午夜免费福利视频| 亚洲自偷自偷在线成人网站传媒 | 一本久久a久久精品亚洲| 免费看少妇高潮成人片| 亚洲视频在线免费看| 暖暖在线日本免费中文| 国产一级高青免费| 亚洲不卡中文字幕| 国产午夜影视大全免费观看| 国产一级婬片A视频免费观看| 亚洲欧洲自拍拍偷午夜色| 国产zzjjzzjj视频全免费| 日本亚洲欧洲免费天堂午夜看片女人员 | 精品女同一区二区三区免费播放 | 无码欧精品亚洲日韩一区| 毛片免费在线观看网站| 亚洲伊人久久精品| 一级毛片直播亚洲| 五月婷婷在线免费观看|