CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  框架、开源

关于Hibernate 中使用 ThreadLocal 管理 Session 的疑惑.

楼主CAYU(中原)2004-12-03 22:37:11 在 Java / 框架、开源 提问

这是网上使用的一段代码,但我感觉有几个问题.  
  如果同时调用两次   currentSession()   得到的是同一个   Session,一旦关闭一个,另一个也就关闭了。  
  在这样的情况下,如果做包含查询,子查询一但关闭了Session就会出错.   我希望每个Session有自己的  
  ThreadLocal,应该如何做,比较好.  
   
  public   static   final   ThreadLocal   session   =   new   ThreadLocal();  
  public   static   Session   currentSession()   throws   HibernateException  
  {  
  Session   s   =   (Session)   session.get();  
  if   (s   ==   null)   {  
  s   =   sessionFactory.openSession();  
  session.set(s);  
  }  
  return   s;  
  }  
  public   static   void   closeSession()   throws   HibernateException   {  
  Session   s   =   (Session)   session.get();  
  session.set(null);  
  if   (s   !=   null)  
  s.close();  
  }  
  }  
  问题点数:200、回复次数:4Top

1 楼catblue(佛家说:一粒沙中看世界。)回复于 2004-12-04 15:25:54 得分 60

不会,帮你顶一下。Top

2 楼zh_baiyu(SkyBay)回复于 2004-12-04 16:12:56 得分 60

顶,干点力气活Top

3 楼cm4ever(小P[Fly Away])回复于 2004-12-05 00:40:50 得分 80

//注意:这里的session用完之后并不close,而只flush一下  
          public   void   delCat(String   strCatId)   {  
                  try   {  
                          Session   s   =   HibernateSessionFactory.currentSession();  
   
                          Object   cat   =   s.load(Cat.class,   strCatId);  
                          s.delete(cat);  
                          s.flush();  
                  }   catch   (HibernateException   e)   {  
                          e.printStackTrace();  
                  }  
          }  
   
  ----------  
  http://www.hibernate.org.cn:8000/92.htmlTop

4 楼CAYU(中原)回复于 2004-12-05 19:27:12 得分 0

网上的代码被简化了,很不安全,spring   的方法  
   
  Steve   Ebersole:   Here   is   a   variation   on   the   above   ThreadLocalSession.   The   main   difference   is   that   it   undersands   the   concept   of   nested   calls.   Note   that   this   usage   is   meant   specifically   for   use   in   CMT   session   beans.  
   
  public   class   ThreadLocalSession  
  {  
          private   static   final   ThreadLocal   sessionContext   =   new   ThreadLocal();    
   
          private   Session   session;  
          private   int   level;  
   
          public   static   Session   currentSession()  
          throws   HibernateException  
          {  
                  SessionFactory   factory   =   ...;  
                  ThreadLocalSession   tlSession   =   (ThreadLocalSession)sessionContext.get();  
                  if   (tlSession   ==   null)  
                  {  
                          tlSession   =   new   ThreadLocalSession();  
                          tlSession.session   =   factory.openSession();  
                          tlSession.level   =   0;  
                          sessionContext.set(   tlSession   );  
                  }  
                  tlSession.level++;  
                  return   tlSession.session;  
          }  
   
          public   static   void   closeSession()  
          throws   HibernateException  
          {  
                  ThreadLocalSession   tlSession   =   (ThreadLocalSession)sessionContext.get();  
                  if   (tlSession   ==   null)  
                  {  
                          return;  
                  }  
   
                  tlSession.level--;  
                  if   (tlSession.level   <=   0)  
                  {  
                          if   (tlSession.session   !=   null   &&   tlSession.session.isOpen())  
                          {  
                                  tlSession.session.close();  
                          }  
                          sessionContext.set(   null   );  
                  }  
          }  
   
  }  
   
  Top

相关问题

  • hibernate 在update 时的疑惑?
  • 关于session的疑惑
  • 关于Session的两点疑惑
  • java.lang.NoClassDefFoundError: org/hibernate/Session
  • 疑惑!
  • 疑惑!
  • 疑惑??????
  • 疑惑
  • 疑惑!!!
  • 疑惑!?

关键词

  • null
  • threadlocal
  • currentsession
  • session
  • hibernateexception
  • 关闭
  • cat
  • public static

得分解答快速导航

  • 帖主:CAYU
  • catblue
  • zh_baiyu
  • cm4ever

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo