java EJB 和同步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/891333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EJB and Synchronization
提问by Pahari Chora
Are Session Beans (stateless session beans, stateful session beans) Synchronized?
会话 Bean(无状态会话 Bean、有状态会话 Bean)是否同步?
采纳答案by Rob Di Marco
Only one thread at a time will be accessing your beans. It is up to the application server to manage this. So you should not be using synchronized from within your beans. This is why a non-threadsafe like EntityManager can be an instance value and not have synchronization issues.
一次只有一个线程会访问您的 bean。由应用程序服务器来管理它。所以你不应该在你的 bean 中使用同步。这就是为什么像 EntityManager 这样的非线程安全可以是实例值并且没有同步问题的原因。
回答by Peter ?ály
Stateless beans: Every thread/request will get different instance of EJB from pool. SLB should not hold any user session data, any state. The same code may be executed in parallel. One instance is accessed by one thread at a time.
无状态 bean:每个线程/请求将从池中获取不同的 EJB 实例。SLB 不应持有任何用户会话数据、任何状态。可以并行执行相同的代码。一个线程一次访问一个实例。
Statefull beansare synchronized for user session. Every user will get own session scoped instance. Second thread/request will wait until the first thread finishes. Statefull EJB can hold user specific data. One user cannot execute same code in parallel. Different users may execute same code in parallel.
有状态的 bean为用户会话同步。每个用户都将获得自己的会话范围实例。第二个线程/请求将等到第一个线程完成。Statefull EJB 可以保存用户特定的数据。一个用户不能并行执行相同的代码。不同的用户可以并行执行相同的代码。
If accessing a resource that does not allow parallel access use Singleton EJB. As name implies there is only one instance. By default EJB Singleton can be accessed only by one thread (Container Managed Concurrency and @Lock(WRITE)).
如果访问不允许并行访问的资源,请使用Singleton EJB。顾名思义,只有一个实例。默认情况下,EJB Singleton 只能由一个线程(Container Managed Concurrency 和 @Lock(WRITE))访问。
回答by Sriram
Stateless/Stateful session beans are thread safe. Because each request will get a dedicated instance of the bean and so it doesn't need to be synchronized.
无状态/有状态会话 bean 是线程安全的。因为每个请求都会获得一个专用的 bean 实例,所以不需要同步。
Singleton session beans are shared and needs to be synchronized either by the container (Container Managed Concurrency - CMC) or by the user (Bean Managed Concurrency - BMC).
单例会话 bean 是共享的,需要由容器(Container Managed Concurrency - CMC)或用户(Bean Managed Concurrency - BMC)同步。
回答by JavaSun
Very True thing about EJB beans is that once you have created EJB 3.0 beans then the methods of the EJB is by default Synchronized.
EJB bean 非常真实的一点是,一旦您创建了 EJB 3.0 bean,那么默认情况下 EJB 的方法是同步的。
e.g.
例如
@Statelss Class EJBclass {
@Statelss 类 EJBclass {
void someMethod(){ }
void someMethod(){ }
}
}
now if you will make this someMethod Synchronize it will show Error like it is can not be Synchronize at this level as it is synchronized.
现在,如果您将此 someMethod 同步,它将显示错误,因为它已同步,因此无法在此级别同步。
EJB 3.0 Beans are smart and performance is good.
EJB 3.0 Beans 很聪明,性能也很好。
回答by JavaSun
Enterprise java beans are not synchronized . As session beans are maintained by ejb container so you have to implement synchronization logic in application level.
企业 java bean 不同步。由于会话 bean 由 ejb 容器维护,因此您必须在应用程序级别实现同步逻辑。

