6 Replies Latest reply on May 6, 2008 5:06 AM by jazir1979

    Stateless Session Bean in Application context

    mbelicek

      Hi, I have a problem understanding the implications of having the Stateless Session Bean (SLB) in Application context. The current reference documentation says that SLBs always live in stateless context.


      So if I mark the SLB with @Scope(ScopeType.APPLICATION) (or any other scope in that matter) what happens? Is the scope annotation ignored or the SLB will be living the the specified context or in both contexts (stateless context and application context)?


      Found no detailed information in documentation how such SLB behaves. The docs say that the SLBs do not hold state across multiple invocations. So if I have SLB in app context does it mean that everytime I access it e.g. from conversation or session scope it would be created new or not? Or maybe because of the pooling sometimes I would get my stored values and sometimes not?


      How is SLB in app context different from SFB (stateful bean) in app context? I know that concurrent access to SFB is serialized. But any more info?


      Additionaly if I want a bean in app context for storing some nonchanging data - like a list of some categories for example what would be suggested? I've read in the forum that stateless pojo bean with @Startup annotation and @Create method for initialization of the data is suggested. But what if I want to use EJB not a pojo? The @Create annotation in SLB is not allowed and if I use @Init annotation then I can't use entity manager in it because I get exception.


      I'm just starting with Seam now and try to read all the relevant docs I can find so sorry if my questions might be off. Currently I'm using Seam 2.0.1.GA.

        • 1. Re: Stateless Session Bean in Application context
          matt.drees

          You can't have a Stateless session bean in the Application context (or any stateful context).  IIRC, if you annotate a component both @Stateless and @Scope(ScopeType.APPLICATION), Seam will throw an exception.

          • 2. Re: Stateless Session Bean in Application context
            matt.drees

            If you want an EJB that lives in the application context, it'll have to be a stateful session bean.  You could either use @Startup and @Create, as mentioned, or use an @Observer("org.jboss.seam.postInitialize") method.  I tend to prefer the latter.

            • 3. Re: Stateless Session Bean in Application context
              mbelicek

              With stateful session bean I would get the concurent access performance problem. So I guess using a pojo in application context is the answer. Thanks for the hint with the Observer.

              • 4. Re: Stateless Session Bean in Application context
                mbelicek

                Seam will not throw any exception (2.0.1.GA). That's the reason why I'm asking. I have seen some examples on the net with this combination but they might be wrong. I don't know. So I guess it would be nice if somebody could bring some light on the matter.

                • 5. Re: Stateless Session Bean in Application context
                  matt.drees

                  Just looked through the code, and it doesn't look like it throws an exception like I thought it did, as you say. 


                  It looks like what will happen is it will store the reference in the specified scope.  So, if you had a @Stateless component with @Scope(APPLICATION), you'd keep the same reference.  Of course, this doesn't help for @Stateless beans; I imagine if you tried to store information in it you'd see sometimes get that data back, but sometimes not (because of pooling, as you mentioned).


                  I'm not sure why Gavin didn't throw an exception in this scenario.  Maybe this was an oversight?  Maybe it's intentional, because this could allow you to avoid a jndi lookup every time you need to get a reference to your stateless component.  I don't know enough about EJB to know if that's useful or not.


                  Anyone else have thoughts?


                  In my opinion, Seam should throw an exception.

                  • 6. Re: Stateless Session Bean in Application context
                    jazir1979

                    In my app I use a stateless session bean in stateless scope, but with a number of @Factory methods whose results get stored in the Application context.


                    I use this for returning lists of entities for select boxes, etc.  And it seems to work very well.


                    eg:


                    @Factory(value = "allRoles", scope = ScopeType.APPLICATION)
                    public List<Role> getAllRoles() {
                        return roleService.getAllRoles();
                    }