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

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

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

    DANCE WITH JAVA

    開發出高質量的系統

    常用鏈接

    統計

    積分與排名

    好友之家

    最新評論

    hibernate事務管理 (jdbc jta)

    hibernate的兩種事務管理jdbc 和jta方式。下邊說說兩者的區別
    一、說明一下jdbc和jta方式事務管理的區別:
    JDBC事務由Connnection管理,也就是說,事務管理實際上是在JDBC Connection
    中實現。事務周期限于Connection的生命周期之內

    JTA 事務管理則由 JTA 容器實現,JTA 容器對當前加入事務的眾多Connection 進
    行調度,實現其事務性要求。JTA的事務周期可橫跨多個JDBC Connection生命周期。

    二、在了解jdbc和jta事務的基礎上,再來討論hibernate的兩種事務
    對于基于JDBC Transaction的Hibernate 事務管理機制而言,事務管理在Session 所依托的JDBC Connection
    中實現,事務周期限于Session的生命周期。

    對于基于JTA事務的Hibernate而言,JTA事務橫跨可橫跨多個Session。
    三、hibernate中寫法的不同

    jdbc的寫法
    public void saveUser(){
        Session session 
    = sessionFactory.openSession();
        Transaction tx 
    = session.beginTransaction();
        session.save(user);
        tx.commit();
        session.close();
    }

    必須在session.close()之前commit或者rollback

    jta寫法
    public void saveUser(){
        Session session 
    = sessionFactory.openSession();
        Transaction tx 
    = session.beginTransaction();
        
        session.save(user);
        session.close();
        
        Session session1 
    = sessionFactory.openSession();
        session1.save(user1);
        session.close();
        
        tx.commit();
    }

    commit和rollback可以在session.close()之后執行.
    同時應該注意的一點是,事務是不能嵌套的,在使用jta的事務的情況下,如果要讓一個事務跨越兩個
    session,則必須在兩個session的外層開始事務和完成事務。而不能再在session內部開始事務和完成事務。




    posted on 2007-07-29 01:58 dreamstone 閱讀(5274) 評論(3)  編輯  收藏 所屬分類: dao層框架

    評論

    # re: hibernate事務管理 (jdbc jta) 2007-07-29 10:18 pig

    JTA事務的開始
    Transaction tx = session.beginTransaction();
    應該不是這樣吧,應該是從容器中獲得。  回復  更多評論   

    # re: hibernate事務管理 (jdbc jta) 2007-07-29 12:35 slx

    @pig
    建議看看hibernate reference 事務處理 jta部分。

    11.2.2. Using JTA
    If your persistence layer runs in an application server (e.g. behind EJB session beans), every datasource connection obtained by Hibernate will automatically be part of the global JTA transaction. You can also install a standalone JTA implementation and use it without EJB. Hibernate offers two strategies for JTA integration.

    If you use bean-managed transactions (BMT) Hibernate will tell the application server to start and end a BMT transaction if you use the Transaction API. So, the transaction management code is identical to the non-managed environment.

    // BMT idiom
    Session sess = factory.openSession();
    Transaction tx = null;
    try {
    tx = sess.beginTransaction();

    // do some work
    ...

    tx.commit();
    }
    catch (RuntimeException e) {
    if (tx != null) tx.rollback();
    throw e; // or display error message
    }
    finally {
    sess.close();
    }
    If you want to use a transaction-bound Session, that is, the getCurrentSession() functionality for easy context propagation, you will have to use the JTA UserTransaction API directly:

    // BMT idiom with getCurrentSession()
    try {
    UserTransaction tx = (UserTransaction)new InitialContext()
    .lookup("java:comp/UserTransaction");

    tx.begin();

    // Do some work on Session bound to transaction
    factory.getCurrentSession().load(...);
    factory.getCurrentSession().persist(...);

    tx.commit();
    }
    catch (RuntimeException e) {
    tx.rollback();
    throw e; // or display error message
    }
    With CMT, transaction demarcation is done in session bean deployment descriptors, not programatically, hence, the code is reduced to:

    // CMT idiom
    Session sess = factory.getCurrentSession();

    // do some work
    ...

    In a CMT/EJB even rollback happens automatically, since an unhandled RuntimeException thrown by a session bean method tells the container to set the global transaction to rollback. This means you do not need to use the Hibernate Transaction API at all with BMT or CMT, and you get automatic propagation of the "current" Session bound to the transaction.

    Note that you should choose org.hibernate.transaction.JTATransactionFactory if you use JTA directly (BMT), and org.hibernate.transaction.CMTTransactionFactory in a CMT session bean, when you configure Hibernate's transaction factory. Remember to also set hibernate.transaction.manager_lookup_class. Furthermore, make sure that your hibernate.current_session_context_class is either unset (backwards compatiblity), or set to "jta".

    The getCurrentSession() operation has one downside in a JTA environment. There is one caveat to the use of after_statement connection release mode, which is then used by default. Due to a silly limitation of the JTA spec, it is not possible for Hibernate to automatically clean up any unclosed ScrollableResults or Iterator instances returned by scroll() or iterate(). You must release the underlying database cursor by calling ScrollableResults.close() or Hibernate.close(Iterator) explicity from a finally block. (Of course, most applications can easily avoid using scroll() or iterate() at all from the JTA or CMT code.)

      回復  更多評論   

    # re: hibernate事務管理 (jdbc jta) 2007-11-03 06:17 jeadu

    pig 所說的是JTA規范中定義的寫法,而你所說的是經過hibernate包換的寫法。  回復  更多評論   

    主站蜘蛛池模板: 91嫩草私人成人亚洲影院| 亚洲白色白色在线播放| 中文字幕日本人妻久久久免费| 亚洲一区精品无码| 最近免费中文在线视频| 亚洲乱码国产乱码精华| 在线a亚洲v天堂网2018| 无码免费一区二区三区免费播放| 亚洲va精品中文字幕| 夜色阁亚洲一区二区三区| 国产精品免费AV片在线观看| 亚洲国产日韩综合久久精品| 亚洲乱码中文字幕综合234| 日本免费中文视频| 337P日本欧洲亚洲大胆精品| 亚洲AV无码成人精品区天堂| 成人毛片免费播放| 97无码人妻福利免费公开在线视频| 亚洲精品第五页中文字幕| 国产成人精品123区免费视频| 国产又黄又爽胸又大免费视频| 亚洲天堂2017无码中文| 国产美女亚洲精品久久久综合| 日韩精品无码区免费专区| 国产成人精品免费大全| 亚洲一区二区三区在线网站| 亚洲精品午夜无码电影网| 在线观看视频免费完整版| a级日本高清免费看| 亚洲国产欧美日韩精品一区二区三区 | 91福利免费视频| 无码色偷偷亚洲国内自拍| 亚洲熟妇av一区二区三区下载| 精品亚洲成α人无码成α在线观看| 69天堂人成无码麻豆免费视频| 国产在线国偷精品免费看| 亚洲AV无码成人网站在线观看| 亚洲国产美女福利直播秀一区二区| 亚洲欧洲自拍拍偷精品 美利坚| 免费无码又爽又刺激高潮| 4399影视免费观看高清直播|