7 Replies Latest reply on Mar 27, 2007 12:18 PM by Pete Muir

    Question on Stateless Session Bean

    Hannes Schulz Newbie

      Hi,

      i have want a stateless session bean to serve Objects to a selectItems component. So far i have adopted a stateful bean from the dvd example to serve the objects.

      the code is the following:

      @Stateful
      @Name("building")
      public class BuildingListAction implements BuildingList,Serializable{
      
       private List<BuildingType> buildings;
       Map<String,BuildingType> buildingsMap;
      
       @PersistenceContext
       EntityManager em;
      
      
       @Create
       public void loadData() {
       buildings = em.createQuery("select b from BuildingType b")
       .setHint("org.hibernate.cacheable", true)
       .getResultList();
      
       Map<String,BuildingType> results = new TreeMap<String,BuildingType>();
      
       for (BuildingType buildingtype: buildings) {
       results.put(buildingtype.getName(),buildingtype);
       }
      
       buildingsMap = results;
       }
      
       public Map<String,BuildingType> getBuildings(){
      
       return buildingsMap;
       }
      
       public Converter getConverter() {
       return new BuildingConverter(buildings);
       }
      
       @Remove @Destroy
       public void destroy() {}
      }


      my question is now what is necessary to make this bean a stateless bean. I stuck at the point where to initialize the list and where to execute the query.

      Thanks a lot.
      regards,
      hasc

        • 1. Re: Question on Stateless Session Bean
          Hannes Schulz Newbie

          after a while i got this working.

          @Stateless
          @Name("building")
          public class BuildingListAction implements BuildingList,Serializable{
          
           private static final long serialVersionUID = 1L;
          
           private List<BuildingType> objects;
           Map<String,BuildingType> buildingsMap;
          
           @PersistenceContext
           EntityManager em;
          
           public Map<String,BuildingType> getBuildings()
           {
           objects = em.createQuery("select b from BuildingType b")
           .setHint("org.hibernate.cacheable", true)
           .getResultList();
          
           Map<String,BuildingType> results = new TreeMap<String,BuildingType>();
          
           for (BuildingType buildingtype: objects)
           {
           results.put(buildingtype.getName(),buildingtype);
           }
          
           buildingsMap = results;
          
           return buildingsMap;
           }
          
           public Converter getConverter()
           {
           return new BuildingTypeConverter(objects);
           }
          }



          im not familiar with ejb but this obviously leads to a query every time the method getBuildings() is called. My goal is to load the objects only once and then return a list every time the methos is called. Can somone help me with that or point me to an example or tutorial which describes that?

          regards,
          hasc


          • 2. Re: Question on Stateless Session Bean
            Pete Muir Master

            You'll either need to make the component stateful, or outject into a scope that holds state. Stateless components hold no state between invocations. That's what the snippet of code in your first post is doing.

            • 3. Re: Question on Stateless Session Bean
              Hannes Schulz Newbie

              so is it impossible to create an object, thats is initialized once (in my example retrieves the required objects out of the database) and only one instance of it exists in the container?

              I mean I have to display lists of objects on several pages and the objects in the database are almost never changed or new entities added. so my idea was to have one Stateless Bean that returns the list on each request.

              Would a stateful bean with scope application be a solution?

              Is there a way to declare that only one instance of an object should reside in an ejb container?


              • 4. Re: Question on Stateless Session Bean
                Jason Long Master

                Is it possible to store the lists for s:selectItems in application scope?

                Currently all my select items lists are in conversation scope. These lists are sometimes modified by users and I would like the changes to be globally visible.

                If use Session or Conversation scope and outject the list upon update these changes will only be visible to that one user. Other users will have to start a new conversation or session to see the changes.

                Maybe I should use PojoCache. If this is the case is there an example of this configuration?

                • 5. Re: Question on Stateless Session Bean
                  Mike Quilleash Newbie

                  Application scope should be fine for the above examples if you do the initial work in @Create and then just access it. Of course provided the data is the same for all users.

                  Be aware of threading issues in application scope tho as multiple threads may access it at the same time. If you do all your loading in @Create it should be fine.

                  @Name( "cache" )
                  @Scope( ScopeType.APPLICATION )
                  @Intercept( InterceptionType.NEVER )
                  public class Cache
                  {
                   private Map cache;
                  
                   @Create
                   public void create()
                   {
                   // build cache here...
                   }
                  
                   public Map getCache()
                   {
                   return cache;
                   }
                  }
                  


                  HTH.

                  Mike.

                  • 6. Re: Question on Stateless Session Bean
                    Hannes Schulz Newbie

                    thanks a lot.

                    Is there a way to declare that only one instance of a class may exist in an ejb container and all requests are bound to this instance?

                    hasc

                    • 7. Re: Question on Stateless Session Bean
                      Pete Muir Master

                      As Mike says, put your Seam component in the application scope - then whenever you inject it (e.g. EL or @In) you'll get the same instance