2011-09-01 57 views
15

我使用Tomcat 7.0.12和接收這個錯誤,每當我attmept通過的.jsp頁面訪問JNDI數據源連接到一個PostgreSQL數據庫中的web應用程序類」的JDBC驅動程序稱爲'ROOT':無法創建「的連接網址「空」

SEVERE: Servlet.service() for servlet [jsp] in context with path [] threw exception 
[java.lang.RuntimeException: Cannot create JDBC driver of class '' for connect URL 'null'] with root cause 
java.lang.NullPointerException 
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507) 
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476) 
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307) 
at java.sql.DriverManager.getDriver(DriverManager.java:253) 
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437) 
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371) 
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) 

postgresql JDBC驅動程序位於我的CATALINA/lib文件夾中。

這裏是我的META-INF/context.xml中:

<?xml version="1.0" encoding="UTF-8"?> 

<Context> 

<Resource name="jdbc/webdbro" auth="Container" type="javax.sql.DataSource" 
    driverClassName="org.postgresql.Driver" url="jdbc:postgresql://127.0.0.1:5432/webdb" 
    username="webdbro" password="pass" maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="30"/> 

<Resource name="jdbc/webdbrw" auth="Container" type="javax.sql.DataSource" 
    driverClassName="org.postgresql.Driver" url="jdbc:postgresql://127.0.0.1:5432/webdb" 
    username="webdbrw" password="pass" maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="30"/> 

<Resource name="jdbc/shadowdbro" auth="Container" type="javax.sql.DataSource" 
    driverClassName="org.postgresql.Driver" url="jdbc:postgresql://127.0.0.1:5432/shadowdb" 
    username="shadowdbro" password="pass" maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="30"/> 

</Context> 

這裏是我的WEB-INF/web.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0"> 
<display-name>ROOT</display-name> 

<resource-ref> 
    <description>Read only webdb connector.</description> 
    <res-ref-name>jdbc/webdbro</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 
<resource-ref> 
    <description>Read write webdb connector.</description> 
    <res-ref-name>jdbc/webdbrw</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 
<resource-ref> 
    <description>Read only shadow db connector.</description> 
    <res-ref-name>jdbc/shadowdbro</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

</web-app> 

奇怪的是,我還有其他2種的webapps使用完全相同的配置(web.xml和context.xml)運行在同一個Tomcat服務器上,以便他們可以使用JNDI方法連接到數據庫,並且這兩個Web應用程序的這兩個工作都很好 - 我可以查詢並更新數據庫這些應用中的問題或例外。 TIA ...

+0

異常告訴Tomcat的未讀/找到你配置了''。你是從Eclipse內部運行webapp還是什麼? – BalusC

回答

17

爲了讓所有3種的webapps正確使用相同的數據源,我有我的所有<Resource>項從META-INF/context.xml的文件夾移動到服務器的$ CATALINA_BASE/conf目錄/上下文。 xml文件夾。不是一個很好的解決方案,但它的工作。

+0

更新到您的context.xml不生效,在$ CATALINA_BASE/conf/context.xml文件,除非你可以重新部署web應用程序或應用自己他們文件夾中。 – EJP

+0

謝謝。經過6個小時的狩獵之後,您的解決方案纔有效。 – Hafnernuss

2

如上回答,一種解決方案是移動條目以$ CATALINA_BASE/conf/context.xml文件

另一種方法描述如下: http://blogs.agilefaqs.com/2009/11/23/cannot-create-jdbc-driver-of-class-for-connect-url-null/

特別是「複製context.xml文件到tomcat/conf目錄/卡塔利娜/本地主機文件夾,並將其重命名爲<web_app_name>.xml

的根本原因可能是由於文件權限的一個問題:」在其下的tomcat正在運行沒有寫權限對這些文件夾的用戶。所以我們改變了團隊的所有者p的tomcat和/ etc/tomcat文件夾到tomcat用戶所屬的同一組,並且魔術般的一切都開始像以前一樣工作了。「

該解決方案似乎是「清潔劑」對我來說,因爲你可以再有你的整個Web應用程序,包括一個.war文件包裹起來的數據庫連接屬性。

3

我遇到了同樣的問題,事實證明我的名稱聲明在context.xml上與在web.xml上的名稱<resource-ref>上定義的名稱聲明不匹配。

相關問題