无法为连接 URL 'null' 创建 class 的 JDBC 驱动程序 - HikariCP、Tomcat8、PostgreSQL
Cannot create JDBC driver of class '' for connect URL 'null" - HikariCP, Tomcat8, PostgreSQL
首先,我以前从未设置过 JDBC 池(如果这看起来很普通,我很抱歉)。我已经尝试了一段时间来解决这个问题,但无济于事。我已经探索了各种其他 Whosebug 帖子的建议选项,但 none 已经成功。
我试过:
- 仅在 tomcat 的 /lib 中包含 'pgjdbc-ng' 驱动程序,并将上下文移动到 catalina home conf/
- 切换到使用 Hikari 给出的示例(但收到相同的错误)https://github.com/brettwooldridge/HikariCP/wiki/JNDI-DataSource-Factory-(Tomcat,-etc.)
我正在将 HikariCP 与 PostgreSQL(pgjdbc-ng) 驱动程序一起使用。 Tomcat8 部署了我使用 Maven 构建的 war 文件。 web.xml、context.xml、java代码。
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代码
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();
}
输出:
连接失败!
java.sql.SQLException: 无法为连接 URL 'null'
创建 class '' 的 JDBC 驱动程序
ds.getConnection() 抛出错误。
任何关于我如何解决这个问题或进一步调试的建议将不胜感激。
已解决!!! - context.xml 没有被放入 META-INF 文件夹
对于 JNDI 提供程序,请勿使用 dataSourceClassName
(或 dataSource.URL
)属性。如有必要,请使用 jdbcUrl
和 driverClassName
(但请先尝试不使用它)。
context.xml 未放入 META-INF 文件夹
首先,我以前从未设置过 JDBC 池(如果这看起来很普通,我很抱歉)。我已经尝试了一段时间来解决这个问题,但无济于事。我已经探索了各种其他 Whosebug 帖子的建议选项,但 none 已经成功。
我试过:
- 仅在 tomcat 的 /lib 中包含 'pgjdbc-ng' 驱动程序,并将上下文移动到 catalina home conf/
- 切换到使用 Hikari 给出的示例(但收到相同的错误)https://github.com/brettwooldridge/HikariCP/wiki/JNDI-DataSource-Factory-(Tomcat,-etc.)
我正在将 HikariCP 与 PostgreSQL(pgjdbc-ng) 驱动程序一起使用。 Tomcat8 部署了我使用 Maven 构建的 war 文件。 web.xml、context.xml、java代码。
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代码
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();
}
输出: 连接失败!
java.sql.SQLException: 无法为连接 URL 'null'
创建 class '' 的 JDBC 驱动程序ds.getConnection() 抛出错误。 任何关于我如何解决这个问题或进一步调试的建议将不胜感激。
已解决!!! - context.xml 没有被放入 META-INF 文件夹
对于 JNDI 提供程序,请勿使用 dataSourceClassName
(或 dataSource.URL
)属性。如有必要,请使用 jdbcUrl
和 driverClassName
(但请先尝试不使用它)。
context.xml 未放入 META-INF 文件夹