I'm maintaining some older JEE code which runs fine but is using some static helper classes where an entity manager is passed in the methods from the calling EJB(s) like this:
public class StaticHelper {
public static void helpingOut(EntityManager entityManager, String value) {
// i.e. insert value
}
}
Since this doesn't seem to fit JEE very well and is not nice to unit-test, I've converted these helpers to @Stateless
EJBs like so:
@Stateless
public class StatelessHelper {
@PersistenceContext(unitName="SuperUnit")
private EntityManager entityManager;
public void helpingOut(String value) {
// i.e. insert value
}
}
Like that I can inject a mocked helper in the calling EJB with CDI-Unit.
Now, depending on the load, 1-3 instances of that stateless helper is created by the container which isn't a problem at all I would say, but anyway I thought about a @Singleton
using either @ConcurrencyManagement(ConcurrencyManagementType.BEAN)
or @Lock(LockType.READ)
to make it multithreaded - but this doesn't seem to be a good idea since EntityManager
is not thread-safe. Or does this explained here still apply?
"...The container serializes calls to each stateful and stateless session bean instance. Most containers will support many instances of a session bean executing concurrently; however, each instance sees only a serialized sequence of method calls. Therefore, a stateful or stateless session bean does not have to be coded as reentrant..."
EntityManager
instance yourself. The container does that. So the citation doesn't apply on it at all. – Denice