自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

講解Hibernate ThreadLocal

開(kāi)發(fā) 后端
這里介紹當(dāng)線程調(diào)用Hibernate ThreadLocal.get方法時(shí), Hibernate ThreadLocal會(huì)根據(jù)當(dāng)前線程對(duì)象的引用,取出Map中對(duì)應(yīng)的對(duì)象返回。

Hibernate有很多值得學(xué)習(xí)的地方,這里我們主要介紹Hibernate ThreadLocal,包括介紹Hibernate官方開(kāi)發(fā)手冊(cè)標(biāo)準(zhǔn)示例等方面。

Hibernate ThreadLocal

它會(huì)為每個(gè)線程維護(hù)一個(gè)私有的變量空間。實(shí)際上, 其實(shí)現(xiàn)原理是在JVM 中維護(hù)一個(gè)Map,這個(gè)Map的key 就是當(dāng)前的線程對(duì)象,而value則是 線程通過(guò)Hibernate ThreadLocal.set方法保存的對(duì)象實(shí)例。當(dāng)線程調(diào)用Hibernate ThreadLocal.get方法時(shí), Hibernate ThreadLocal會(huì)根據(jù)當(dāng)前線程對(duì)象的引用,取出Map中對(duì)應(yīng)的對(duì)象返回。

這樣,Hibernate ThreadLocal通過(guò)以各個(gè)線程對(duì)象的引用作為區(qū)分,從而將不同線程的變量隔離開(kāi)來(lái)。

Hibernate官方開(kāi)發(fā)手冊(cè)標(biāo)準(zhǔn)示例:

  1. public class HibernateUtil {   
  2. private static SessionFactory sessionFactory;  
  3. static {   
  4. try {   
  5. // Create the SessionFactory sessionFactory = new Configuration().
    configure().buildSessionFactory();  
  6. }   
  7. catch (HibernateException ex) {   
  8. throw new RuntimeException( "Configuration problem: " + ex.getMessage(), ex );  
  9. }   
  10. }   
  11. public static final ThreadLocal session = new ThreadLocal();  
  12. public static Session currentSession() throws HibernateException {   
  13. Session s = (Session) session.get();  
  14. // Open a new Session, if this Thread has none yet if (s == null) {   
  15. s = sessionFactory.openSession();  
  16. session.set(s);  
  17. }   
  18. return s;  
  19. }   
  20. public static void closeSession() throws HibernateException {   
  21. Session s = (Session) session.get();  
  22. session.set(null);  
  23. if (s != null) s.close();  
  24. }   

通過(guò)filter實(shí)現(xiàn)session的重用:

  1. public class PersistenceFilter implements Filter {   
  2. protected static ThreadLocal hibernateHolder = new ThreadLocal();  
  3. public void doFilter(ServletRequest request, 
    ServletResponse response, FilterChain chain) 
  4. throws IOException,ServletException {   
  5. hibernateHolder.set(getSession());  
  6. try {   
  7. ……   
  8. chain.doFilter(request, response);  
  9. ……   
  10. }   
  11. finally {   
  12. Session sess = (Session)hibernateHolder.get();  
  13. if (sess != null) { hibernateHolder.set(null);  
  14. try { sess.close(); } catch (HibernateException ex) {   
  15. throw new ServletException(ex);  
  16. }   
  17. }   
  18. }   
  19. }   
  20. ……  

【編輯推薦】

  1. 描述Hibernate使用JCA
  2. Hibernate cartridge學(xué)習(xí)總結(jié)
  3. 淺析Hibernate實(shí)現(xiàn)對(duì)象持久化
  4. 簡(jiǎn)述Hibernate Synchronizer
  5. 概述Hibernate Session
責(zé)任編輯:佚名 來(lái)源: IT168
相關(guān)推薦

2009-09-29 10:12:03

Hibernate A

2009-09-25 16:08:12

Hibernate f

2009-09-24 10:50:31

Hibernate主鍵

2009-09-24 18:11:56

Hibernate q

2009-09-29 14:03:14

Hibernate數(shù)據(jù)

2009-09-23 15:50:21

Hibernate u

2009-09-28 11:30:53

Hibernate核心

2009-09-25 17:03:29

Hibernate是什

2009-09-24 17:11:53

Hibernate處理

2009-06-16 14:36:54

Hibernate繼承

2009-09-27 17:23:16

Hibernate應(yīng)用

2009-09-24 10:22:38

Hibernate3新

2009-09-24 11:04:56

Hibernate二級(jí)

2024-10-28 08:15:32

2018-04-09 08:17:36

線程ThreadLocal數(shù)據(jù)

2011-07-14 13:50:09

ThreadLocal

2021-01-19 05:24:36

ThreadLocal線程編程

2015-09-09 08:45:49

JavaThreadLocal

2023-10-07 08:26:40

多線程數(shù)據(jù)傳遞數(shù)據(jù)共享

2021-05-06 08:55:24

ThreadLocal多線程多線程并發(fā)安全
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)