3 Replies Latest reply on Dec 18, 2004 10:17 AM by starksm64

    Best practice for sharing a data structure through JNDI ?

    fredatwork

      Hello,

      I would like to have to share a data structure though JNDI. This data structure behaves like a synchonized cache (let's say a data structure based on an instance of HashMap). Some session beans of mine will then use this shared cache to store/access some stuff.

      How should I do implement this shared cache ? I would like to understand what is the best practice to share a data structure between all instances a a session bean ?

      I understand <env-entry> tags only allos you to share some basic types.

      Therefore, I tried to bind such a cache in the init() method of a servlet (loaded at startup) :

      InitialContext context = new InitialContext();
      // Create application sub context
      try {
       Context appContext = context.createSubcontext("java:comp/env/myApp");
      } catch (NameAlreadyBoundException e) {
       // Do nothing
      }
      try {
       Context cacheContext = context.createSubcontext("java:comp/env/myApp/cache");
      } catch (NameAlreadyBoundException e) {
       // Do nothing
      }
      context.rebind("java:comp/env/myApp/cache/aCache", new Cache());


      In my session bean code, I attempt to get access to my cache with :
      try {
       InitialContext context = new InitialContext();
       return (Cache) context.lookup("java:comp/env/myApp/cache/aCache");
      } catch (NamingException e) {
       ... /...
      }


      However, when a session bean attempts to access to the shared cache, I get a NameNotFoundException saying 'myApp not bound' (note that the cache looked like well bound at startup of the initialization servlet).

      Any ideas how I should proceed ?

      Fred