6 Replies Latest reply on Oct 2, 2003 4:22 AM by Colm O Flaherty

    **Programatically** attach DataSource to JNDI

    Colm O Flaherty Newbie

      Is it possible to **programatically** attach a Datasource instance (com.sybase.jdbc2.jdbc.SybDataSource, in my case) to JNDI in JBoss 3.2.2 (RC2, RC3 or RC4 using TomCat)?

      I've tried the following code (reduced to the basics necessary to illustrate the point), and every time I do a lookup on the exact JNDI name I specified, it gets back a *null* object (no Exceptions thrown)..

      Any ideas???

      //instantiate the Datasource here..
      try {
      DataSource ds = new com.sybase.jdbc2.jdbc.SybDataSource ();
      String jndiName = "java:jdbc/sybase-ks013-clientdb";

      //get the initial JNDI context
      InitialContext initialContext = new InitialContext();

      //Bind the Datasource to JNDI so that we can access it from elsewhere later
      initialContext.bind (jndiName, ds);

      //now check the binding
      if ( initialContext.lookup(jndiName) == null )
      {
      //AAGGGH!!!! the object came back null, even though it was NOT null when we bound it to JNDI
      //log the error here using Log or whatever

      System.err.println ("ERROR: the object came back from JNDI as NULL");
      }
      }
      catch (Exception e)
      {
      //just report the exception

      System.err.println ("ERROR somewhere" );
      e.printStackTrace();
      }