7 Replies Latest reply on Feb 9, 2004 2:32 PM by cari34

    Private attribute in a Stateless Session Bean

    cari34

      Hi guys,
      I have a question about the design of a stateless session EJB.
      I would like to know :

      Does it make sense to put a private field in a stateless session bean???

      Imagine the following sitiation:
      I have a stateless session bean deployed on JBOSS.
      Its job is to delegate the clients calls to some services (Java objects).
      So I would like these services (Java objects) be instantiated before the clients invoke the bean's method for delegation - I mean, when JBOSS
      decides to create beans and put it in the pool (on the server startup for example).

      But what happens when the same bean once having finished its job is used by a different client???

      Are the services once instatiated available to the new client or JBOSS invokes the ejbCreate() method and the services are instatiated one more time???(which would be a performence killer)

      I must mention that in order to make the instatiation of the services possible, I presume I must implement the ejbCreate() method on the bean implementation class which invokes a private method doing the instatiation.

      So, do you think these design is a good design and what is its impact on the performence of the stateless session EJB in JBOSS???

      Thank you.
      Cari

        • 1. Re: Private attribute in a Stateless Session Bean

          The session objects are pooled (and lazily constructed).

          This means your ejbCreate() won't be invoked until the first request.
          It is not when they do home.create(), this just creates a proxy to the bean without
          doing anything with bean instances.

          Once your object is in the pool, it won't invoke ejbCreate() again.

          If you want to configure expensive resources you should consider
          JCA. Or if there is no need for transactions and security and MBean.

          Regards,
          Adrian

          • 2. Re: Private attribute in a Stateless Session Bean
            marc.fleury

            having a field in stateless only makes sense if all the sessions need that field. If the field is session specific then you need stateful sessions.

            • 3. Re: Private attribute in a Stateless Session Bean
              cari34

              Hi Adrian and Marc,
              Thank you.

              The field I want to put in the stateless session bean is not session specific.
              (the bean has only one method).
              It holds reference to an array of objects that I want to be instatiated before the client invokes the EJB method.
              So I want to instantiate these objects when the container call the ejbCreate() method.

              But I want to know what happens when the same bean instance serves a different client??
              Will the objects that have been instantiated when the container called ejbCreate() method be available to that new client???

              And in general, is there any impact on performance to declare a private field in a Stateless session bean???

              Thank you very much
              Cari

              • 4. Re: Private attribute in a Stateless Session Bean

                Yes the objects will be available to all clients (they're pooled). So if you use instance fields they must be immutable throughout the life cycle of the bean (or you need to figure out how to notify the bean instances of the state change)

                • 5. Re: Private attribute in a Stateless Session Bean
                  cari34

                  Hi Juha,
                  Thank you very much.

                  I wandered whether there is a harm to call a singleton class from the ejbCreate method in order to retrieve the array of objects and populate
                  the bean's instance field with it?

                  I know that EJB must avoid using static fields and the singleton class has static field.

                  Do you think it's a good choice to do like that??
                  Could this raise performence issues??

                  I precise that the bean's method delegates every client call to the objects method contained in bean's intance field.

                  Thank you very much.
                  Cari.

                  • 6. Re: Private attribute in a Stateless Session Bean

                    If the singleton returns immutable values it should be ok. Mutable values don't work in a cluster setup.

                    • 7. Re: Private attribute in a Stateless Session Bean
                      cari34

                      Thanks Juha,
                      Your advice is very helpfull.
                      Cari