java - session 范围的支持 bean 是否必须以线程安全的方式实现?

标签 java multithreading jsf servlets specifications

是否有可能多个线程同时访问 session 范围的支持 bean?

servlet 规范说,这是可能的:

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.

但是我无法让服务器 (JBoss) 对同一个 session 使用不同的线程。当我打开多个选项卡并在一个选项卡中启动一个长时间运行的请求,然后在另一个选项卡中启动一个请求时,第二个选项卡必须等待响应,直到第一个选项卡中启动的操作完成。

我还通过在支持 bean 中使用断点阻塞线程来验证这一点。在我恢复线程之前,无法在同一 session 的其他选项卡中执行任何操作。

尽管如此,我们在生产日志中还是有一些奇怪的异常,到目前为止,我们唯一可能的解释是,多个线程同时访问同一个 session 范围的支持 bean。

最佳答案

是的,Servlet session 是线程安全的。但是,如果您将可变对象放入 session 中。应用程序应负责同步。

在您的情况下,如果您的 Bean 是可变的,即具有状态。是的,它必须是线程安全的。

关于您的测试用例,这取决于您使用的浏览器。大多数浏览器支持每个服务器最多 6 个并行连接。但是,如果有 cookie,不确定他们是否使用并行连接。

关于java - session 范围的支持 bean 是否必须以线程安全的方式实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10932066/

相关文章:

java - CAS 单点注销不起作用

java - 如何使用线程池读取多个文件?

java - 如何将对象从 primefaces 上的树传递到 View

javascript - 如何向jsf标签graphicImage添加多个事件

jsf - 有没有办法以编程方式检测用户何时在浏览器中按下 F5 按钮?

java - Firebase 身份验证服务器错误

java - 引导 Swing 应用程序

java - 使用单循环进行图案打印

java - 在开始另一个方法之前等待方法完成的简单方法

ios - 使用 NSURLConnection 是否需要考虑线程安全? - iOS