Cannot create JDBC driver of class '' for connect URL 'null" - HikariCP, Tomcat8, PostgreSQL

一世执手 提交于 2019-12-13 20:52:53

问题


Firstly I have never setup a JDBC Pool before (So i am sorry if this seems mundane). I have been trying for some time to solve the problem but to no avail. I have explored the suggested options from various other stackoverflow posts but none have been successful.

I have tried:

  • Including the 'pgjdbc-ng' driver only in the /lib of tomcat and moving the context to the catalina home conf/
  • Swapping to using the example given by Hikari (but received same error) https://github.com/brettwooldridge/HikariCP/wiki/JNDI-DataSource-Factory-(Tomcat,-etc.)

I am using HikariCP with a PostgreSQL(pgjdbc-ng) driver. Tomcat8 deploys my war file which i build using maven. web.xml, context.xml, java code.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name>Restful Web Application</display-name>
  <resource-ref>
    <res-ref-name>jdbc/postgresHikari</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <servlet>
    <servlet-name>Jersey Web Application</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.seng402.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/postgresHikari" auth="Container"
    factory="com.zaxxer.hikari.HikariJNDIFactory"
    type="javax.sql.DataSource"
    minimumIdle="5" 
    maximumPoolSize="10"
    connectionTimeout="300000"
    dataSource.implicitCachingEnabled="true" 
    dataSource.user="docker"
    dataSource.password="docker"
    jdbcUrl="jdbc:postgresql://192.168.59.103:5432/docker"
    driverClassName="com.impossibl.postgres.jdbc.PGDataSource"/>
</Context>

java code

Context initCtx = null;
    try {
        initCtx = new InitialContext();
        Context envCtx;
        try {
            envCtx = (Context) initCtx.lookup("java:comp/env");
            // Look up our data source
            DataSource ds = (DataSource) envCtx.lookup("jdbc/postgresHikari");
            try {
                // Allocate and use a connection from the pool
                Connection conn = ds.getConnection(); // throws the error
                conn.close();
                System.out.println("Connected!");
            } catch (SQLException e) {
                System.out.println("Failed to Connect!");
                e.printStackTrace();
            }
        } catch (NamingException e) {
            System.out.println("Failed to Lookup!");
            e.printStackTrace();
        }
    } catch (NamingException e1) {
        System.out.println("Fail to get Context!");
        e1.printStackTrace();
    }

Output: Failed to Connect!

java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'

ds.getConnection() throws the error. Any suggestions as to how i could fix this or debug further would be greatly appreciated.

SOLVED !!! - context.xml was not being put into the META-INF folder


回答1:


With the JNDI provider, do not use the dataSourceClassName (or dataSource.URL) properties. Use jdbcUrl, and driverClassName if necessary (but try without it first).




回答2:


context.xml was not being put into the META-INF folder



来源:https://stackoverflow.com/questions/32003999/cannot-create-jdbc-driver-of-class-for-connect-url-null-hikaricp-tomcat8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!