I am developing a JavaEE6 based web application using JBoss7. In my application I am injecting the EntityManager in my EJBs as:
class ForumServiceEJB
{
@PersistenceContext(type=EXTENDED)
private EntityManager em;
}
class TopicServiceEJB
{
@PersistenceContext(type=EXTENDED)
private EntityManager em;
}
The problem when I update some data using ForumServiceEJB's EntityManager then the changes are made into DB but TopicServiceEJB's EntityManager is not able to see those changes and the results are always fetched from Cache.
I am using ExtendedPerssisteenceContext as My Entities contain child Entity Collections of Lazy Loading type.
How can I use/Inject EntityManager of type ExtendedPersistenceContext and make different EntityManager in one EJB can still see the changes done by other different EJB EntityManagers?
Somewhere I read EntityManagers should be RequestScoped objects.
public class MyEntityManagerProducers {
@Produces @RequestScoped
public EntityManager createDbEm() {
return Persistence.createEntityManagerFactory("forumDb").
createEntityManager();
}
public void disposeUdEm(@Disposes EntityManager em) {
em.close();
}
Is it the way to go?