6 Replies Latest reply on Oct 2, 2003 4:22 AM by themax

    **Programatically** attach DataSource to JNDI

    themax

      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();
      }

        • 1. Re: **Programatically** attach DataSource to JNDI

          Just guessing, is that datasource implementation serializable?

          -- Juha

          • 2. Re: **Programatically** attach DataSource to JNDI

            umm, nevermind the above, you're doing the lookup straight after you bound it in the same VM

            • 3. Re: **Programatically** attach DataSource to JNDI

              But still... you might have to use NonSerializableFactory.rebind() to bind the DS if it's not serializable, the JBoss naming is doing this with its bind() implementation:

              
               public void bind (Name name, Object obj)
               throws NamingException
               {
               Hashtable refEnv = getEnv (name);
               checkRef (refEnv);
              
               try
               {
               String className;
              
               // Referenceable
               if (obj instanceof Referenceable)
               obj = ((Referenceable)obj).getReference ();
              
               if (!(obj instanceof Reference))
               {
               className = obj.getClass ().getName ();
              
               // Normal object - serialize using a MarshalledValuePair
               obj = new MarshalledValuePair(obj);
               }
               else
              


              that should have thrown an IO exception which in turn should have been converted to a Naming Exception...?

              -- Juha

              • 4. Re: **Programatically** attach DataSource to JNDI
                themax

                The class is definitely Serializable alright.. I have a check for that in the original code:

                if ( ! ( ds instanceof Serializable ))
                {
                if ( log.isErrorEnabled ())
                log.error ("The Datasource class '" + ds.getClass().getName() + "' is NOT Serializable");
                }

                (using Commons logging, with the priority set to "Debug" or "info", so I always see "error")

                I'm also seeing a *possible* explanation in a TomCat Servlet init bug (where my code is fired from).. will check this out, and report back..

                http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10948

                • 5. Re: **Programatically** attach DataSource to JNDI
                  themax

                  No joy.. I changed the code to be user-initiated (after the Servlet init), a new (non-null) Datasource was re-bound, but the same issue arose: the lookup (the next line of code after the bind) returned null.. so it look like the TomCat Servlet init bug is NOT my issue (or else there is a similar bug applicable to lookups outside the Servlet init).

                  Will check out the "Referenceable" references (forgive the pun) mentioned in earlier replies.

                  Thanks!

                  Colm

                  • 6. Re: **Programatically** attach DataSource to JNDI
                    themax

                    If I switch the class being bound to a fairly straightforward Test class (implements EntityBean, Serializable), and try the rebind and lookup outside the init (haven't tried it from inside the init yet), it works (I get the actual instance back, as opposed to getting null back), which suggests that the issue is either related to the Sybase DataSource hierarchy, or that particular Sybase DataSource class itself.. will try a different type of DataSource (MySQL, or something), and see if the issue can be reproduced.