I'm on Windows XP, using Tomcat 6 ( I can't upgrade to 7 until the end of the month ).
I've been trying to implement a JNDI database resource to Oracle 11g without success.
A number of other applications on my computer connect just fine with the same database credentials. I made a test JSP using straight up JDBC and put it into Tomcat. It connects just fine too.
I modified a section of my conf/server.xml like this:
auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:@apollo.abc.acme.com:2222:mydatabase"
user="joe"
password="blow"
maxActive="20"
maxIdle="30"
maxWait="-1"/>
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"/>
My conf/context.xml:
WEB-INF/web.xml
My conf/web.xml:
jdbc/mydb
javax.sql.DataSource
Container
This is an excerpt from test JSP, it is crapping out with a nullpointer exception right where it goes to get the JNDI resource:
Connection conn = null;
ResultSet result = null;
Statement stmt = null;
String nsdtestcount = null;
InitialContext ctx = null;
Context envContext = null;
javax.sql.DataSource ds = null;
try
{
ctx = new InitialContext();
envContext = (Context)ctx.lookup("java:/comp/env");
ds = (DataSource)envContext.lookup("jdbc/mydb");
conn = ds.getConnection();
}
catch (Exception e)
{
System.out.println(nameJSP + "Failed to connect to the database: " +
"\n ctx = " + ctx +
"\n envContext = " + envContext +
"\n ds = " + ds +
"\n conn = " + conn );
e.printStackTrace();
}
An excerpt from my log::
INFO: Server startup in 675 ms
testJNDI2.jsp: Failed to connect to the database:
ctx = javax.naming.InitialContext@15356d5
envContext = org.apache.naming.NamingContext@69d02b
ds = null
conn = null
java.lang.NullPointerException
at org.apache.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:114)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Thread.java:595)
The code at the line in the stack trace:
at org.apache.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:114)
ctx = new InitialContext();
envContext = (Context)ctx.lookup("java:/comp/env");
ds = (DataSource)envContext.lookup("jdbc/mydb");
conn = ds.getConnection();
conn = ds.getConnection(); is line 114
From my catalina log:
May 1, 2012 4:17:48 PM org.apache.tomcat.util.modeler.Registry registerComponent
SEVERE: Null component Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/mydb"
The contents of my CATALINA_HOME/lib:
C:\tomcat\lib>ls -l
annotations-api.jar
catalina-ant.jar
catalina-ha.jar
catalina-tribes.jar
catalina.jar
ecj-3.3.1.jar
el-api.jar
jasper-el.jar
jasper.jar
jsp-api.jar
log4j-1.2.16.jar
ojdbc14.jar
servlet-api.jar
tomcat-coyote.jar
tomcat-dbcp.jar
tomcat-i18n-es.jar
tomcat-i18n-fr.jar
tomcat-i18n-ja.jar
tomcat-juli-adapters.jar
tomcat-juli.jar
C:\tomcat\lib>
The contents of my JAVA/JDK jre/lib/ext:
C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext>ls -l
activation.jar
dnsns.jar
localedata.jar
log4j-1.2.16.jar
mail.jar
nls_charset12.jar
sunjce_provider.jar
sunmscapi.jar
sunpkcs11.jar
C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext>
Any ideas of what I can try? I would like to make the database resource available to everything running in Tomcat ( it is my dev environment )
Thanks in advance.
解决方案
As shown in this example on the tomcat site, I believe "user" should be "username" in your Resource definition.