I am trying to inject a EJB in POJO by using context lookup. What I am expecting is a stateless behavior of EJB as you get when you do a
@EJB annotation
The EJB has a entityManager which I get from the EntityManagerFactory in the constructor for thr EJB
@Stateless
Class ReprovProcess implements ReprovisioningProcess {
protected EntityManager em;
public ReprovProcess(){
//init em from entityManagerFactory;
}
public EntityManager getEm(){
return em;
}
}
@LocalBinding(jndiBinding = "ReprovProcess/local")
class interface ReprovisioningProcess {
}
Next I look up the EJB twice such that
on first retrieval I close the entitymanager
and then on second lookup I should see the EntityManager is open as I am expecting a Stateless EJB like behavior.
But I am not observing this. The second time I do a lookup and do an em.isOpen()
, I get a false.
So the question is: Can context.lookup be used for obtaining a stateless EJB like behavior? If not what can be used?
ReprovisioningProcess pro = (ReprovisioningProcess)
ic.lookup("ReprovProcess/local");
EntityManager em = pro.getEm();
System.out.println("Entity Manager State = "+em.isOpen());
em.close();
System.out.println("Entity Manager State = "+em.isOpen());
pro = (NetElementReprovisioningProcess) ic.lookup("ReprovProcess/local");
em = pro.getEm();
System.out.println("Entity Manager State = "+em.isOpen());
em.close();
System.out.println("Entity Manager State = "+em.isOpen());
Output is
Entity Manager State = true
Entity Manager State = false
Entity Manager State = false
------------- ---------------- ---------------
EntityManager is closed
java.lang.IllegalStateException: EntityManager is closed
at org.hibernate.ejb.EntityManagerImpl.close(EntityManagerImpl.java:97)
at com.cisco.cgms.factoryconfig.reprovision.ReprovisiongGroupTest.testIntialLookup(ReprovisiongGroupTest.java:135)