gpt4 book ai didi

java - 对 mysql 驱动程序使用 hikari cp 时 JNDI 查找失败

转载 作者:行者123 更新时间:2023-11-29 04:15:37 25 4
gpt4 key购买 nike

我已经将资源信息放在我的 webapp 的 META-INF 下的 context.xml 中,以下是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" />

使用的mysql connector/j版本是6.0.6java版本是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$0(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

最佳答案

如评论所述,我在驱动程序类名中有错字

正确的名字是:

com.mysql.cj.jdbc.Driver

相反,我使用的是:

com.cj.mysql.jdbc.Driver

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

com.mysql.jdbc.Driver

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

com.mysql.cj.jdbc.Driver

关于java - 对 mysql 驱动程序使用 hikari cp 时 JNDI 查找失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48944797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com