hibernate-session產生方式

來源:互聯網
上載者:User

標籤:hibernate

19.session產生方式

業務情境:如何保持service調用多個dao時,統一事務。

session的產生方式

不考慮分布式環境

分布式調用

不同java虛擬機器直接的對象調用

1.sessionFactory.openSession() 每次建立新session

             

2.sessionFactory.getCurrentSession();

(1)查看當前線程中的session是否存在

如果存在,從當前線程中取出session

如果不存在,建立一個session,並把這個session放到threadlocal中,使用者從threadlocal中取出session。

 

(2)session.getCurrentSession的用法

  1、在hibernate的設定檔中:

        <property name="current_session_context_class">thread</property>

  2、不需要寫session.close方法,在事務提交的時候會自動關閉(由hibernate內部完成)

  3、crud都需要事務

      1、因為是一個線程,所以整個方法中一個session,一個事務

      2、保證了整個業務操作的安全性

 


hibernate-session產生方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.