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.
– Guerra
Ta ai cara, editei meu post...thanks for responding...
– Andrey