java - 无法为连接 URL '' 创建类 'null' 的 JDBC 驱动程序

标签 java postgresql tomcat jndi

我正在使用 Tomcat 7.0.12,每当我尝试通过名为“ROOT”的 Web 应用程序中的 .jsp 页面访问连接到 postgresql 数据库的 JNDI 数据源时,都会收到此错误:

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 方法连接到数据库和这两个 webapps工作得很好——我可以在这些应用程序中查询和更新数据库,没有任何问题或异常。 TIA...

最佳答案

为了让所有 3 个网络应用程序正确使用相同的数据源,我必须移动我所有的 <Resource> META-INF/context.xml 文件夹中的条目到服务器的 $CATALINA_BASE/conf/context.xml 文件夹中。这不是一个很好的解决方案,但它确实有效。

关于java - 无法为连接 URL '' 创建类 'null' 的 JDBC 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7274722/

相关文章:

apache - Tomcat 服务器已启动但未在浏览器上运行?

java - 绑定(bind)<丰富:listShuttle> or put two list in render attribute

java - java中变量numeros可能尚未初始化编译错误?

django - Docker-compose with django 无法将主机名 "db"转换为地址 : Name or service not known

java - 一个或多个监听器无法启动 SringMVC 电子商务项目

java - 使用大量数据提高 hibernate 插入性能的最佳方法

java - 使用自定义构造函数设置 BaseActivity 以从子类提供数据

java - 编写我自己的消费者线程——我没有看到什么?

sql - SELECT 查询合并/加入 PostgreSQL 中的两个表

python - Postgres 外部数据包装器 : insert to mysql table primary key increment issue