Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Apache Tomcat
|
Apache Tomcat General discussion of the Apache Tomcat servlet container. For discussions specific to the Professional Apache Tomcat book, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Apache Tomcat section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2005, 04:55 AM
Registered User
 
Join Date: Aug 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default JNDI setup in Embedded Tomcat 5.0.19

Hi All,

I have embedded Tomcat 5.0.19 in my application, which is a simple web based database application in servlets. In Embedded Tomcat we don't have server.xml.

Reference:
http://www.vsj.co.uk/articles/display.asp?id=319

I am trying to create JNDI Context to connect to my database.

My configuration file is like this:

<naming>
   <context >

     <resource name="jdbc/myDS" type="javax.sql.DataSource" >
       <parameter>
         <name>factory</name>
         <value>org.apache.commons.dbcp.BasicDataSourceFact ory</value>
       </parameter>
       <parameter>
         <name>driverClassName</name>
         <value>net.sourceforge.jtds.jdbc.Driver</value>
       </parameter>
       <parameter>
         <name>url</name>
         <value>jdbc:jtds:sqlserver://SQLServer:1433;databaseName=myDB;</value>
       </parameter>
       <parameter>
         <name>username</name>
         <value>sa</value>
       </parameter>
       <parameter>
         <name>password</name>
         <value>sa</value>
       </parameter>
     </resource>
   </context>
</naming>

I have put this config file in the root directory and called thru the following method.
XmlConfigurator.loadConfiguration(getClass().getRe sourceAsStream("/myConfig.xml"));

Ref:
http://directory.apache.org/subproje...ing/using.html

I have written a class which starts the embedded tomcat and calls the above method.

The following is the output of tomcat:


Apr 15, 2005 3:19:00 PM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
Apr 15, 2005 3:19:01 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.0.19
Apr 15, 2005 3:19:01 PM org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
Apr 15, 2005 3:19:01 PM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.util.LocalStrings', returnNull=tru
e
Apr 15, 2005 3:19:01 PM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.action.ActionResources', returnNul
l=true
Apr 15, 2005 3:19:02 PM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='view.ApplicationResources', returnNull=true
Apr 15, 2005 3:19:02 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on port 9889
Apr 15, 2005 3:19:02 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on port 9889




I have written another class which does the context lookup function and creates the Connection Object like this:

public Connection getConnection()
{
    Context ctx=new InitialContext();
        Context jdbcCtx = (Context)ctx.lookup("jdbc");
        DataSource datasource=(DataSource)jdbcCtx.lookup("myDS");
        return datasource.getConnection();
}

but every time my web application calls this function to create a new connection it throws:

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
        at org.apache.naming.NamingContext.lookup(NamingConte xt.java:815)
        at org.apache.naming.NamingContext.lookup(NamingConte xt.java:198)
        at org.apache.naming.SelectorContext.lookup(SelectorC ontext.java:183)
        at javax.naming.InitialContext.lookup(InitialContext. java:347)
        at view.DatabaseConnection.getConnection(DatabaseConn ection.java:109)
        at view.Admin_LoginValidation.doPost(Admin_LoginValid ation.java:42)
        at javax.servlet.http.HttpServlet.service(HttpServlet .java:763)
        at javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
        at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(Appl
icationFilterChain.java:284)
        at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationF
ilterChain.java:204)
        at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperV
alve.java:257)
        at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValv
eContext.java:151)
        at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.jav
a:567)
        at org.apache.catalina.core.StandardContextValve.invo keInternal(Standard
ContextValve.java:245)
        at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextV
alve.java:199)
        at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValv
eContext.java:151)
        at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.jav
a:567)
        at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.j
ava:184)
        at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValv
eContext.java:151)
        at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.j
ava:164)
        at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValv
eContext.java:149)
        at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.jav
a:567)
        at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineVal
ve.java:156)
        at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValv
eContext.java:151)
        at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.jav
a:567)
        at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:972)

        at org.apache.coyote.tomcat5.CoyoteAdapter.service(Co yoteAdapter.java:20
6)
        at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java
:833)
        at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.proce
ssConnection(Http11Protocol.java:732)
        at org.apache.tomcat.util.net.TcpWorkerThread.runIt(P oolTcpEndpoint.java
:619)
        at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadP
ool.java:688)
        at java.lang.Thread.run(Thread.java:534)
java.lang.NullPointerException


when I tried Context lookup in the same class which starts Tomcat, there is no problem: it gets the Name jdbc/myDB. How to make the Context Global.
Please help me out to solve this problem.

Thanks in Advance,

Pretty


 
Old May 4th, 2005, 07:57 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try to use follwing statement

Context ctx=new InitialContext();
DataSource datasource=(DataSource)ctx.lookup("jdbc/myDS");


 
Old May 29th, 2005, 01:46 PM
Registered User
 
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Has anybody made any progress on this issue? I'm seeing the same problem and haven't yet found a solution. Thanks, Joe

 
Old July 12th, 2005, 06:55 AM
Registered User
 
Join Date: Jul 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello all,

As part of the solution for embedded Tomcat you will need to add "naming-factory-dbcp.jar" to your classpath. Without it, you can get various JNDI exceptions at runtime.





Similar Threads
Thread Thread Starter Forum Replies Last Post
JBuilder/Tomcat/JNDI/JDBC DataSource cac07 BOOK: Professional Apache Tomcat 0 January 26th, 2006 12:28 PM
jndi tomcat anandp Apache Tomcat 1 July 13th, 2005 05:39 AM
Context Logs with Tomcat 5.0.19 shantawn Apache Tomcat 0 April 4th, 2005 05:53 AM
TomCat 5.0.19 CaseSensitive deactivate mulle78 Apache Tomcat 0 May 11th, 2004 07:05 AM
Tomcat JNDI DataSource issues mowoski Apache Tomcat 2 April 9th, 2004 12:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.