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

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

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

    DANCE WITH JAVA

    開發(fā)出高質(zhì)量的系統(tǒng)

    常用鏈接

    統(tǒng)計(jì)

    積分與排名

    好友之家

    最新評(píng)論

    hibernate事務(wù)管理 (jdbc jta)

    hibernate的兩種事務(wù)管理jdbc 和jta方式。下邊說說兩者的區(qū)別
    一、說明一下jdbc和jta方式事務(wù)管理的區(qū)別:
    JDBC事務(wù)由Connnection管理,也就是說,事務(wù)管理實(shí)際上是在JDBC Connection
    中實(shí)現(xiàn)。事務(wù)周期限于Connection的生命周期之內(nèi)

    JTA 事務(wù)管理則由 JTA 容器實(shí)現(xiàn),JTA 容器對(duì)當(dāng)前加入事務(wù)的眾多Connection 進(jìn)
    行調(diào)度,實(shí)現(xiàn)其事務(wù)性要求。JTA的事務(wù)周期可橫跨多個(gè)JDBC Connection生命周期。

    二、在了解jdbc和jta事務(wù)的基礎(chǔ)上,再來討論hibernate的兩種事務(wù)
    對(duì)于基于JDBC Transaction的Hibernate 事務(wù)管理機(jī)制而言,事務(wù)管理在Session 所依托的JDBC Connection
    中實(shí)現(xiàn),事務(wù)周期限于Session的生命周期。

    對(duì)于基于JTA事務(wù)的Hibernate而言,JTA事務(wù)橫跨可橫跨多個(gè)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()之后執(zhí)行.
    同時(shí)應(yīng)該注意的一點(diǎn)是,事務(wù)是不能嵌套的,在使用jta的事務(wù)的情況下,如果要讓一個(gè)事務(wù)跨越兩個(gè)
    session,則必須在兩個(gè)session的外層開始事務(wù)和完成事務(wù)。而不能再在session內(nèi)部開始事務(wù)和完成事務(wù)。




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

    評(píng)論

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

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

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

    @pig
    建議看看hibernate reference 事務(wù)處理 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.)

      回復(fù)  更多評(píng)論   

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

    pig 所說的是JTA規(guī)范中定義的寫法,而你所說的是經(jīng)過hibernate包換的寫法。  回復(fù)  更多評(píng)論   

    主站蜘蛛池模板: 成人免费网站在线观看| 在线视频免费观看高清| 亚洲国产精品成人| 亚洲精品无码久久久久牙蜜区| 日本免费网站视频www区| 亚洲一区二区电影| 少妇人妻偷人精品免费视频| 亚洲av日韩av天堂影片精品| 国产精品视频白浆免费视频| 亚洲精品中文字幕乱码三区| 免费成人在线电影| 亚洲视频在线播放| 免费在线视频你懂的| 亚洲宅男精品一区在线观看| 成人免费无毒在线观看网站| 亚洲人成综合网站7777香蕉| 成全视频在线观看免费高清动漫视频下载 | 中日韩亚洲人成无码网站| 免费视频爱爱太爽了| 亚洲熟妇无码八V在线播放| 在线观看人成网站深夜免费| 国产亚洲精品第一综合| 亚洲人成人77777网站| 久久久久高潮毛片免费全部播放| 亚洲春色另类小说| 成人免费看片又大又黄| 又黄又大的激情视频在线观看免费视频社区在线 | 最近免费字幕中文大全视频| 亚洲天堂免费在线| 亚洲av成人一区二区三区在线观看| 亚洲免费在线观看| 亚洲春黄在线观看| 日本黄色免费观看| 日本免费在线观看| 亚洲人成电影网站久久| 亚洲国产精品成人| 中文字幕成人免费视频| 蜜臀亚洲AV无码精品国产午夜.| 亚洲一区二区三区香蕉| 国产1024精品视频专区免费| 免费夜色污私人影院网站|