我无法使得从Tomcat 7.0的JNDI获取的DataSource连接起来工作,导致出现如下错误:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of cla
ss '' for connect URL 'null'
我已经按照文档和其他人的建议尝试了多种方式,但仍无法解决问题。以下是我的设置:
在 web.xml 中:
<resource-ref>
<description>MyDataSource</description>
<res-ref-name>jdbc/MyDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在context.xml文件中:<Context>
<Resource name="jdbc/MyDataSource" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="abc" password="abc" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@123.123.123.123:1521:xe"/>
<!-- // tried both driver class names but all not work driverClassName="oracle.jdbc.driver.OracleDriver" -->
</Context>
此外,我尝试将其放入server.xml中,但也没有起作用。
<Resource name="jdbc/MyDataSource" auth="Container" type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@123.123.123.123:1521:xe"
servicename="MyDataSource" username="abc" password="abc"
maxActive="20" maxIdle="10" maxWait="20000"/>
<ResourceParams name="jdbc/MyDataSource">
<parameter>
<name>user</name>
<value>abc</value>
</parameter>
<parameter>
<name>password</name>
<value>abc</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
</ResourceParams>
将classes12.jar和ojdbc14.jar放在Tomcat 7.0\lib目录下。
有人可以指点一下吗?
我在这个配置上浪费了很多时间,但还是无法使其工作。提前致谢!
John
以下是堆栈跟踪:
DS: org.apache.tomcat.dbcp.dbcp.BasicDataSource@497062
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at DBTest.doGet(DBTest.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(Unknown Source)
at java.sql.DriverManager.getDriver(Unknown Source)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 21 more
这是代码,exception 位于 ds.getConnection 处。(这不是获取 DS 的典型方式,因为需要匹配 WebSphere 的方式...)
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
initCtx = new InitialContext(env);
DataSource ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/MyDataSource");
System.err.println("DS: " + ds.toString());
Connection conn = ds.getConnection();