4 Replies Latest reply on Nov 18, 2009 1:43 PM by v.lukoyanov

    Call SFSB from SLSB.

    allanjun

      Does it make sense to call stateful session bean from stateless session bean?


      It works, but I don't understand it.



      @stateless
      public ClientServiceBean implements ClientService {
      
        @EJB ClientDao clientDao;
      
        public Client findClient(Long clientId) {
          return clientDao.findClient(clientId);
        }
      
        public void save(Client client) {
          clientDao.save(client);
        }
      }
      
      @Stateful
      public ClientDaoBean implements ClientDao {
        
        @PersistenceContext(type=Extended)
        EntityManager em;
      
        public void save(Client client) {
          em.persist(client);
        }
      
        public Client load(Long clientId) {
          em.find(Client.class, clientId);
        }
      }
      



      The reason that I need ClientDaoBean to be stateful is that the em has to be extened type to support lazy loading from the web page.


      It actually works this way.


      Can someone explain how does this work?


      And as I don't have a method with @Remove in ClientDaoBean, does that mean the SFSB will only be destroyed when web session ends?


      Do I need to call a @Remove method on ClientDao in ClientService?


        • 1. Re: Call SFSB from SLSB.
          v.lukoyanov

          It's not good in use SFSB from SLSB.


          Quote from EJB3 in Action:





          If you are using DI, make sure you don’t inject a stateful session bean into a stateless ses-
          sion bean or servlet. Injected EJB instances are stored in an instance variable and
          are available globally for subsequent clients even if a stateless bean instance is
          returned to the pool, and an injected stateful bean instance may contain inaccu-
          rate state information that will be available to a different client. It’s legal to inject
          a stateful bean instance to another stateful session bean or an application client.


          ----------

          My seam website

          • 2. Re: Call SFSB from SLSB.
            allanjun

            Vasilii L. wrote on Nov 18, 2009 10:40:


            Injected EJB instances are stored in an instance variable and
            are available globally for subsequent clients even if a stateless bean instance is
            returned to the pool


            I see, that's how my example above worked. Didn't know it's available globally.


            So, does that mean I need to change my ClientServiceBean to stateful?

            • 3. Re: Call SFSB from SLSB.
              allanjun

              And I guess I need to call a @remove method when I'm done with the bean, otherwise, it's only destroyed when web session expired, right?

              • 4. Re: Call SFSB from SLSB.
                v.lukoyanov

                Allan Li wrote on Nov 18, 2009 11:41:

                So, does that mean I need to change my ClientServiceBean to stateful?

                Yes, seems so.



                And I guess I need to call a @remove method when I'm done with the bean, otherwise, it's only destroyed when web session expired, right?

                I'm not sure if web session lifetime is related to stateful ebj lifetime, but you definitely have to destroy your bean somehow.


                ----------

                My seam website