-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: EntityManager closed after subsequent envocation
PostPosted: Sun Sep 07, 2008 7:43 pm 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello,
i have a stateful session bean, which declares a dependancy on EntityManager:
Code:
@PersistenceContext(
         type = PersistenceContextType.EXTENDED,
         properties =
            @PersistenceProperty(
                  name = "org.hibernate.flushmode",
                  value = "MANUAL"))


stateful session bean also declares a dependancy on a stateless DAO. Stateful session bean has method: createEntity:

Code:
public JobSeeker createJobSeeker(JobSeeker seeker) {
      seeker = jobSeekerDAO.makePersistent(seeker);
      return seeker;
}


Stateful bean as you may guess defines a dependancy on a stateless via @EJB. Stateful bean also has a @Remove method (public void submit()) - which flushes entity manager. Similar example is in Java Persistance With Hibernate on pg 727. The stateless is implemented as suggested in the example: stateless extends abstract class GenericEJB3DAO wich defines methods basic to all entities: makePersistent. Here is the caveat: stateless is a @Local bean stateful is remote, when i obtain reference to stateful, first execution of createJobSeeker, followed by submit - persists the entity (although does it in stateless bean not on submit invokation), the following invocation of the same method throws an exception:

Code:
java.lang.IllegalStateException: EntityManager is closed at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:66) at org.hibernate.ejb.AbstractEntityManagerImpl.getDelegate(AbstractEntityManagerImpl.java:559)
   at www.freejobagent.com.dao.GenericEJB3DAO.getSession(GenericEJB3DAO.java:93)
   at www.freejobagent.com.dao.GenericEJB3DAO.makePersistent(GenericEJB3DAO.java:59)


Now when i invoke same set of methods in about 1 - 2 minutes, its okay again. Shouldn't the container set my stateless EntityManager with the one in a stateful session bean and not keep the old closed entityManager. This is deployed on glassfish.

Thank you.


Top
 Profile  
 
 Post subject: Re: EntityManager closed after subsequent envocation
PostPosted: Mon Sep 08, 2008 3:07 pm 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello,
i forgot to mention an important detail. Here is the problem i got faced with at first:

Code:
private EntityManager em;
private Session session;

@PersistentContext
private void setEntityManager(EntityManager em) {
   // session = (Session) em.getDelegate();  // throws ClassCastException
                                                                // cannot cast from
                                                                // EntityManagerImpl to Session
   this.em = (EntityManager) em.getDelegate(); // ok
   session = (Session) this.em.getDelegate(); // ok now
}


from preceding stateless everything is legal, except the code is buggy, in a scene that when ever container pooled the stateless it kept the EntityManagerImpl - which was by now closed for sure. Two questions:
1) when i work with session obtained in the followign way (actually thats what i did - having original EntityManager as a type injected the correct one always):

Code:
public Session getSession() {
   EntityManager emLocal = em.getDelegate();
   return (Session) emLocal.getDelegate();
}


from preceding, will session be automatically managed, by hibernate and be associated with JTA? I don't have to close it or separately create transactions right?

2) calling getDelegate() twice seems repetitive, is that how it is supposed to be?. I bet on JBoss 1 call is enough.

Thank you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.