6 Replies Latest reply: Apr 5, 2013 6:04 PM by REDO LOG RSS

    problem with the remote ejb

    REDO LOG
      Hi all,
      I have developed an EJB and run it in a glassfish server, now I want to test if it is really working so I created a java client to do so,
      My main class for test is as follows :
      public class Tester {
      
           public static void main(String[] args) {
                try {
                     InitialContext ctx = new InitialContext();
      
                     ApplicationManagementRemote appM = (ApplicationManagementRemote) ctx
                               .lookup("java:global/MAS/MwpServices/ApplicationManagementService");
                     
                     if(appM != null){
                          System.out.println("not null");
                                      System.out.println(appM.listApplications().size());
                     }else{
                          System.out.println("null");
                     }
      
                } catch (NamingException e) {
                     e.printStackTrace();
                }
      
           }
      
      }
      and as to the EJB:
      /**
       * Session Bean implementation class ApplicationManagementService
       */
      @Stateless
      public class ApplicationManagementService implements
                ApplicationManagementRemote {
      
           public ApplicationManagementService() {
                // TODO Auto-generated constructor stub
           }
      
           @PersistenceContext(unitName = "data-model")
           protected 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);
                // applyFilters(h, cb, q, filters);
                // applySort(cb, q, sortedField, sortOrder);
                // q.select(JPAUtils.constructeApplicationDTO(cb, h));
                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();
           }
      
           @Override
           public List<ApplicationDTO> listApplicationDTO() {
                return allApplications;
           }
      }
      when I run the client I got the following eroor:
      not null
      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.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.$Proxy256.listApplicationDTO(Unknown Source)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              ....
      Caused by: java.lang.NullPointerException
           at com.mob.pass.portal.subscription.ApplicationManagementService.listApplicationDTO(ApplicationManagementService.java:59)
           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 org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
           at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
           at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5367)
           at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
           at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:801)
              ...
           ... 19 more
      what I am thinking is that the container could not establish a connection to the database because in the entity manager, the @Persistence has no unit name

      what I am missing here?

      thanks for any help
      best regards , Rachid

      Edited by: REDO LOG on Apr 5, 2013 11:29 AM

      Edited by: REDO LOG on Apr 5, 2013 11:29 AM
        • 1. Re: problem with the remote ejb
          gimbal2
          what I am thinking is that the container could not establish a connection to the database because in the entity manager, the @Persistence has no unit name
          So you are not posting the actual code that is failing, because the code you posted DOES have a PU name. That is not really a smart thing to do, post the -real- code.

          Now lets examine the code:

          - when is allApplications actually initialized? Inside load(). Are you calling that?
          - its a STATELESS EJB. Do you know what that means? What do you think the allApplications member variable is? State. You are putting state in a STATELESS EJB. Sounds wrong, don't you think?
          • 2. Re: problem with the remote ejb
            REDO LOG
            Hi, thanks for responding,

            Stateless means that when we call a method in the EJB the EJB instance is destroyed and the new instance will not be available until we need a method in that EJB

            as to the persistence unit it serves for the EJB to connect to the DB


            for the initialisation of the list allApplications, I am using a lazy loading and the interface extends the primefaces LazyDataModel class so the load() is called automatically


            correct me if I am wrong
            • 3. Re: problem with the remote ejb
              REDO LOG
              Hi,

              here is the new EJB:
              import com.mobiwigo.datamodel.Application;
              import com.mobiwigo.pay.webportal.ApplicationManagementRemote;
              import com.mobiwigo.pay.webportal.dto.ApplicationDTO;
              
              /**
               * Session Bean implementation class ApplicationManagementService
               */
              @Stateful
              public class ApplicationManagementService implements
                        ApplicationManagementRemote, Serializable {
              
                   /**
                    * 
                    */
                   private static final long serialVersionUID = 1L;
              
                   public ApplicationManagementService() {
              
                   }
              
                   @PersistenceContext(unitName = "data-model")
                   protected 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() {
                        Query query = entityManager
              .createQuery("SELECT e FROM APPLICATION e");
                        allApplications = query.getResultList();
                        return allApplications;
                   }
              
              }
              the error this time is :
              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
              I
              • 4. Re: problem with the remote ejb
                gimbal2
                REDO LOG wrote:
                Stateless means that when we call a method in the EJB the EJB instance is destroyed and the new instance will not be available until we need a method in that EJB
                No, you are very wrong. Or perhaps not entirely wrong but that is in no way what stateless and stateful is all about. You should consider reading a proper book on EJB technology to really know the technology you're trying to use and abuse. I recommend "Enterprise Javabeans 3.1".

                You mention primefaces - that is built for JSF, a web framework. You're not doing web stuff here, you're just calling an EJB from a plain old client application. So primefaces isn't even initialized in any way, you're doing basic EJB invocation stuff. It isn't too good an idea to make primefaces work on EJBs in any case. EJBs are server managed resources, not JSF and/or primefaces resources.
                • 5. Re: problem with the remote ejb
                  REDO LOG
                  Hi thanks for your help,

                  I understand that, know I am just testing the EJB if it work properly,
                  I am facing this error:
                  Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.
                  the name of the persistence unit is omitted and even if I provide one, I still get the same error
                  • 6. Re: problem with the remote ejb
                    REDO LOG
                    it is reported that the error attempting to exe... is a bug on the glassfish 3.1.2

                    see link: http://www.java.net/forum/topic/glassfish/glassfish/nb-701-gf-311-mysql-50-jsf-create-entities-attempting-execute-operation-closed-entitymanagerfact