Reputation: 6802
When I have a stateless service class using a singleton JPA DAO and lots of clients ask for the same method simultaneously, does EJB pile up the remaining requests? Are there a time/request list size limit for waiting? Does it know that only a single stateless at a time should access DAO? If yes, how it does it?
I thought if 100 requests are made, 100 stateless beans are instantiated, however only one DAO instance. Would this throw an exception or some sort of managing would occur? Having a DAO does make a pool of stateless services useless?
And finally, what is necessary to have the correct behavior, I mean, stateless beans waiting in line to have a moment with singleton DAO?
Upvotes: 2
Views: 898
Reputation: 1271
No, it won't throw any exception, container will make your 99 threads waiting while the one will access the method, if you use
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
and
@Lock(WRITE)
Nearly the same as if you have used synchronized block.
It looks to be a bad design however, if you assume so many threads could be blocked on a synchronized method.
Why do you need your DAO to be a singleton? I'd say it is not typical.
Upvotes: 1
Reputation: 2152
Access to Singleton
is synchronized by EJB
container. Check out documentation section about concurency management. This from docs:
Singleton session beans are designed for concurrent access, situations in which many clients need to access a single instance of a session bean at the same time. A singleton’s client needs only a reference to a singleton in order to invoke any business methods exposed by the singleton and doesn’t need to worry about any other clients that may be simultaneously invoking business methods on the same singleton.
I recommend to use this part of documentation:
Annotate a singleton’s business or timeout method with @Lock(READ) if the method can be concurrently accessed, or shared, with many clients. Annotate the business or timeout method with @Lock(WRITE) if the singleton session bean should be locked to other clients while a client is calling that method. Typically, the @Lock(WRITE) annotation is used when clients are modifying the state of the singleton.
Or you can control synchronization process by yourself by using bean-managed concurency
. Then you need to add synchronized
locks or whatever you used to control access to shared resource. Container will not participate on that in such case.
Upvotes: 1