We are trying to build a system, which "produces" an entitymanager depending on the logged-in user (kind of multitenancy). Therefor we implemented a stateless ejb like this:
@Stateless
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public class CustomEntityManagerFactory {
@PersistenceContext(unitName = "EM1")
private EntityManager em1;
@PersistenceContext(unitName = "EM2")
private EntityManager em2;
@Produces
@RequestScoped
public EntityManager getEntityManager() {
// check which entitymanager to return
}
}
The entitymanager is injected like this:
@Stateless
public class EmployeeService {
@Inject
private EntityManager em;
...
}
This producer works as long as only an entitymanager without extended persistence context is needed (in stateless ejb). Unfortunately we also have some stateful ejbs, which need the extended persistence context. Is there a way to implement a CDI producer for this purpose or does this approach only work for stateless ejb with transactional entitymanager?