隨筆-4  評(píng)論-1  文章-0  trackbacks-0
          SessionFactory.getCurrentSession() 是 Hibernate 應(yīng)用獲取 Session 的常用方法。在調(diào)用該方法時(shí),Hibernate 會(huì)從 interface CurrentSessionContext 獲取當(dāng)前的 Session,這是Hibernate 在不同組件中傳遞 Session 的方法。

          CurrentSessionContext 有三個(gè)實(shí)現(xiàn),分別是 ThreadLocalSessionContext、JTASessionContext 和 ManagedSessionContext。ThreadLocalSessionContext 將 Session 與當(dāng)前線程綁定,是使用較多的一種方案;JTASessionContext 將 Session 與 JTA 事務(wù)綁定,在 JTA 環(huán)境中使用;ManagedSessionContext 使應(yīng)用可以通過 bind() 和 unbind() 方法控制 Session 的綁定,主要在有 Conversation 的應(yīng)用中使用(如果使用 ManagedSessionContext,開發(fā)人員要做的事情還是很多的)。CurrentSessionContext 實(shí)現(xiàn)的選擇可以通過 hibernate.current_session_context_class 來配置。

          另一種更常見的創(chuàng)建 Session 的方法是 openSession()。openSession() 與 getCurrentSession() 有何不同和關(guān)聯(lián)呢?在 SessionFactory 啟動(dòng)的時(shí)候,Hibernate 會(huì)根據(jù)配置創(chuàng)建相應(yīng)的 CurrentSessionContext,在 getCurrentSession() 被調(diào)用的時(shí)候,實(shí)際被執(zhí)行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 執(zhí)行時(shí),如果當(dāng)前 Session 為空,currentSession 會(huì)調(diào)用 SessionFactory 的 openSession。所以 getCurrentSession() 對(duì)于 Java EE 來說是更好的獲取 Session 的方法。

          再說 ManagedSessionContext,它提供了更靈活的綁定 Session 的方式,但是使用起來去不簡(jiǎn)單。在 Hibernate 的 CaveatEmptor 實(shí)例中有關(guān)于使用 ManagedSessionContext 的例子,但更好的選擇是使用 Seam Framework。
          posted on 2008-07-16 15:48 阿里 閱讀(1545) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 衡山县| 噶尔县| 元阳县| 玛沁县| 兴宁市| 保定市| 大关县| 康保县| 乌海市| 新昌县| 高尔夫| 台湾省| 武清区| 民乐县| 石景山区| 呼伦贝尔市| 新密市| 巩义市| 鄂伦春自治旗| 通城县| 抚宁县| 屏山县| 无棣县| 山阴县| 六枝特区| 永泰县| 炉霍县| 普兰店市| 祁东县| 黄陵县| 建瓯市| 防城港市| 巩留县| 海盐县| 双峰县| 文山县| 杂多县| 昌平区| 甘孜| 西青区| 吴忠市|