简体   繁体   English

为什么无状态会话bean是单线程的?

[英]Why stateless session beans are single threaded?

As per my understanding stateless session beans are used to code the business logic. 根据我的理解,无状态会话bean用于编写业务逻辑。 They can not store data in their instance variables because their instance is shared by multiple requests. 它们无法在实例变量中存储数据,因为它们的实例由多个请求共享。 So they seem to be more like Singleton classes. 所以他们似乎更像是单身人士课程。 However the difference is contain creates (or reuses from pool) the separate instance of stateless session beans for every request. 但是,区别在于包含为每个请求创建(或从池中重用)无状态会话Bean的单独实例。

After googling I could find the reasoning that the Java EE specification says they are suppose to be single threaded. 谷歌搜索后,我可以找到Java EE规范说他们认为是单线程的原因。 But I can't get the reason why the are specified to be SINGLE THREADED ? 但我无法理解为什么指定为单线程

The SLSBs are single threaded because of the TX Context, Principal is associated with a bean instance when it is called. 由于TX Context,SLSB是单线程的,Principal在调用时与bean实例相关联。 These beans are pooled and unless the max pool size is reached are processed in separate threads ( Vendor dependent). 这些bean是合并的,除非达到最大池大小在单独的线程中处理(依赖于供应商)。

If SLSBs were designed thread safe every call would have looked like a servlet doGet/Post with request info containing Tx Context , Security Context info and etc. So at least the code looks clean (developer dependent). 如果SLSB设计为线程安全,则每个调用看起来都像servlet doGet / Post,其请求信息包含Tx上下文,安全上下文信息等。因此,至少代码看起来很干净(取决于开发人员)。

The primary reason stateless session beans are single threaded is to make them highly scalable for the container. 无状态会话bean是单线程的主要原因是使它们对容器具有高度可伸缩性。 The container can make a lot of simplifying assumptions about the runtime environment. 容器可以对运行时环境做出许多简化的假设。 A second reason is to make life easier for the developer because the developer doesn't have to worry about any synchronization or re-entrancy in his business logic because the bean will never be called in another thread context. 第二个原因是让开发人员的生活更轻松,因为开发人员不必担心他的业务逻辑中的任何同步或重新进入,因为bean永远不会在另一个线程上下文中调用。

I remember the reasoning being discussed in the reviews of the original EJB 1.0 specification. 我记得原始EJB 1.0规范的评论中讨论的原因。 I would look at the goals section of the specification. 我会看一下规范的目标部分。 See http://java.sun.com/products/ejb/docs.html for the list of specifications. 有关规范列表,请参见http://java.sun.com/products/ejb/docs.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM