Problem with EntityManager and JPA

3

I have a problem here that says entitymanager is closed ... I took a look at the net to follow the same model, but here it is 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 method 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 solve this? Many thanks.

error stracktrace ...

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
    
asked by anonymous 07.07.2016 / 20:17

2 answers

1

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

    
07.07.2016 / 21:30
0

Make the EntityManagerFactory into a variable in the class scope rather than the method scope, since you will only need one for each program execution, and make the declaration directly.

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

Finally, the problem is in the very method that opens the connection in finally , to solve, just delete it from the getEntityManager() method and leave only the one that is within the gravar() method.

    
15.07.2016 / 19:43