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

时间:2018-08-19 07:14:44

标签: java tomcat jdbc jndi tomcat9

  • Java版本:10.0.1
  • Tomcat版本:9.0.10
  • Postgres驱动程序版本:42.2.4

我在stackoverflow上看到了一个类似的问题,但这与我的有点不同,那里的任何解决方案似乎都不适合我:Cannot create JDBC driver of class ' ' for connect URL 'null' : I do not understand this exception

这是我第一次使用Tomcat 9,也是我第一次尝试将JNDI用于数据源。

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

似乎无法从读取驱动程序/ url / etc属性,尽管我一生都无法找出原因。

错误:

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)

设置/配置:

tomcat库中的postgresql-42.2.4.jar。

server.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>

Web应用程序web.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();
                    }
                }
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您正在web.xml文件中定义resource-ref,请尝试使用context.xml文件