2 Replies Latest reply on Jun 20, 2006 2:13 AM by jaboj

    State isn't reset for stateless session beans!

    jaboj

      HI,

      I'm using JBOSS 4.0.4GA. To demonstrate my question below I've done a brand new install of jboss.

      I thought that no state at all was kept in a stateless session bean between lookups - and is is really possible to share a stateless session bean across sessions!

      Take a look at this bean:

      package com.jaboj.mm.dao;
      
      import javax.ejb.Stateless;
      
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      @Stateless
      public class LocationDAO implements LocationDAOLocal {
      
       @PersistenceContext(unitName = "MM")
       private EntityManager em;
      
       private int count = 0;
      
       public LocationDAO() {
       count = 0;
       }
      
       public String getHello() {
       count++;
       System.out.println("COUNT: " + count);
       return "hello: " + count;
       }
      
      }
      


      It's quite simple. Everytime hello() is called, count is increased by one. So, I would expect, that the first time I invoke hello() after a bean lookup, "hello: 1" is returned. But that isn't allway the case.

      I use the following code to lookup the sessionbean, from a test servlet.

       ---
       InitialContext ctx = new InitialContext();
       LocationDAOLocal bean =
       (LocationDAOLocal)ctx.lookup("mm2007/LocationDAO/local");
       out.print(bean.getHello());
       --
      


      When using this method to lookup the stateless session bean, the beans sometime get reused - even accross sessions! I really don't understand why it is possible to get the return values "hello: 2", "hello: 3", ... etc. from the session bean, on the first call after lookup!

      Below I've pasted the console-output after I've invoked the servlet 3 times from IE and Firefox - to demonstrate that the session beans also are shared accross sessions!!!

      22:46:09,763 INFO [STDOUT] COUNT: 1 (IE)
      22:46:11,235 INFO [STDOUT] COUNT: 2 (IE)
      22:46:15,591 INFO [STDOUT] COUNT: 3 (FIREFOX)
      


      What am I doing wrong - this can't be real?

      Regards
      Jacob