2 Replies Latest reply on Oct 7, 2009 11:52 AM by bwalker55

    Looking for standard jndi/jdbc java connection syntax

      Deploying java servlet code on jBoss for the first time and am having trouble with jndi/jdbc database connection.
      Code from long running Tomcat/Resin deployments look like:

      Context init = new InitialContext();
       Context ctx = (Context)init.lookup("java:comp/env");
       DataSource ds = (DataSource) ctx.lookup("jdbc/" + dbName); // dbname defined in Tomcat server.xml


      With jBoss the above code gives: javax.naming.NameNotFoundException: jdbc not bound error.

      As per http://www.jboss.org/community/wiki/WhyDoIGetNameNotFoundException
      I have changed the last line to:
      DataSource ds = (DataSource) ctx.lookup("java:/" + dbName); // dbname defined in *-ds.xml for jBoss

      and this works.

      However, I would like to find a solution that works on both servers. I've looked through the JNDI Wiki and FAQ as suggested in the Sticky, but there's nothing obvious jumping out at me as to what I need to do.

      Thanks, Bob.