AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
jonck@vanderkogel.net
Guest





PostPosted: Thu Dec 23, 2004 10:56 pm    Post subject: DBCP problem "Name jdbc is not bound in this Context", SOLUT Reply with quote



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
([url]http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html#Database%20Connection%20Pool%20(DBCP)%20Configurations)[/url],
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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.