Problem with Entitymanager and JPA

Asked

Viewed 452 times

3

I have a problem here that says the entitymanager is closed...I took a look at the net to follow the same model, but here’s wrong... this is my entitymanager method

private EntityManager getEntityManager(){
    EntityManagerFactory factory = null;
    EntityManager em = null;
    try{
        factory = Persistence.createEntityManagerFactory("afastamentoGuarnicao");
        em = factory.createEntityManager();
    }finally{
        em.close();
    }
    return em;
}

and this is my way to record...

    public boolean gravar(UsuarioArranchamento usuarioArranchamento) {
    EntityManager em = null;        
    if (usuarioArranchamento.getSenha2().equals(usuarioArranchamento.getSenha())) {
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            if (usuarioArranchamento.getId() == 0) {
                em.persist(usuarioArranchamento);
            } else {
                em.merge(usuarioArranchamento);
            }
            em.getTransaction().commit();
            UtilMensagens.mensagemInfo("Cadastro realizado com sucesso!");
            return true;
        } catch (Exception ex) {
            if (em.getTransaction().isActive() == false) {
                em.getTransaction().begin();
            }
            em.getTransaction().rollback();
            UtilMensagens.mensagemErro("Erro ao cadastrar: Usuario ja existente, por favor escolha outro.");
            return false;
        } finally {
            em.close();
        }
    } else {
        UtilMensagens.mensagemErro("Senhas diferentes.");
        return false;
    }
}

Does anyone know how to handle this? Thank you very much

stracktrace of the error...

Caused by: java.lang.IllegalStateException: EntityManager is closed
at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:97)
at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:88)
at org.hibernate.jpa.internal.EntityManagerImpl.close(EntityManagerImpl.java:140)
at dao.UsuarioArranchamentoDAO.login(UsuarioArranchamentoDAO.java:97)
at control.ControlUsuarioArranchamento.efetuarLogin(ControlUsuarioArranchamento.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:279)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(Unknown Source)
... 26 more
  • Welcome to Stackoverflow, it would be good if you put the stacktrace of Exception to know the line and the error that gave.

  • Ta ai cara, editei meu post...thanks for responding...

2 answers

1

In your getentityManager method you have a finally{ em.close(); }. And that’s what’s closing your entityManager.

  • Yeah, man, that I know....

  • Ué, just do not close it... if the method exists to return an entitymanager does not make sense to close it, agree?

  • All right, thank you...

0

Transform the EntityManagerFactory in a variable in the class scope and not method, since you will only need one for each execution of the program, and make the declaration directly.

private static EntityManagerFactory factory =
Persistence.createEntityManagerFactory("persistenceUnit");

Finally, the problem lies in the method itself that opens the connection in the finally, to solve, just exclude it from the method getEntityManager() and let only that which is within the way gravar().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.