Tomcat 9 / JNDI 数据源 - 无法为连接 URL 'null'创建类的 JDBC 驱动程序


  • Java 版本: 10.0.1
  • 雄猫版本:9.0.10
  • 后发驱动程序版本:42.2.4

我在stackoverflow上看到了一个类似的问题,但它与我的有点不同,而且那里的解决方案似乎都不适合我: 无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序:我不明白这个例外

这是我第一次使用 Tomcat 9,也是我第一次尝试使用 JNDI for DataSources。

使用文档作为参考:https://tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html

似乎无法从<资源>读取驱动程序/url/等属性...虽然我一辈子都想不通为什么。

错误:

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:432)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

设置/配置:

Postgresql-42.2.4.jar in tomcat lib.

服务器.xml代码段:

<GlobalNamingResources>
. . .
<Resource name="jdbc/xxxx" auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/xxxx"
username="xxxx"
password="xxxx"
maxTotal="20"
maxIdle="10"
maxWaitMillis="15000" />
</GlobalNamingResources>

网络应用网络.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<resource-ref>
<description>xxxx Connection</description>
<res-ref-name>jdbc/xxxx</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>Anwinity Test App</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.anwinity.webapps.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Anwinity Test App</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>

Java 资源类:

package com.anwinity.webapps.test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("test")
public class TestResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response test() throws Exception {
// InitialContext ctx = new InitialContext();
// DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/xxxx");
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/xxxx");
if(ds == null) {
System.out.println(":(");
return Response.noContent().build();
}
else {
try(Connection conn = ds.getConnection()) {
String sql = "select * from test";
try(Statement stmt = conn.createStatement()) {
List<Integer> list = new ArrayList<>();
try(ResultSet rs = stmt.executeQuery(sql)) {
while(rs.next()) {
int i = rs.getInt(1);
System.out.println(i);
list.add(i);
}
TestResponse resp = new TestResponse();
resp.setN(list.stream().mapToInt(i -> i).toArray());
return Response.ok(resp).build();
}
}
}
}
}
}

您正在 web.xml 文件中定义资源引用,请尝试使用 context.xml 文件