<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 閱讀(826) 評論(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
    主站蜘蛛池模板: 亚洲最新黄色网址| 最近免费mv在线电影| 亚洲国产精品一区二区九九 | 全黄a免费一级毛片人人爱| 亚洲不卡在线观看| 99久久国产免费-99久久国产免费| 中文字幕亚洲乱码熟女一区二区| 人成午夜免费大片在线观看| 国产免费资源高清小视频在线观看| 亚洲人成网站色在线观看| 最近免费中文字幕大全| 亚洲熟妇无码一区二区三区| 在线观看人成视频免费| MM1313亚洲国产精品| 亚洲综合久久夜AV | WWW免费视频在线观看播放| 国产亚洲精品资在线| a毛片免费全部播放完整成| 亚洲AV成人片色在线观看高潮| 黄页免费在线观看 | 女人张腿给男人桶视频免费版| 亚洲码欧美码一区二区三区| 国产无遮挡吃胸膜奶免费看| 一级视频免费观看| 久久精品亚洲一区二区| 曰批视频免费40分钟试看天天| 亚洲欧洲自拍拍偷午夜色| 最新69国产成人精品免费视频动漫 | 亚洲av无码成人精品区在线播放| 一级女人18片毛片免费视频| 亚洲国产精品无码中文字| 久热中文字幕在线精品免费| 亚洲成AV人片高潮喷水| 国产精品亚洲综合一区| 2021在线永久免费视频| 亚洲av日韩精品久久久久久a | 久久久久亚洲精品美女| 成人免费毛片观看| a级片在线免费看| 亚洲一本到无码av中文字幕| 中文字幕精品亚洲无线码一区 |