1. 程式人生 > >openSession 與 getCurrentSession的區別 openSession 與 getCurrentSession的區別

openSession 與 getCurrentSession的區別 openSession 與 getCurrentSession的區別

openSession 與 getCurrentSession的區別

 

1、openSession 每一次獲得的是一個全新的session物件,而getCurrentSession獲得的是與當前執行緒繫結的session物件

 

[java]  view plain copy print ?
  1. package cn.kiwifly.view;  
  2.   
  3. import org.hibernate.SessionFactory;  
  4. import org.hibernate.cfg.Configuration;  
  5. import org.hibernate.classic.Session;  
  6.   
  7. import cn.kiwifly.util.MySessionFactory;  
  8.   
  9. public class View {  
  10.   
  11.     public static void main(String[] args) {  
  12.   
  13.         Configuration configuration = new Configuration().configure();  
  14.         SessionFactory sf = configuration.buildSessionFactory();  
  15.           
  16.         Session sessionOpen1 = sf.openSession();  
  17.         Session sessionOpen2 = sf.openSession();  
  18.           
  19.         Session sessionThread1 = sf.getCurrentSession();  
  20.         Session sessionThread2 = sf.getCurrentSession();  
  21.           
  22.         System.out.println(sessionOpen1.hashCode() + "<-------->" + sessionOpen2.hashCode());  
  23.         System.out.println(sessionThread1.hashCode() + "<-------->" + sessionThread2.hashCode());  
  24.           
  25.   
  26.     }  
  27.   
  28. }  

上面程式碼輸出結果:

546579839<-------->1579795854
141106670<-------->141106670

 

2、openSession不需要配置,而getCurrentSession需要配置

1中程式碼如果直接執行會報錯,要在hibernate.cfg.xml中加入如下程式碼才行

 

[html]  view plain copy print ?
  1. <property name="current_session_context_class">thread</property>  

 

這裡我們是讓session與當前執行緒繫結,這裡的執行緒範圍應該是一次瀏覽器的會話過程,也就是說開啟網站和關閉瀏覽器這個時間段。

 

3、openSession需要手動關閉,而getCurrentSession系統自動關閉

openSession出來的session要通過:

 

[java]  view plain copy print ?
  1. session.close();  
而getSessionCurrent出來的session系統自動關閉,如果自己關閉會報錯

 

 

4、Session是執行緒不同步的,要保證執行緒安全就要使用getCurrentSession

1、openSession 每一次獲得的是一個全新的session物件,而getCurrentSession獲得的是與當前執行緒繫結的session物件

 

[java]  view plain copy print ?
  1. package cn.kiwifly.view;  
  2.   
  3. import org.hibernate.SessionFactory;  
  4. import org.hibernate.cfg.Configuration;  
  5. import org.hibernate.classic.Session;  
  6.   
  7. import cn.kiwifly.util.MySessionFactory;  
  8.   
  9. public class View {  
  10.   
  11.     public static void main(String[] args) {  
  12.   
  13.         Configuration configuration = new Configuration().configure();  
  14.         SessionFactory sf = configuration.buildSessionFactory();  
  15.           
  16.         Session sessionOpen1 = sf.openSession();  
  17.         Session sessionOpen2 = sf.openSession();  
  18.           
  19.         Session sessionThread1 = sf.getCurrentSession();  
  20.         Session sessionThread2 = sf.getCurrentSession();  
  21.           
  22.         System.out.println(sessionOpen1.hashCode() + "<-------->" + sessionOpen2.hashCode());  
  23.         System.out.println(sessionThread1.hashCode() + "<-------->" + sessionThread2.hashCode());  
  24.           
  25.   
  26.     }  
  27.   
  28. }  

上面程式碼輸出結果:

546579839<-------->1579795854
141106670<-------->141106670

 

2、openSession不需要配置,而getCurrentSession需要配置

1中程式碼如果直接執行會報錯,要在hibernate.cfg.xml中加入如下程式碼才行

 

[html]  view plain copy print ?
  1. <property name="current_session_context_class">thread</property>  

 

這裡我們是讓session與當前執行緒繫結,這裡的執行緒範圍應該是一次瀏覽器的會話過程,也就是說開啟網站和關閉瀏覽器這個時間段。

 

3、openSession需要手動關閉,而getCurrentSession系統自動關閉

openSession出來的session要通過:

 

[java]  view plain copy print ?
  1. session.close();  
而getSessionCurrent出來的session系統自動關閉,如果自己關閉會報錯

 

 

4、Session是執行緒不同步的,要保證執行緒安全就要使用getCurrentSession