Setting up JNDI Datasource with oracle in Tomcat

John picture John · Jan 21, 2012 · Viewed 26.9k times · Source

I can't make it work to get connection from DataSource obtained from JNDI in tomcat 7.0, resulting in error like this:

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

I have done in many ways following the doc and other people suggestions, but still not able to fix it. Here are my settings:

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

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

Also, I tried to put that in server.xml, also not work

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

Put classes12.jar as well as ojdbc14.jar in Tomcat 7.0\lib

Can anyone point me the right way?

I wasted so much time on that configuration but still cannot make it work. Thanks in advance!

John


Stack Trace as below:

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

And here is the code, exception at ds.getConnection. (It is not typical way to get DS, bcoz need to match WebSphere way...)

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();

Answer

Cody Cusic picture Cody Cusic · Nov 8, 2012

I realized that I defined the resource twice, once in my TOMCAT_HOME/conf/context.xml and in my applicationPath/META-INF/context.xml. Once I removed the resource from TOMCAT_HOME/conf/context.xml everything worked great.