2012-01-21 66 views
9

設置JNDI數據源與Oracle,我不能讓它的工作就是讓在Tomcat的7.0從JNDI獲得數據源的連接,導致錯誤這樣的:在Tomcat中

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

任何人都可以指點我嗎?

我在這種配置上浪費了很多時間,但仍無法使其工作。提前致謝!

約翰


堆棧跟蹤如下:

DS: [email protected] 
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(); 
+0

John,請發佈完整的StackTrace。 –

+0

@DarkRider,stacktrace add,也附上代碼片段 – John

回答

2

有過相同的情況下離開很久了。這個鏈接Here 幫助我。

+0

感謝您的幫助,我之前已經閱讀過這個鏈接,後面跟着很多建議,但仍然是同樣的問題... – John

4

我意識到我定義資源兩次,一次在我的TOMCAT_HOME/conf/context.xml和我的applicationPath/META-INF/context.xml中。一旦我從TOMCAT_HOME/conf/context.xml中刪除資源,一切都很好。