DBCP problem "Name jdbc is not bound in this Context", SOLUTION

jonck_at_vanderkogel.net
Date: 12/23/04

  • Next message: IINET: "Re: Using Borland JBuilder for JSP and MySQL but can't Connect :-("
    Date: 23 Dec 2004 14:56:36 -0800
    
    

    Hi everybody,
    I've been struggling with this all day and during extensive googling I
    noticed that a lot of people are having problems with this and nobody
    (as far as I could find) actually found (or posted) the answer.
    Therefore I thought I would post the solution, as in the end it turned
    out to be very simple.

    If you follow the tutorial at the Tomcat website
    (http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html#Database%20Connection%20Pool%20(DBCP)%20Configurations),
    step 4 is to set up a JSP. A lot of people will not be doing this for
    JSP's in mind, but for something else; I wanted to figure out how to do
    this for a servlet-based webapp.
    So if you translate the tutorial found on the Tomcat site to your
    particular situation, and then try to find your DataSource, you will
    probably try to do so using the following lines of code, since that's
    how they describe it at
    http://dev.mysql.com/tech-resources/articles/connection_pooling_with_connectorj.html:

    <codeSnippet>
    InitialContext initCtx = new InitialContext();
    DataSource ds = (DataSource) initCtx.lookup("jdbc/yourName");
    </codeSnippet>

    You will get the error "Name jdbc is not bound in this Context". After
    hours and hours of tweaking and changing the Context element and the
    web application deployment descriptor file I found out that what is
    actually causing the problem lies elsewhere. The above code is *almost*
    correct, you need to change it to read:

    <codeSnippet>
    InitialContext initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/yourName");
    </codeSnippet>

    And voila, the DataSource will be found and off you go.

    Hope this helps and saves some time for someone out there.
    Regards, Jonck


  • Next message: IINET: "Re: Using Borland JBuilder for JSP and MySQL but can't Connect :-("