在Tomcat中使用oracle设置JNDI数据源

时间:2012-01-21 11:59:00

标签: oracle tomcat jndi

我无法通过tomcat 7.0中从JNDI获取的DataSource获取连接,导致如下错误:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of cla
ss '' for connect URL 'null'

我在很多方面都遵循了doc和其他人的建议,但仍然无法修复它。以下是我的设置:

在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>

在Tomcat 7.0 \ lib中放置classes12.jar和ojdbc14.jar

有人能指出正确的方法吗?

我浪费了很多时间在这个配置上,但仍然无法使其工作。提前谢谢!

约翰


Stack Trace如下:

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

这是ds.getConnection上的代码异常。 (这不是获取DS的典型方式,bcoz需要与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();

2 个答案:

答案 0 :(得分:4)

我意识到我在我的TOMCAT_HOME / conf / context.xml和我的applicationPath / META-INF / context.xml中定义了两次资源。一旦我从TOMCAT_HOME / conf / context.xml中删除了资源,一切都很顺利。

答案 1 :(得分:2)

长期以来经历过相同的情景。

此链接Here 帮助了我。