views:

55

answers:

1

I am trying to place an object in JNDI, so that only one of the progam should be able to place it in JNDI. is there any global lock that can be used in J2EE environment. Is RMI can be used for this purpose? please provide any reference links. Thanks in advance.

Also, what is NameAlreadyBoundexception? I am trying to use it as a method to synchronize, i.e, only one program places it in JNDI and if other trying to bind should get that exception. But when i am testing the multiple binding I am not getting the Exception.And second binding is done. look up is giving the second object bound. here is my code:

public class TestJNDI {
 private static String JNDI_NAME = "java:comp/env/test/something";

   public static void main(String[] args) throws NamingException {

      Hashtable<String, String> env = new Hashtable<String, String>();
      env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
      env.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001");

      Context ctx = new InitialContext(env);
      System.out.println("Initial Context created");

      String obj1 = "obj1";
      String obj2 = "obj2";

      try{
          ctx.bind(JNDI_NAME, obj1);
           System.out.println("Bind Sucess");
        }catch(NameAlreadyBoundException ne ){
         // already bound
         System.out.println("Name already bound");
        } 


     ctx.close();

     Context ctx2 = new InitialContext(env);
    try{
      // Second binding to the same name not giving the Exception??
      ctx2.bind(JNDI_NAME, obj2);
      System.out.println("Re Bind Sucess");
      }catch(NameAlreadyBoundException ne ){
     // already bound
     System.out.println("Name already bound");
      } 


    String lookedUp = (String) ctx2.lookup(JNDI_NAME);

    System.out.println("LookedUp Object"+lookedUp);
    ctx2.close();
  }


}
A: 

When you close the first content ctx1 you release any objects bound to it see : Context

So your second context has nothing to do with the first one.

Gandalf