Ratha
Ratha

Reputation: 9692

Hibernate EntityManager is getting closed, in wildfly 10

I use container managed JPA where inject the EntityManager instance. With the injected entitymanager instance, when I use find() method it says entity manager is closed. I use wildfly 10.

How can I overcome this issue? What I do wrong here?

I create entity manager like this;

        @PersistenceContext
        protected EntityManager em;


    @Stateless
    public class CustomerService  CrudService<Customer> {


    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void update(Customer entity) {

    Customer item = em.find(Customer.class,entity.getId()); //ISSUE
    if (entity.getParentId()!=null) {
        item.setParent(em.find(CRMEntity.class , entity.getParentId()));
    item.setParentId(entity.getParentId());
    }
....

super.update(item);
}

public abstract class CrudService<T extends BaseEntity>  {
public void update(T entity) {
        Session session = null;

        try {
            session = getSession();
            session.update(entity);
            session.flush();

        } catch (HibernateException ex) {
            log.error("Error when update." + entity.getCode(), ex);
            if (session != null) {
                session.cancelQuery();
            }
        } finally {
            if (session != null && session.isOpen()) {
                session.clear();
                session.close();
            }
        }
    }
public Session getSession() {
        if (session == null || !session.isOpen()) {
            session = getEntityManager().unwrap(Session.class);
        }
        return session;
    }

}

I get issue at this line;

Customer item = em.find(Customer.class,entity.getId());

Error

Caused by: java.lang.IllegalStateException: Session/EntityManager is closed
    at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:326)
    at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:126)
    at org.hibernate.internal.SessionImpl.find(SessionImpl.java:3312)
    at org.hibernate.internal.SessionImpl.find(SessionImpl.java:3297)
    at org.jboss.as.jpa.container.AbstractEntityManager.find(AbstractEntityManager.java:213)
    at com.leightonobrien.lob2.service.autogen.CustomerService.update(CustomerService.java:412)
    at sun.reflect.GeneratedMethodAccessor249.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)

Upvotes: 0

Views: 736

Answers (1)

Ratha
Ratha

Reputation: 9692

Issue here is I closed the session. After removing it, everything works fine

Upvotes: 1

Related Questions