java jdk 数据源,在Tomcat中为Oracle 11g安装JNDI数据源

用户在Windows XP环境下使用Tomcat6尝试配置JNDI数据源连接Oracle11g数据库,但遇到NullPointerException。已经确认其他应用和JDBC直连方式可以正常连接。问题可能出在server.xml和context.xml的配置上,尝试了多种方法,包括检查资源定义和上下文查找,但未成功。用户希望找到解决方案,以便让所有Tomcat内的应用都能访问该数据库。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值