Reputation: 583
@Singleton EJBs like this:
@Singleton
public class MySingleton {
@PersistenceContext
private EntityManager em;
...
@Lock(LockType.READ)
public void doPersistanceAction() {
}
}
So all callers of MySingleton#doPersistanceAction() would use the same instance of EntityManager simultaneously. And persistence context with attached entitys will share between callers. And transaction started by one caller may be commited by another. Whether the ejb container handle such a situation?
Upvotes: 1
Views: 1296
Reputation: 11612
So all callers of MySingleton#doPersistanceAction() would use the same instance of EntityManager simultaneously
Yes, but it isn't a preferred & isn't thread-safe. Instead you should inject EntityManagerFactory
& then within method you can get EntityManager
from it.
And persistence context with attached entitys will share between callers
Yes, same EntityManager
instance is shared.
And transaction started by one caller may be commited by another
LockType.READ : For read-only operations. Allows simultaneous access to methods designated as READ, as long as no WRITE lock is held.
Therefore, as you have read-only operation, it shouldn't matter, but you need to revisit design.
Whether the ejb container handle such a situation?
Container will be responsible for initialization, injection, concurrency etc. for singleton bean, but you have to utilize it correctly.
Upvotes: 1