简体   繁体   English

EJB和同步

[英]EJB and Synchronization

会话Bean(无状态会话bean,有状态会话bean)是否已同步?

Only one thread at a time will be accessing your beans. 一次只有一个线程将访问您的bean。 It is up to the application server to manage this. 由应用程序服务器来管理它。 So you should not be using synchronized from within your beans. 所以你不应该在bean中使用synchronized。 This is why a non-threadsafe like EntityManager can be an instance value and not have synchronization issues. 这就是像EntityManager这样的非线程安全可以是实例值而没有同步问题的原因。

Stateless beans : Every thread/request will get different instance of EJB from pool. 无状态bean :每个线程/请求都将从池中获取不同的EJB实例。 SLB should not hold any user session data, any state. SLB不应该保存任何用户会话数据,任何状态。 The same code may be executed in parallel. 可以并行执行相同的代码。 One instance is accessed by one thread at a time. 一个线程一次访问一个实例。

Statefull beans are synchronized for user session. 为用户会话同步Statefull bean 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. Statefull EJB可以保存用户特定的数据。 One user cannot execute same code in parallel. 一个用户不能并行执行相同的代码。 Different users may execute same code in parallel. 不同的用户可以并行执行相同的代码。

If accessing a resource that does not allow parallel access use Singleton EJB . 如果访问不允许并行访问的资源,请使用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)). 默认情况下,EJB Singleton只能由一个线程(Container Managed Concurrency和@Lock(WRITE))访问。

Stateless/Stateful session beans are thread safe. 无状态/有状态会话bean是线程安全的。 Because each request will get a dedicated instance of the bean and so it doesn't need to be synchronized. 因为每个请求都将获得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). Singleton会话bean是共享的,需要由容器(Container Managed Concurrency - CMC)或用户(Bean Managed Concurrency - BMC)同步。

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的方法默认是Synchronized。

eg 例如

@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很聪明,性能也很好。

Enterprise java beans are not synchronized . 企业java bean未同步。 As session beans are maintained by ejb container so you have to implement synchronization logic in application level. 由于会话bean由ejb容器维护,因此您必须在应用程序级别实现同步逻辑。

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

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