EJB and Synchronization

2019-04-07 17:56发布

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

5条回答
爷的心禁止访问
2楼-- · 2019-04-07 18:35

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.

查看更多
走好不送
3楼-- · 2019-04-07 18:47

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-07 18:55

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

查看更多
迷人小祖宗
5楼-- · 2019-04-07 18:58

Session bean means there is logged user. If user accidentally double clicks on UI action two http requests are created in two separate threads.

  • Stateless beans are NOT SYNCHRONIZED. Every thread will get different instance and both are executing the logic paralely. Both can access the same resources!
  • Statefull beans are synchronized by container. Both threads will get the same ejb instance. Second thread is waiting to finish first one. No problem.
查看更多
The star\"
6楼-- · 2019-04-07 18:58

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

查看更多
登录 后发表回答