struts2中,session是线程安全的吗?

chenise 2017-03-19 11:21:43
首先说下我的理解,不一定对:session是由容器管理(如tomcat)的,而且我在action中往session中 put的值在一次请求结束后仍然存在,所以我觉得session不是 threadlocal的,action中的session只是对tomcat中的session的一个引用

基于以上猜测,我有以下疑问:tomcat中的session是线程安全的吗?(我个人觉得不是),如果是,理由。
如果不是,那么框架有没有生什么措施保证多个线程对一个session操作时 的安全?是不是要自己实现呢?

麻烦各位。
...全文
254 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
街头小贩 2017-03-20
  • 打赏
  • 举报
回复
下面引自:https://community.oracle.com/thread/1473709?db=5
引用
It may be true that session attributes are not thread-safe, but that is extremely unimportant. The normal problem with synchronization in servlets is with two different users accessing the same servlet. Those two users will normally have different sessions, so there is no synchronization issue with the two different sessions there.

It's possible that those two users are in fact the same user who opened two browser windows and clicked on things very fast to make two simultaneous requests to the same servlet. But I would not do anything in my programming to allow for that remote possibility.


google翻译的(本人太菜了,译不了)

街头小贩 2017-03-20
  • 打赏
  • 举报
回复
没研究过struts2,也不用它.我也问过别人(当然比我厉害的).java web需要作到线程安全吗?他说不需要

HttpSession session=request.getSession();
synchronize(session)
{
//setAttributes in the session object
//getAttributes from the session object
}
chenise 2017-03-20
  • 打赏
  • 举报
回复
引用 1 楼 xiaofanku 的回复:
没研究过struts2,也不用它.我也问过别人(当然比我厉害的).java web需要作到线程安全吗?他说不需要

HttpSession session=request.getSession();
synchronize(session)
{
//setAttributes in the session object
//getAttributes from the session object
}
谢谢你的回答。google的资料很好 你第一个回答说 不需要 但是代码却是需要的。 嗯 我已经弄明白了 其实session是线程不安全的,但是 同时操作一个session的情况很极端 谢谢解答

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧