-->
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: Should Entity Manager be closed ??
PostPosted: Thu Jul 02, 2009 1:43 pm 
Newbie

Joined: Mon May 11, 2009 11:16 am
Posts: 13
Hi to everyone,

I've a little question. I work with Hibernate JPA in an simple Java Application, which runs in the JVM, not in some Application Server or something.
This application is a client server application where communication is done by RMI. The Database is at the servers machine, if you want to know

Sometimes I get an IllegalStateException with that the message that the EntityManager is closed when a client calls a remote method

Code:
java.lang.IllegalStateException: EntityManager is closed
at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:66)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:93)
at de.jhomuth.itbos.server.persistence.UserPersistenceFacade.getAllUsers(UserPersistenceFacade.java:166)
....
....


When I persist a user for example I will call the entitymanager.close() method. This method will called at other locations too, here only an example

Code:
    /**
     * This method adds a Service to the db
     * @param category
     */
    public void persist(User user) {
        em.getTransaction().begin();
        try {
            em.persist(user);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close(); //CLOSE CALL HERE
        }
    }


When I try to create a query with the entitymanger, after the close method was called, I get the provided exception

Code:
public User getUserByUid(String uid) {
        Query q = em.createQuery("SELECT user FROM User user where user.uid=:uid"); //EXCEPTION OCCURS AT THIS LOCATION
        q.setParameter("uid", uid);
        try {
            return (User) q.getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }


Only for information, this is the initializing code for the entityManager

Code:
   /**
     * The entity manager factory
     */
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("itbos_server2PU");
    /**
     * The entity manager
     */
    private EntityManager em = emf.createEntityManager();


Does anyone know if it is neccessary to call the close method or not? What will happen if I dont close the entitymanager, might a TooManyConnectionsException or something like this be thrown.

Thx for suggestions


Top
 Profile  
 
 Post subject: Re: Should Entity Manager be closed ??
PostPosted: Thu Jul 02, 2009 8:28 pm 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
It's been a while since I last used JPA, but my understanding is that you should obtain a fresh EntityManager for each unit of work that you are performing, and then close it once you have made the appropriate requests for creating / retrieving / updating / deleting etc.

If you think of it as being the equivalent of a session, then it becomes more apparent how long you should keep it open.


--
Stephen Souness


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.