2 Replies Latest reply on May 27, 2004 6:22 PM by Adrian Brock

    sharing data between multiple instances of a session bean

    vjha Newbie

      If this has been discussed before, please point me to the appropriate threads (I couldn't find anything related using search).

      I have a stateless session bean that is called by a JSP. A new feature that I am implementing requires the session bean to "recall", whenever possible, what was requested by and served to the current client last time the client made a request.

      Because of performance (throughput) requirements, if at all possible, I want to avoid the obvious solutions to the problem, namely, (a) use "stateful" session beans, or (b) store and recall data from an entity bean (i.e. database)

      So, my question is - are either of the following two things possible to implement?

      (1) "Client stickiness": a given client always (or "mostly") goes to the same instance of stateless session bean, as long as that instance is alive. Note that I don't want to create a new session bean for each new client (which would be the case with stateful session beans). A given instance can serve multiple clients, as long as a particular client goes to the same instance. Given the definition of stateless beans, this seems impossible to implement, but I thought I would check.

      (2) If (1) is not possible, can multiple instances share data between them (without using a database)? Sort of like shared memory between threads? Note that this data is not critical, and does not need to be backed by a database. If all the instances die for some reason, and the shared data gets lost, no big deal. As long multiple instances of the session bean are able to read/write to this shared memory, in a synchronized way, most of the time, I am fine.

      Thanks for reading this, and for any suggestions you may have.


        • 1. Re: sharing data between multiple instances of a session bea
          damo9f Newbie

          I think you can squirrel the data away in JNDI.

          A more interesting question to me is that if you want fast access to per-JVM caches, can you somehow synchronize access to static data without deadlocking the system?

          It seems you've got to be able to do that, because 3rd party persistence products will need to use caches such as an Object-Identity cache and there should therefore be a way to use a cache and still work inside J2EE.

          • 2. Re: sharing data between multiple instances of a session bea
            Adrian Brock Master

            Your question is
            "I want Stateful behaviour but with Stateless beans".
            You are haven't got a prayer.

            You can use singletons in your stateless beans
            private static final MyCache cache = MyCache.getSingleton();
            but ...
            1) you are still breaking the spec if you hold state based upon the client
            2) doesn't work unless like your in example the data can be lost (e.g. a clustered environment)
            3) usually causes memory leaks