无法创建“的连接网址“空”

问题描述:

我使用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

为了让所有3种的webapps正确使用相同的数据源,我有我的所有<Resource>项从META-INF/context.xml的文件夹移动到服务器的$ CATALINA_BASE/conf目录/上下文。 xml文件夹。不是一个很好的解决方案,但它的工作。

+0

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

+0

谢谢。经过6个小时的狩猎之后,您的解决方案才有效。 – Hafnernuss

如上回答,一种解决方案是移动条目以$ 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文件包裹起来的数据库连接属性。

我遇到了同样的问题,事实证明我的名称声明在context.xml上与在web.xml上的名称<resource-ref>上定义的名称声明不匹配。