论坛首页 Java版 Hibernate

建议不使用ThreadLocal来管理SESSION的几个理由

浏览 12420 次
该帖已经被评为精华帖
作者 正文
时间:2004-03-01
用JAVA做服务器端程序碰到最大的问题是什么?我想大多数人都会回答:占用太多系统资源,占用太多内存.SERVLET和JSP容器为了解决JAVA速度慢的问题,在第一个人访问网站时,就把程序自动放入内存中,以后再有人访问,就不再去读写硬盘了.所以JSP容器总是占用了太多的系统资源.

使用ThreadLocal来管理SESSION,将会增加联结时间,就意味着更多的资源不能及时释放.在用户并发数很大的情况下,这种做法是不合适的.

不要使用ThreadLocal,在加大数据库联结池的基础上,只使用SESSION.用后马上关闭.

当然,你做的程序是B/S结构,才会有这样的好处!
   
时间:2004-03-01
具体情况具体分析,根据自己的需要进行选择吧
   
0 请登录后投票
时间:2004-03-01
其实threadlocal只是保证session是在某一线程中的唯一,由此可以在全部数据库操作结束后再关session.所以其实这样并非占用了不该占用的资源,因为这些数据库操作都需要得到连接,你又何必关了又开呢?再说,你可以再你使用后及时关闭。
   
0 请登录后投票
时间:2004-03-01
In a web-based application, a servlet filter can be used to close the Session only at the very end of a user
Hibernate 2.0.2 35
request, once the rendering of the view is complete. Of course, this places heavy demands upon the correctness
of the exception handling of your application infrastructure. It is vitally important that the Session is
closed and the transaction ended before returning to the user, even when an exception occurs during rendering
of the view. The servlet filter has to be able to access the Session for this approach. We recommend
that a ThreadLocal variable be used to hold the current Session.
Collections

-如果不用threadlocal保存session,有什么好的方法吗?
谢谢
   
0 请登录后投票
时间:2004-03-01
如果你要在多个层次上使用同一session的话,除了threalocal好像我还不知道有什么其他方法。
   
0 请登录后投票
时间:2004-03-01
首先感谢四位网友的回答:

第一位网友,等于没回答.

第二位网友,没有考虑到如果SESSION没有及时关闭,那么肯定多占用了那么一点资源(当然还是数据库联结的资源).在多用户访问的情况下,占用的资源就由少变多了.所以我就觉得用threadlocal没好处,只有坏处.

第三个网友的那一大段英文说:SESSION 在filter中应用,并说有推荐使用,却好象没有说明有什么好处.并问 如果不用threadlocal保存session,有什么好的方法吗?那为什么要用 threadlocal.

第四个网友说:要在多个层次上使用同一session. 给个理由,为什么要这么做.我们只要在不同层上使用SESSIONFACTORY的单例就可以了.

我在不同的公司工作过,常常为程序用户量大的问题而束手无策. 比如APACHE在36个并发情况下,同时1400人在线,服务器就快死了,APACHE的并发达到43,就普通SERVER服务器就玩完了.
所以,我认为节约资源是很必要的.用ThreadLocal好处安在?
   
0 请登录后投票
时间:2004-03-01
http://www.west-cn.com:8080/bbs/board.jsp 没有使用TheadLocal做的小程序,人人都说速度快,占用的资源也很少.

系统构架为 : STRUTS+FACADE+DAO+HIBERNATE
   
0 请登录后投票
时间:2004-03-02
打个比方,如果你通过dao返回了一个对象,这个对象有一个集合的属性,是懒加载的,如果你希望在页面jsp层再初始化的话,那你的session是不能在dao层就关了的。这也是为什么hibernate手册中有以下论述:
引用

In some application architectures, particularly where the code that accesses data using Hibernate, and the code
that uses it are in different application layers, it can be a problem to ensure that the Session is open when a collection
is initialized. They are two basic ways to deal with this issue:
• In a web-based application, a servlet filter can be used to close the Session only at the very end of a user
request, once the rendering of the view is complete. Of course, this places heavy demands upon the correctness
of the exception handling of your application infrastructure. It is vitally important that the Session is
closed and the transaction ended before returning to the user, even when an exception occurs during rendering
of the view. The servlet filter has to be able to access the Session for this approach. We recommend
that a ThreadLocal variable be used to hold the current Session.
• In an application with a seperate business tier, the business logic must "prepare" all collections that will be
needed by the web tier before returning. Usually, the application calls Hibernate.initialize() for each
collection that will be needed in the web tier (this call must occur before the session is closed) or retrieves
the collection eagerly using a query with a FETCH clause.
Collections
   
0 请登录后投票
时间:2004-03-02
另外session是否已经及时关闭完全取决于编码者,如果你很清楚什么时候不需要使用session了,你一定会马上关闭它的,就像你一般地关闭数据库连接一样。且session占用数据库连接的时间主要是发生数据库操作时,并非执行非数据库代码的时间,没有必要为了及时释放资源而频繁地释放再获取session,这样反而会带来额外的性能上的损失。如果要解决高并发性,在session上作文章能带来多大好处很难说,立杆见影的做法是提高你的数据库连接池的连接数量并优化你的程序使数据库访问的程序效率更高。不要应为threadlocal带来的一点不好的地方就完全抛弃它,所谓拣了芝麻丢了西瓜。如果你觉得还有需要说明的,可以作一个测试,用数据来说明你的方式有多优越。
   
0 请登录后投票
时间:2004-03-02
dhj1 写道
http://www.west-cn.com:8080/bbs/board.jsp 没有使用TheadLocal做的小程序,人人都说速度快,占用的资源也很少.

系统构架为 : STRUTS+FACADE+DAO+HIBERNATE


负载能力有点差啊,用微软的WAS,给10并发就撑不住了。
   
0 请登录后投票
论坛首页 Java版 Hibernate

跳转论坛:
JavaEye推荐