为什么我们应该在 JSF 中使 SessionScoped ManagedBean 线程安全?
我知道 Application-Scope 会跨多个用户持续存在,因此很明显,我们应该确保所有 ApplicationScoped ManagedBean 都是线程安全的。
我还了解到,我们不需要关心 RequestScoped ManagedBean 的线程安全。这是因为它仅持续一个 HTTP 请求,并且如果引用它,就会为每个请求重新实例化。
但我不太清楚为什么我们应该担心 SessionScoped ManangedBean 的线程安全。尽管它在多个请求中持续存在,但每个用户都会获得他/她自己的实例,对吧?
那么,为什么我们需要担心 SessionScoped ManagedBeand 的线程安全性,这也适用于 ViewScoped ManagedBean 吗? ViewScope 在对同一视图的 2 个连续请求中持续存在,对吧?
I know that Application-Scope persists across multiple users, so it's obvious that we should make sure that all the ApplicationScoped ManagedBeans are thread safe.
I also understand that we don't need to care about thread safety for a RequestScoped ManagedBean. That's because it last only for one HTTP request, and is newly instantiated for each request if it's referenced.
But I am not quite sure why we should worry about thread safety for a SessionScoped ManangedBean. Even though it persists across multiple requests, each individual user gets his/her own instance of it, right?
So, why do we need to worry about thread safety in case of SessionScoped ManagedBeand, and does that apply to ViewScoped ManagedBean too? ViewScope persists across 2 consecutive requests for the same view, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您已经担心某个范围内数据的线程安全性,那么该数据很可能属于更窄的范围(即高层设计存在缺陷)。如果数据被放置在正确的范围内,那么完全没有理由担心线程安全。我假设您的 bean 的设计方式正确,它们不会在 吸气剂。
将应用程序范围用于应用程序范围内的数据/常量,例如对每个人都相同的下拉列表。使用客户端特定数据的会话范围,例如登录的用户和用户首选项(语言等)。将视图范围用于丰富的支持 ajax 的动态视图(基于 ajax 的验证、渲染等)。将请求范围用于简单和非 ajax 表单/演示文稿。
If you already worry about the threadsafety of the data in a certain scope, then the data most likely belongs in a more narrow scope (i.e. there is a flaw in the high level design). If the data is been put in the right scope, then there's totally no reason to worry about threadsafety. I assume that your beans are designed the right way that they aren't doing any business logic in the getters.
Use the application scope for application wide data/constants, such as dropdown lists which are the same for everyone. Use the session scope for client specific data, such as the logged-in user and user preferences (language, etc). Use the view scope for rich ajax-enabled dynamic views (ajaxbased validation, rendering, etc). Use the request scope for simple and non-ajax forms/presentations.