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

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

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

    posts - 495,comments - 227,trackbacks - 0

    hibernate.current_session_context_class屬性配置

    此設置的作用如下:

    What does sessionFactory.getCurrentSession() do? First, you can call it
    as many times and anywhere you
    like, once you get hold of your SessionFactory (easy thanks to
    HibernateUtil). The getCurrentSession()
    method always returns the "current" unit of work. Remember that we
    switched the configuration option for this
    mechanism to "thread" in hibernate.cfg.xml? Hence, the scope of the
    current unit of work is the current Java
    thread that executes our application. However, this is not the full
    truth. A Session begins when it is first
    needed, when the first call to getCurrentSession() is made. It is then
    bound by Hibernate to the current
    thread. When the transaction ends, either committed or rolled back,
    Hibernate also unbinds the Session from
    the thread and closes it for you. If you call getCurrentSession() again,
    you get a new Session and can start a
    new unit of work. This thread-bound programming model is the most
    popular way of using Hibernate.

    意思是說:

    sessionFactory.getCurrentSession()可以完成一系列的工作,當調用時,
    hibernate將session綁定到當前線程,事務結束后,hibernate
    將session從當前線程中釋放,并且關閉session。當再次調用getCurrentSession
    ()時,將得到一個新的session,并重新開始這一系列工作。
    這樣調用方法如下:

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    Event theEvent = new Event();
    theEvent.setTitle(title);
    theEvent.setDate(theDate);
    session.save(theEvent);
    session.getTransaction().commit();

    不需要close session了。

     

    前提是改值設置為了thread.

     

    1 getCurrentSession創建的session會和綁定到當前線程,而openSession不會。

    2 getCurrentSession創建的線程會在事務回滾或事物提交后自動關閉,而openSession必須手動關閉

    這里getCurrentSession本地事務(本地事務:jdbc)時 要在配置文件里進行如下設置

        * 如果使用的是本地事務(jdbc事務)
     <property name="hibernate.current_session_context_class">thread</property>
     * 如果使用的是全局事務(jta事務)
     <property name="hibernate.current_session_context_class">jta</property> 

     getCurrentSession () 使用當前的session
    openSession()         重新建立一個新的session

    在一個應用程序中,如果DAO 層使用Spring 的hibernate 模板,通過Spring 來控制session 的生命周期,則首選getCurrentSession ()。

    使 用Hibernate的大多數應用程序需要某種形式的“上下文相關的”session,特定的session在整個特定的上下文范圍內始終有效。然而,對 不同類型的應用程序而言,要為什么是組成這種“上下文”下一個定義通常是困難的;不同的上下文對“當前”這個概念定義了不同的范圍。在3.0版本之前,使 用Hibernate的程序要么采用自行編寫的基于ThreadLocal的上下文session,要么采用HibernateUtil這樣的輔助類,要 么采用第三方框架(比如Spring或Pico),它們提供了基于代理(proxy)或者基于攔截器(interception)的上下文相關 session。

    從3.0.1版本開始,Hibernate增加了 SessionFactory.getCurrentSession()方法。一開始,它假定了采用JTA事務,JTA事務定義了當前session的范 圍和上下文(scope and context)。Hibernate開發團隊堅信,因為有好幾個獨立的JTATransactionManager實現穩定可用,不論是否被部署到一個 J2EE容器中,大多數(假若不是所有的)應用程序都應該采用JTA事務管理。基于這一點,采用JTA的上下文相關session可以滿足你一切需要。

    更 好的是,從3.1開始,SessionFactory.getCurrentSession()的后臺實現是可拔插的。因此,我們引入了新的擴展接口 (org.hibernate.context.CurrentSessionContext)和新的配置參數 (hibernate.current_session_context_class),以便對什么是“當前session”的范圍和上下文 (scopeand context)的定義進行拔插。

    請參閱 org.hibernate.context.CurrentSessionContext接口的Javadoc,那里有關于它的契約的詳細討論。它定義 了單一的方法,currentSession(),特定的實現用它來負責跟蹤當前的上下文session。Hibernate內置了此接口的兩種實現。

    org.hibernate.context.JTASessionContext - 當前session根據JTA來跟蹤和界定。這和以前的僅支持JTA的方法是完全一樣的。詳情請參閱Javadoc。

    org.hibernate.context.ThreadLocalSessionContext - 當前session通過當前執行的線程來跟蹤和界定。詳情也請參閱Javadoc。

    這 兩種實現都提供了“每數據庫事務對應一個session”的編程模型,也稱作每次請求一個session。Hibernatesession的起始和終結 由數據庫事務的生存來控制。假若你采用自行編寫代碼來管理事務(比如,在純粹的J2SE,或者JTA/UserTransaction/BMT),建議你 使用Hibernate TransactionAPI來把底層事務實現從你的代碼中隱藏掉。如果你在支持CMT的EJB容器中執行,事務邊界是聲明式定義的,你不需要在代碼中進 行任何事務或session管理操作。請參閱第 11 章 事務和并發一節來閱讀更多的內容和示例代碼。

    hibernate.current_session_context_class 配置參數定義了應該采用哪個org.hibernate.context.CurrentSessionContext實現。注意,為了向下兼容,如果未 配置此參數,但是存在org.hibernate.transaction.TransactionManagerLookup的配 置,Hibernate會采用org.hibernate.context.JTASessionContext。一般而言,此參數的值指明了要使用的實 現類的全名,但那兩個內置的實現可以使用簡寫,即"jta"和"thread"。

    1、getCurrentSession()與openSession()的區別?

    * 采用getCurrentSession()創建的session會綁定到當前線程中,而采用openSession()創建的session則不會* 采用getCurrentSession()創建的session在commit或rollback時會自動關閉,而采用openSession()創建 的session必須手動關閉2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:* 如果使用的是本地事務(jdbc事務)<property name="hibernate.current_session_context_class">thread</property>* 如果使用的是全局事務(jta事務)

    <property name="hibernate.current_session_context_class">jta</property>

    利于ThreadLocal模式管理Session
       早在Java1.2推出之時,Java平臺中就引入了一個新的支持:java.lang.ThreadLocal,給我們在編寫多線程程序
       時提供了一種新的選擇。ThreadLocal是什么呢?其實ThreadLocal并非是一個線程的本地實現版本,它并不是一個Thread,
       而是thread local variable(線程局部變量)。也許把它命名為ThreadLocalVar更加合適。線程局部變量(ThreadLocal)
       其實的功用非常簡單,就是為每一個使用某變量的線程都提供一個該變量值的副本,是每一個線程都可以獨立地改變自己的副本,
       而不會和其它線程的副本沖突。從線程的角度看,就好像每一個線程都完全擁有一個該變量。
       ThreadLocal是如何做到為每一個線程維護變量的副本的呢?其實實現的思路很簡單,在ThreadLocal類中有一個Map,
       用于存儲每一個線程的變量的副本。比如下面的示例實現(為了簡單,沒有考慮集合的泛型):
    public class HibernateUtil {

    public static final ThreadLocal session =new ThreadLocal();

    public static final SessionFactory sessionFactory;
       static {
          try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
          } catch (Throwable ex) {
               throw new ExceptionInInitializerError(ex);
          }    
    }

         public static Session currentSession() throws HibernateException {
            Session s = session.get();
            if(s == null) {
              s = sessionFactory.openSession();
              session.set(s);
               }
             return s;
           }

        public static void closeSession() throws HibernateException {
               Session s = session.get();
            if(s != null) {
                s.close();
            }
            session.set(null);
        }
    }

    openSession() getCurrentSession() 有何不同和關聯呢?

     

    在 SessionFactory 啟動的時候, Hibernate 會根據配置創建相應的 CurrentSessionContext ,在 getCurrentSession() 被調用的時候,實際被執行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 執行時,如果當前 Session 為空, currentSession 會調用 SessionFactory 的 openSession 。所以 getCurrentSession() 對于 Java EE 來說是更好的獲取 Session 的方法。

    posted on 2012-10-16 16:04 SIMONE 閱讀(10304) 評論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 三上悠亚在线观看免费| 亚洲娇小性xxxx| 成人久久久观看免费毛片| 日本免费网站观看| 亚洲国产成人久久综合| 免费黄色小视频网站| 亚洲色偷偷综合亚洲AV伊人蜜桃 | 国产乱辈通伦影片在线播放亚洲 | 亚洲av无码一区二区三区网站| 狠狠躁狠狠爱免费视频无码| 日韩va亚洲va欧洲va国产| 久久九九全国免费| 亚洲欧洲日产专区| 女人18一级毛片免费观看| 亚洲1区2区3区精华液| 亚洲精品成人久久久| 国内精品一级毛片免费看| 亚洲精品第五页中文字幕| a级毛片无码免费真人| 美女视频黄a视频全免费网站色 | 日本视频一区在线观看免费| 亚洲一欧洲中文字幕在线| 精品免费国产一区二区| 一区在线免费观看| 亚洲综合无码一区二区三区| 最近中文字幕免费mv视频7| 免费的黄色的网站| 亚洲宅男永久在线| 日本媚薬痉挛在线观看免费| 中文在线观看免费网站| 亚洲中文久久精品无码1| 亚洲成A人片在线观看中文| 182tv免费视频在线观看 | jizz免费一区二区三区| 97亚洲熟妇自偷自拍另类图片| 拍拍拍又黄又爽无挡视频免费| 免费无码午夜福利片69| 亚洲神级电影国语版| 又黄又爽一线毛片免费观看 | 999久久久免费精品国产| 手机永久免费的AV在线电影网|