Java Servlet 上下文和会话级别变量

发布于 2024-10-27 00:57:08 字数 172 浏览 0 评论 0原文

我已经尝试了多个线程访问/修改上下文变量时的不一致,但无法在会话级别产生相同的行为。例如,当同一 sessionid 的两个请求(这意味着两个线程)进来时,在服务方法中调用 session.setAttribute("something") 方法不会导致竞争条件。是因为 Tomcat 为会话变量提供了线程安全性还是我有完全错了?

I have experimented the inconsistency when multiple threads access/modify context variables but could not produce the same behaviour at session level. For example calling session.setAttribute("something") method within the service method does not cause race-condition when two requests (which means two threads) for same sessionid come in. Is it because Tomcat provides thread safety for session variables or I have got in completely wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2024-11-03 00:57:08

Servlet 规范 3.0 版在第 7.7.1 节中明确指出对会话密钥的访问是线程安全的。
但是,对存储在这些键下的元素的访问不是线程安全的。在这种情况下,线程安全必须由应用程序开发人员确保。

7.7.1 线程问题
执行请求线程的多个 Servlet 可以主动访问同一个请求线程
同时会话对象。容器必须确保操作
表示会话属性的内部数据结构在线程中执行
安全的方式。开发人员有责任线程安全地访问
属性对象本身。这将保护内部的属性集合
HttpSession 对象免受并发访问,消除了
示例代码

来说明这一点:

HttpSession session;
List items;
session.put("cart", items); // thread1 writes cart reference to session, this is thread-safe
...
items = session.get("cart"); // thread1 reads cart reference from session, this is thread-safe
items.get(0); // access to elements of application collection is *not* thread-safe, you must use explicit synchronization here.

我相信这里“线程安全方式”的意思是 HttpSession 访问方法保证是线程安全的,但是通过这些元素的方法对会话中存储的元素的所有访问都不能保证是线程安全的。

Servlet spec ver 3.0 explicitly states that access to session keys is thread-safe, in section 7.7.1.
However, access to elements stored under these keys isn't thread-safe. Thread safety in this case must be ensured by application developer.

7.7.1 Threading Issues
Multiple servlets executing request threads may have active access to the same
session object at the same time. The container must ensure that manipulation of
internal data structures representing the session attributes is performed in a thread
safe manner. The Developer has the responsibility for thread safe access to the
attribute objects themselves. This will protect the attribute collection inside the
HttpSession object from concurrent access, eliminating the opportunity for an
application to cause that collection to become corrupted.

Sample code to illustrate this:

HttpSession session;
List items;
session.put("cart", items); // thread1 writes cart reference to session, this is thread-safe
...
items = session.get("cart"); // thread1 reads cart reference from session, this is thread-safe
items.get(0); // access to elements of application collection is *not* thread-safe, you must use explicit synchronization here.

I believe what is meant here by "thread safe manner" is that HttpSession access methods are guaranteed to be thread-safe, but all access to elements stored in session by methods of these elements is not guaranteed to be thread-safe.

書生途 2024-11-03 00:57:08

经过一番阅读后(除其他此错误修复),我确实得到了强烈的印象是会话是线程安全的,或者应该是线程安全的。

After a reading a bit (amongst others this bug fix) I do get the strong impression that sessions are thread-safe, or should be.

一桥轻雨一伞开 2024-11-03 00:57:08

你知道所有上下文变量都是线程不安全的,除了本地变量,所以如果你试图访问/修改这个上下文变量,请使用锁,在java中它是同步对象,有关更多信息,请阅读Head First Servlet 和JSP - 第 5 章。祝你好运!

you know all context variables are thread unsafe, except local, so if you are trying to access/modify this context variables use locks, in java it is synchronized objects, for more info read Head First Servlets & JSP - Chapter 5. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文