• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting IllegalStateException

 
Krishna Attravanam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This probably might sound like a beginner's question but I have been wondering on how to get this to work in EJB3. Ofcourse EJB3 uses EntityManager API behind the scenes for Persistence, I would like to know why did code throws an IllegalStateException.

class SomeClass {

@PersistenceUnit
private EntityManagerFactory emf;

.....

public void someMethod() {
EntityManager em = emf.getEntityManager();
ClassB b = new ClassB();
while (someCondition) {
b.setEntityManager(em);
b.call();
}
em.close();
}

} //End of SomeClass

//Begin ClassB
class ClassB {

private EntityManager em;

public void setEntityManager(EntityManager em) {
this.em = em;
}

public void call() {
em.getTransaction().begin();
//doSomething();
em.commit();

}
} // End of ClassB
I have simplified this code not to show any exception handling etc....but I was wondering if there's anything wrong with em.getTransaction().

This is what I understand from the code.

The EntityManagerFactory is created by the container (in this case an EJB3 container). Each single instance of EntityManager is managed by the application and so is the Transaction ??

THats where I am lost ... ANy thoughts?

Thanks
K
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic