6 Replies Latest reply on Nov 5, 2001 6:26 PM by mmagnaye

    stateless session bean ejbCreate()

    jpvasquez

      I guess this is more of an ejb-spec question.

      I was under the (mis?)understanding that on a statless session bean, ejbCreate() is only called the first time any instance of a SSB is created. Once the server pools that instance, ejbCreate() isn't called again.

      In our application, I am noticing that ejbCreate() is getting called on every lookup (well, every time we call create on the home interface). Is this correct? Is it possible that I don't have JBoss configured correctly to pool the instances of SSB's?

      --or quite likely, i'm just really screwed up in the head :)

        • 1. Re: stateless session bean ejbCreate()
          p_d_austin

          I have just looked at the EJB 2.0 spec and it says the following about the invocation of ejbCreate.

          A stateless session bean instance’s life starts when the container invokes newInstance() on the session bean class to create a new instance. Next, the container calls setSession-Context() followed by ejbCreate() on the instance. The container can perform the instance creation at any time—there is no relationship to a client’s invocation of the create()
          method.


          So basically the container can call ejbCreate when it wants to, this could either be just before it adds it to the pool or on every call to create on the home interface.

          The best place to do your setup would be in setEntityContext as this is typically called once per pooled instance.

          Hope this helps,
          Paul

          • 2. Re: stateless session bean ejbCreate()
            jpvasquez

            Thanks -- that's very helpful. I had been accomplishing *almost* the same thing by just setting an initialized flag after the first time through so that my initialization steps didn't need to occur twice. Your method definitely sounds better... :)

            • 3. Re: stateless session bean ejbCreate()

              There should be no need to use an initialized flag. The spec says that ejbCreate will be called exactly one time per stateless session bean instace - when the container decides to create a new instance. In my opinion ejbCreate is better suited for setting up bean configuration as setSessionContext for stateless sessions, simply because yout have access to more of the methods of SessionContext and in all other ways this methods are equal. But this only applies to stateless sessions. When jboss calls ejbCreate more times on a bean instance it is an serrious error, but i dont believe that, someone would have noticed before, so possibly there is an error in your code.

              • 4. Re: stateless session bean ejbCreate()
                nhebert

                Jason,

                Are'nt specs wonderful? They are open to interpration.
                What Paul Austin (p_d_austin) quotes "The container
                can perform the instance creation at any time?there
                is no relationship to a client?s invocation of the
                create() method.". Is quite correct. But what does
                this mean?

                My experience with various EJB containers (JBoss
                included) is that at any time, a container will
                perform the steps as per spec, in that order when it
                feels it needs to create a new stateless session
                bean. They are all performed just before the bean
                goes into the instance pool and are performed *once*.

                There is *no* relationship to a create() invocation
                on a bean home and the steps performed. The steps
                are performed 1-2-3 for all purposes without deplay
                between them.

                Juraj Vasko (lothar) is also quite correct the
                way the steps are performed per spec, ejbCreate()
                is the appropriate point of bean initialisation as
                you have access to more "goodies".

                If the ejbCreate is being called more than once for a
                *specfic* bean instance, then that most certainly is a
                bug in the EJB container.

                When you see ejbCreate() being called multiple times
                are you _sure_ it is being called repeatedly for the
                same bean instance, or might it be an artifact of
                the way you are reporting/recording the ejbCreate()
                invocation?

                Just some random thoughts...

                Cheers,

                Noel.


                • 5. Re: stateless session bean ejbCreate()
                  jpvasquez

                  Well, this problem seems to have gone away with the 2.4 final code release.

                  BTW, I had been just recording calls to ejbCreate() with a simple System.out.println() whenever ejbCreate() was called -- I had noticed it was called anytime a client called lookup. Could have been some weirdness with the particular build I was using.

                  Thanks for your insight into this problem!

                  • 6. Re: stateless session bean ejbCreate()
                    mmagnaye

                    yep it works with 2.4.3! finally!