JDBC连接MySQL异常:Unable to create initial connections of pool 解决方法
异常信息:
26-Feb-2021 22:38:38.322 严重 [http-nio-8080-exec-10] org.apache.tomcat.jdbc.pool.ConnectionPool.init Unable to create initial connections of pool. java.sql.SQLException at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254) at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182) at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:701) at com.ctrip.platform.dal.dao.datasource.tomcat.DalConnectionPool.createConnection(DalConnectionPool.java:63) at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:635) at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:486) at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:144) at com.ctrip.platform.dal.dao.datasource.tomcat.DalConnectionPool.<init>(DalConnectionPool.java:28) at com.ctrip.platform.dal.dao.datasource.tomcat.DalTomcatDataSource.pCreatePool(DalTomcatDataSource.java:27) at com.ctrip.platform.dal.dao.datasource.tomcat.DalTomcatDataSource.createPool(DalTomcatDataSource.java:19) at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127) at com.ctrip.platform.dal.dao.datasource.RefreshableDataSource.getConnection(RefreshableDataSource.java:321) at com.ctrip.platform.dal.dao.datasource.DefaultDalConnectionLocator.getConnection(DefaultDalConnectionLocator.java:54) at com.ctrip.platform.dal.dao.client.DalConnectionManager.getConnectionFromDSLocator(DalConnectionManager.java:138) at com.ctrip.platform.dal.dao.client.DalConnectionManager.getNewConnection(DalConnectionManager.java:58) at com.ctrip.platform.dal.dao.client.DalTransactionManager.getConnection(DalTransactionManager.java:118) at com.ctrip.platform.dal.dao.client.DalTransactionManager.getConnection(DalTransactionManager.java:99) at com.ctrip.platform.dal.dao.client.DalDirectClient.getConnection(DalDirectClient.java:470) at com.ctrip.platform.dal.dao.client.DalDirectClient$1.execute(DalDirectClient.java:45) at com.ctrip.platform.dal.dao.client.DalConnectionManager._doInConnection(DalConnectionManager.java:190) at com.ctrip.platform.dal.dao.client.DalConnectionManager.doInConnection(DalConnectionManager.java:167) at com.ctrip.platform.dal.dao.client.DalDirectClient.doInConnection(DalDirectClient.java:459) at com.ctrip.platform.dal.dao.client.DalDirectClient.query(DalDirectClient.java:67) at com.ctrip.platform.dal.dao.task.FreeSqlQueryTask.execute(FreeSqlQueryTask.java:21) at com.ctrip.platform.dal.dao.task.DalSqlTaskRequest$SqlTaskCallable.execute(DalSqlTaskRequest.java:428) at com.ctrip.platform.dal.dao.task.DalSqlTaskRequest$SqlTaskCallable.call(DalSqlTaskRequest.java:334) at com.ctrip.platform.dal.dao.task.RequestTaskWrapper.call(RequestTaskWrapper.java:39) at com.ctrip.platform.dal.dao.task.DalRequestExecutor.nonCrossShardExecute(DalRequestExecutor.java:218) at com.ctrip.platform.dal.dao.task.DalRequestExecutor.internalExecute(DalRequestExecutor.java:192) at com.ctrip.platform.dal.dao.task.DalRequestExecutor.execute(DalRequestExecutor.java:176) at com.ctrip.platform.dal.dao.DalQueryDao.query(DalQueryDao.java:140) at com.ctrip.platform.dal.dao.DalQueryDao.query(DalQueryDao.java:130) at com.ctrip.platform.dal.daogen.dao.DaoOfLoginUser.getUserByNo(DaoOfLoginUser.java:46) at com.ctrip.platform.dal.daogen.resource.DalUserResource.userSignIn(DalUserResource.java:193) at jdk.internal.reflect.GeneratedMethodAccessor51.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161) at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99) at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102) at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:267) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473) at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at com.ctrip.platform.dal.daogen.filter.UserFilter.doFilter(UserFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:346) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:887) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1684) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:832) Caused by: java.lang.NullPointerException at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:468) at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)
MySQL版本:
mysql Ver 8.0.23 for osx10.16 on x86_64 (Homebrew)
解决方案:
MySQL 5 的驱动类型名称为:com.mysql.jdbc.Driver
MySQL 8 的驱动类型名称更改为:com.mysql.cj.jdbc.Driver
所以,如果使用的 MySQL 版本为:MySQL 8.x.x 的话,JDBC 连接 MySQL 的 driverClassName 应该配置为:com.mysql.cj.jdbc.Driver