You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Adam Lipscombe <ad...@cobar.fsbusiness.co.uk> on 2002/07/19 13:56:58 UTC

Datasource JNDI lookup failing

Folks,

I am having a problem trying to lookup a Database connection from JNDI with
Tomcat 4.0.4 on Debian Linux.
My code is:

    Connection con = null;

    System.out.println("instantiating database connection ");

    try
    {
      System.out.println("getting InitialContext");
      Context ctx = new InitialContext();
      System.out.println("got InitialContext");

      // Print out bindings to make sure its there
      NamingEnumeration e = ctx.list("java:comp/env/jdbc");
      while(true == e.hasMore())
      {
        System.out.println("enumeration name = " + e.next());
      }

      // Get JDBC context
      System.out.println("getting JDBC context");
      Context jdbcContext = (Context) ctx.lookup("java:comp/env/jdbc");
      System.out.println("got jdbcContext");

      // Look up our data source
      System.out.println("looking up insuranceAdminTest");
      DataSource ds = (DataSource) jdbcContext.lookup("insuranceAdminTest");
      System.out.println("got DataSource");

      con = ds.getConnection();
      System.out.println("got connection");
    }
    catch (NamingException ex)
    {
      System.err.println("failed to obtain JNDI context");
      throw new HalvorException("failed to obtain JNDI context", ex);
    }
    catch (SQLException ex)
    {
      System.err.println("failed to open database connection");
      throw new HalvorException("failed to open database connection", ex);
    }


The output from the above is:

instantiating database connection
getting InitialContext
got InitialContext
enumeration name = insuranceAdminTest: org.apache.naming.ResourceRef
getting JDBC context
got jdbcContext
looking up insuranceAdminTest
failed to obtain JNDI context


The server.xml snippet is:

<Resource name="jdbc/insuranceAdminTest" auth="CONTAINER"
                      type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/insuranceAdminTest">
    <parameter><name>user</name><value>halvor</value></parameter>
    <parameter><name>password</name><value>halvor</value></parameter>

<parameter><name>driverClassName</name><value>org.gjt.mm.mysql.Driver</value
></parameter>

<parameter><name>driverName</name><value>jdbc:mysql:///InsuranceAdminTest</v
alue></parameter>
  </ResourceParams>

the web.xml snippet is:

  <resource-ref>
    <res-ref-name>jdbc/insuranceAdminTest</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>



What I dont understand is that the line that prints "enumeration name = "
find the insuranceAdminTest binding, but the subsequent lookup on the
jdbcContext Context fails...




Does anyone know what is wrong?


Many thanks - Adam Lipscombe



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>