Search code examples
postgresqltomcat8hikaricp

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


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:

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


Solution

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