EJB and Synchronization

荒凉一梦 提交于 2019-12-06 00:26:31

问题


Are Session Beans (stateless session beans, stateful session beans) Synchronized?


回答1:


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.




回答2:


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.

Statefull beans are 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.

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)).




回答3:


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.

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).




回答4:


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.

e.g.

@Statelss Class EJBclass {

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.

EJB 3.0 Beans are smart and performance is good.




回答5:


Enterprise java beans are not synchronized . As session beans are maintained by ejb container so you have to implement synchronization logic in application level.



来源:https://stackoverflow.com/questions/891333/ejb-and-synchronization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!