This discussion is archived
3 Replies Latest reply: Apr 22, 2013 5:04 PM by REDO LOG RSS

problem with an ENtityManager

REDO LOG Newbie
Currently Being Moderated
Hi all
I am facing a horrible problem with an EnitityManager in EJB, actually test the entity manager if it is open before using it but still getting strange error:
Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.
the EJB is:
@Stateless
public class ApplicationManagementService implements
          ApplicationManagementRemote {

     public ApplicationManagementService() {

     }

     @PersistenceContext
     private EntityManager entityManager;

     private List<ApplicationDTO> allApplications;

     @Override
     public List<ApplicationDTO> load(
               int first,
               int pageSize,
               String sortedField,
               com.mobiwigo.common.dataaccess.IDataAccessProxy.SortOrder sortOrder,
               Map<String, String> filters) {

          CriteriaBuilder cb = entityManager.getCriteriaBuilder();
          CriteriaQuery<ApplicationDTO> q = cb.createQuery(ApplicationDTO.class);
          Root<Application> h = q.from(Application.class);
          q.multiselect(h);
          allApplications = entityManager.createQuery(q).setFirstResult(first)
                    .setMaxResults(pageSize).getResultList();

          return allApplications;
     }

     @Override
     public int getResultCount(Map<String, String> filters) {
          return allApplications.size();
     }

     @SuppressWarnings("unchecked")
     @Override
     public List<ApplicationDTO> listApplicationDTO() {
          if (entityManager.isOpen()) {
               System.out.println("ENTITYMANAGER IS OPEN");
               System.out.println(entityManager.toString());
               Query query = entityManager
                         .createQuery("SELECT a FROM APPLICATION a");
               allApplications = query.getResultList();
          }
          return allApplications;

     }

     @PostConstruct
     public void init() {

          ApplicationDTO dto = new ApplicationDTO();
          dto.setCode("123");
          dto.setName("app");
          dto.setAbrevName("azerazr");
          dto.setStatus(ApplicationStatus.A);
          allApplications = new ArrayList<ApplicationDTO>();
          allApplications.add(dto);
     }
is there any thing wrong with?


thanks for any help
  • 1. Re: problem with an ENtityManager
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    please show us some more of the stacktrace.

    bye
    TPD
  • 2. Re: problem with an ENtityManager
    REDO LOG Newbie
    Currently Being Moderated
    thanks for responding, here stack trace :
    Exception in thread "main" javax.ejb.EJBException
         at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5194)
         at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5092)
         at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4880)
         at com.sun.ejb.containers.StatefulSessionContainer.postInvokeTx(StatefulSessionContainer.java:1653)
         at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039)
         at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990)
         at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:213)
         at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
         at sun.proxy.$Proxy370.listApplicationDTO(Unknown Source)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
         at java.lang.reflect.Method.invoke(Method.java:601)
         at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:144)
             ....
     
    Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.
         at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.verifyOpen(EntityManagerFactoryImpl.java:258)
         at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:241)
         at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:237)
         at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:208)
         at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:435)
         ......
         ... 19 more
  • 3. Re: problem with an ENtityManager
    REDO LOG Newbie
    Currently Being Moderated
    just clean and refresh the server ! and it will work properly

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points