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

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

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

    gembin

    OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

    HBase, Hadoop, ZooKeeper, Cassandra

    Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

    There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

    About Me

     

    Thread-safety when injecting JPA EntityManager

    Injecting EJB 3 stateful beans into servlet instance fields is not thread-safe. Along the same line, injecting EntityManager with @PersistenceContext into servlet instance variables is not thread-safe, either. EntityManager is just not designed to be thread-safe.

    For example, the following code snippet of a servlet class is incorrect:

    1
    2
    3
    4
    5
    6
    7
    public class EMTestServlet extends HttpServlet {
      //This field injection is not thread-safe.
      //FIXME
      @PersistenceContext
      private EntityManager em;
    ...
    }

    One way to fix this is to inject EntityManagerFactory instead. EntityManagerFactory is guaranteed to be thread-safe. For example:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class EMTestServlet extends HttpServlet {
      //This field injection is thread-safe
      @PersistenceUnit
      private EntityManagerFactory emf;
      
      protected void doGet(HttpServletRequest request,
                 HttpServletResponse response)
      throws ServletException, IOException {
      EntityManager em = emf.createEntityManager();
      //work with em
      }
    }

    Continuing container-managed EntityManager vs application-managed EntityManager.

    There are important differences between the injected EntityManager, and the EntityManager created from an injected EntityManagerFactory. Basically, injected EntityManager is container-managed, meaning all of its lifecycle is controlled by the container (web container or EJB container). Application code cannot close it, or otherwise interfere with its life.

    In addition, for a container-managed EntityManager, its associated PersistenceContext is automatically propagated along with the underlying JTA, from servlet A to servlet B, from servlet to EJB, from EJB a to EJB B, and so on. As such, EntityManager of the same configuration injected into various classes can share the same PersistenceContext in a call stack.

    On the other hand, EntityManager created from EntityManagerFactory is application-managed EntityManager. Application code is responsible for managing its whole lifecycle. And there is no PersistenceContext propagation for application-managed EntityManager.

    Are all EntityManager's obtained from EntityManagerFactory application-managed EntityManagerYes.

    Is it possible to get a container-managed EntityManager from EntityManagerFactoryNo.

    You may have read about the method EntityManagerFactory.getEntityManager(), which returns a container-managed EntityManager. This method was considered and included in the early draft version of Java Persistence API, but was eventually removed from its final release.

    posted on 2013-02-04 10:49 gembin 閱讀(825) 評論(0)  編輯  收藏 所屬分類: JavaEE

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類(440)

    隨筆檔案(378)

    文章檔案(6)

    新聞檔案(1)

    相冊

    收藏夾(9)

    Adobe

    Android

    AS3

    Blog-Links

    Build

    Design Pattern

    Eclipse

    Favorite Links

    Flickr

    Game Dev

    HBase

    Identity Management

    IT resources

    JEE

    Language

    OpenID

    OSGi

    SOA

    Version Control

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    free counters
    主站蜘蛛池模板: 免费va人成视频网站全| 成全在线观看免费观看大全| 18国产精品白浆在线观看免费| 亚洲人成人一区二区三区| www在线观看播放免费视频日本| 亚洲国产成人久久综合碰| 午夜在线免费视频| 亚洲日本中文字幕天堂网| 国产黄色免费观看| 亚洲国产精品无码成人片久久 | 狠狠色婷婷狠狠狠亚洲综合| 青青免费在线视频| 国产亚洲精品AA片在线观看不加载| 香蕉免费在线视频| 亚洲αv久久久噜噜噜噜噜| 久久99热精品免费观看动漫| 亚洲精品自在线拍| 日韩精品无码区免费专区| 亚洲精品美女久久7777777| 免费人成年轻人电影| aa级女人大片喷水视频免费| 亚洲国产精品国自产拍电影| 我的小后妈韩剧在线看免费高清版 | 麻豆国产人免费人成免费视频 | 久久亚洲精品成人无码| 亚洲精品人成无码中文毛片| 无套内射无矿码免费看黄| 国产国拍亚洲精品福利 | 亚洲日韩AV一区二区三区四区| 日韩免费福利视频| 国产三级在线免费观看| 久久久久亚洲av无码专区喷水| 嘿嘿嘿视频免费网站在线观看| 色噜噜的亚洲男人的天堂| 亚洲人成网亚洲欧洲无码久久| 国产成人免费午夜在线观看| 亚洲狠狠色丁香婷婷综合| 亚洲精品午夜无码专区| 成人午夜18免费看| 人妻在线日韩免费视频| 亚洲丁香婷婷综合久久|