为 mysql 驱动程序使用 hikari cp 时 JNDI 查找失败

JNDI lookup fails when using hikari cp for mysql driver

我已经把资源信息放在 context.xml 我的 webapp 的 META-INF 下, 以下是context.xml文件

<?xml version='1.0' encoding='utf-8'?>

<Resource name="jdbc/MysqlHikari" 
    auth="Container"
    factory="com.zaxxer.hikari.HikariJNDIFactory" 
    type="javax.sql.DataSource"
    maximumPoolSize="10" 
    connectionTimeout="10000"
    jdbcUrl="jdbc:mysql://127.0.0.1:3306/testdb?verifyServerCertificate=false&amp;useSSL=true"
    driverClassName="com.cj.mysql.jdbc.Driver"
    dataSource.implicitCachingEnabled="true"
    dataSource.cachePrepStmts="true"
    dataSource.prepStmtCacheSize="250"
    dataSource.prepStmtCacheSqlLimit="2048"
    dataSource.user="root"
    dataSource.password="root" />

正在使用的mysqlconnector/j版本是6.0.6 java 版本为 1_8_161

我正在尝试使用以下方法执行查找

    try {

        Context ctx = new InitialContext();

        ds = (HikariDataSource)ctx.lookup("java:/comp/env/jdbc/MysqlHikari");

    }catch(Exception e) {
        LOG.error("Error looking up datasource "+e.toString());
    }

这是在init方法内部完成的 然而,查找一直失败,并显示以下堆栈跟踪

[23-Feb-2018 14:50:48.438] [com.zaxxer.hikari.HikariConfig] [DEBUG] - Driver class com.cj.mysql.jdbc.Driver not found in Thread context class loader WebappClassLoader
context: Servelet
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@13221655
, trying classloader WebappClassLoader
context: Servelet
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@13221655

[23-Feb-2018 14:50:48.440] [com.zaxxer.hikari.HikariConfig] [ERROR] - Failed to load driver class com.cj.mysql.jdbc.Driver from HikariConfig class classloader WebappClassLoader
context: Servelet
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@13221655

[23-Feb-2018 14:50:48.441] [com.zaxxer.hikari.util.PropertyElf] [ERROR] - Failed to set property driverClassName on target class com.zaxxer.hikari.HikariConfig
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.zaxxer.hikari.util.PropertyElf.setProperty(PropertyElf.java:146)
at com.zaxxer.hikari.util.PropertyElf.lambda$setTargetFromProperties[=14=](PropertyElf.java:57)
at java.util.Hashtable.forEach(Hashtable.java:878)
at com.zaxxer.hikari.util.PropertyElf.setTargetFromProperties(PropertyElf.java:52)
at com.zaxxer.hikari.HikariConfig.<init>(HikariConfig.java:135)
at com.zaxxer.hikari.HikariJNDIFactory.createDataSource(HikariJNDIFactory.java:71)
at com.zaxxer.hikari.HikariJNDIFactory.getObjectInstance(HikariJNDIFactory.java:59)
at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.apache.naming.NamingContext.lookup(NamingContext.java:841)
at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at foodiniq.CheckProValidityServelet.init(CheckProValidityServelet.java:61)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:828)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: Failed to load driver class com.cj.mysql.jdbc.Driver in either of HikariConfig class loader or Thread context classloader
at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:512)
... 44 common frames omitted

如评论所述,我在驱动程序中输入错误 class 名称

正确的名字是:

com.mysql.cj.jdbc.Driver

相反,我使用的是:

com.cj.mysql.jdbc.Driver

对于使用 connector/j 5.1 或以下版本的用户,正确的 class 是:

com.mysql.jdbc.Driver

对于更高版本,这仍然有效,但会显示一条警告,说明它已被弃用。所以只需使用最新版本(截至撰写本文时),即:

com.mysql.cj.jdbc.Driver