本文介绍了Tomcat / Hibernate与MySql的连接失败,出现“通信链接失败” &安培; “许可被拒绝”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Hibernate从tomcat Web应用程序连接到本地主机上的MySql(= MariaDB)(但可以称它为A),但一直保持获取通信链接失败的状态(下面附有完整的异常追踪)。



我在另一台机器上有另一个数据库副本(可以称之为B),也可以使用第三台计算机进行开发(让我们称它为C)。 >

1)从C连接到任何数据库(A和B)都没有问题。
2)在A上,我本地连接到数据库没有问题(运行'mysql -u'命令)。我也没有问题做端口3306上的Telnet到A或B,所以我假设这两个DB已启动并正在运行并可用于连接。



唯一的问题是何时我试图从生产tomcat(在A上运行)连接到A或B数据库。我收到错误:

  com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败

成功发送到服务器的最后一个数据包是0毫秒前。驱动程序尚未收到来自服务器的任何数据包。

从这个错误的类似案例中,我看到大多数错误都是由于无效的用户/传递凭据或非法IP。我也尝试将ip更改为localhost,127.0.0.1和10.90.41.155(这是本机的实际ip)。没有什么帮助,也要记住,连接只会在A上的特定的tomcat安装失败。



我也尝试了以下方法:
1) Djava.net.preferIPv4Stack = true到catalina启动
2)试图连接到非法地址/端口 - 这改变了(如预期的)最终错误从拒绝权限变为无路由到主机。
3)在我的连接字符串中添加了以下内容:autoReconnect = true& useUnicode = true& createDatabaseIfNotExist = true& characterEncoding = utf-8 - 这导致了相同的错误(尝试重新连接3次。 +权限被拒绝(连接失败)
4)尝试了两个数据库URL。为本地数据库尝试localhost,127.0.0.1和实际的IP。在连接字符串中尝试使用和不使用:3306

附加的是hibernate.cfg文件+异常的完整服务器跟踪:

 <?xml version =1.0encoding =utf-8?> 
<!DOCTYPE hibernate-configuration SYSTEM
http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd\">

< hibernate-configuration>
< session-factory>
< property name =hibernate.dialect>
org.hibernate.dialect.MySQLDialect
< / property>
< property name =hibernate.connection.driver_class>
com.mysql.jdbc.Driver
< / property>

< property name =hibernate.connection.url>
jdbc:mysql://10.90.41.151/boonder
< / property>
< property name =hibernate.connection.username>
root
< / property>
< property name =hibernate.connection.password>
XXXXXXXXXX
< / property>

< mapping resource =User.hbm.xml/>
< mapping resource =Issue.hbm.xml/>
< mapping resource =TraderIssue.hbm.xml/>

< / session-factory>
< / hibernate-configuration>

例外情况是:

 类型异常报告

消息处理程序派送失败;嵌套异常是java.lang.ExceptionInInitializerError

描述服务器遇到内部错误,导致它无法完成此请求。

异常

org.springframework.web.util.NestedServletException:处理程序调度失败;嵌套的例外是java.lang.ExceptionInInitializerError
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:982)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet。服务(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter( WsFilter.java:52)
根本原因

java.lang.Except ionInInitializerError
com.aldondy.boonder.bl.DataSingleton。< init>(DataSingleton.java:54)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework .web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapt er.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet的。
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web。 servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter( CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.serv er.WsFilter.doFilter(WsFilter.java:52)
根本原因

org.hibernate.service.spi.ServiceException:无法创建请求的服务[org.hibernate.engine.jdbc。 env.spi.JdbcEnvironment]
org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
org.hibernat e.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
org.hibernate。 boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
org。 hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
org.hibernate.cfg.Configuration。 buildSessionFactory(Configuration.java:691)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton。< init>(DataSingleton.java :48)
com.aldondy.boonder.bl.DataSingleton.instance(Da taSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl。的java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180 )
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java :428)
org。 springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet。 FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com。 aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因

org.hibernate.exception.JDBCConnectionException:调用Driver#connect
时出错org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator $ 1 $ 1.convert(BasicConnectionCreator.java:101)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
org.hibernate.engine.jdbc.connections.internal.PooledConnections。< init>(PooledConnections.java:42)
org.hibernate.engine.jdbc.connections.internal.PooledConnections。< init> ;(PooledCon nections.java:20)
org.hibernate.engine.jdbc.connections.internal.PooledConnections $ Builder.build(PooledConnections.java:161)
...
org.hibernate.cfg .Configuration.buildSessionFactory(Configuration.java:691)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton。< init>( DataSingleton.java:48)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java: 62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl .invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang。 reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet。 mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web。 servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest( FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springf ramework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers。 CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因

com。 mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败

成功发送到服务器的最后一个数据包是0毫秒前。驱动程序尚未收到来自服务器的任何数据包。
com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:166)
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:58)
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:853)
com.mysql.cj.jdbc.ConnectionImpl。< init>(ConnectionImpl.java:440)
com .mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
org.hibernate.engine.jdbc .connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)

。 ..

org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
org.hibernate.cfg.Configuration。 buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton。< init>(DataSingleton.java:48)
com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton .java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl。 java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440 )
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org .springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet .FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java :846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因

com.mysql.cj.core.exceptions.CJCommunicationsException:通信链路故障

成功发送到服务器的最后一个数据包是0毫秒前。驱动程序尚未收到来自服务器的任何数据包。
sun.reflect.NativeConstructorAccessorImpl.newInstance0(本地方法)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45 )
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
com .mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
com.mysql .cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83)
com.mysql.cj .mysqla.MysqlaSession.connect(Mysqla Session.java:204)
com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:975)
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
com.mysql.cj.jdbc.ConnectionImpl。< init>(ConnectionImpl.java:440)
com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
org .hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
org.hibernate.engine.jdbc.connections.internal.PooledConnections。< init>(PooledConnections.java:42)
org.hibernate.engine.jdbc.connections.internal.PooledConnections。< init>(PooledConnections.java:20)
org.hibernate.engine.jdbc.connections.internal.PooledConnections $ Builder.build(PooledConnections.java :161)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)。 )
org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configur eService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess。的java:111)
org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java: 418)
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
组织。 hibernate.cfg.Config uration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton。< init>(DataSingleton.java:48)
com.aldondy.boonder.bl.DataSingleton.instance (DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl .java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke( DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java: 180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMetho d(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch( DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework。 web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers.CharacterEncodingFilter。 doFilter(CharacterEncodingFilter.java:27)
或g.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因

java.net.SocketException:权限被拒绝(连接失败)
java.net.PlainSocketImpl.socketConnect(本地方法)
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
java.net.Socket.connect(Socket.java:589 )
com.mysql.cj.core.io.StandardSocketFactory.connect(StandardSocketFactory.java:202)
com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:57)
com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:204)
com.mysql.cj.jdbc.ConnectionImpl .connectOneTryOnly(ConnectionImpl.java:975)
com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
com.mysql.cj.jdbc.ConnectionImpl< INIT>( ConnectionImpl.java:440)
com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java: 58)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
org.hibernate.engine.jdbc.connections.internal.PooledConnections<初始化> (PooledConnections.java:42)
org.hibernate.engine.jdbc.connections.internal.PooledConnection s。< init>(PooledConnections.java:20)
org.hibernate.engine.jdbc.connections.internal.PooledConnections $ Builder.build(PooledConnections.java:161)
org.hibernate.engine .jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
org.hibernate作为.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
org.hibernate.service.internal .AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiato r.java:145)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator。 initiateService(JdbcEnvironmentInitiator.java:35)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl。 Java的:259)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)

...
org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691 )
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
com.aldondy.boonder.bl.DataSingleton。< init>(DataSingleton.java:48)
com.aldondy.boonder .bl.DataSingleton.instance(DataSingleton.java:74)
com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
com.aldondy.boonder.web.controllers .LoginCtrl.login(LoginCtrl.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
的太阳。 reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter。句柄(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDis (DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java: 970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
组织。 springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
com.aldondy.boonder.web.controllers。 CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
注意根源的完整堆栈跟踪在Apache Tomcat / 7.0.76日志。


解决方案

我也跟踪了这个问题。我的症状与您的症状完全相同,我甚至尝试了您尝试的确切事物。总之,我的问题只是因为我在SELinux的centos上使用了tomcat。



有些人帮助我诊断使用以下命令来查看安全事件以及为什么某些操作是不允许:

  sudo ausearch -m avc 
sudo greptcp_socket/ var / log / audit / audit。日志| audit2why //将日志翻译为审计记录生成的某些人为理解的原因

我看到的条目如下:


$ b

And then found this article about tomcat and SELinux


$ b$b

You can run the following command to see if tomcat is running in the tomcat_t security context.

$ps auxZ | grep tomcat 

For the purposes of truly determining if my error was SELinux, I disabled SELinux

$ sestatus // shows: SELinux status:                 enabled 
$ sudo vim /etc/selinux/config // set \"SELINUX=disabled\"
$ sudo shutdown -r now
$ sestatus // shows: SELinux status: disabled

restarted my machine and saw that tomcat was now making the outbound connection.



Great, now I at least know WHY that was happening. Of course if you need SELinux, disabling it is NOT a solution. Another workaround which is probably not a solution is to install tomcat without going through yum. Yum will installed tomcat using the security context.



I think the course of action now is to modify the security context or set of rules governing tomcat to allow it to make outgoing connections.


I'm trying to connect to MySql (=MariaDB) on the localhost (lets call it A) from a tomcat webapp using Hibernate, but keep getting Communication link failure (the full exception trace attached below).

I have another replica of the DB on a different machine (lets call it B) and also using a 3rd computer for development (lets call it C).

1) I have no issues connecting from C to either of the DBs (A & B).2) On "A" I have no problems connecting locally to the DB (running 'mysql -u' command). I also have no problems doing telnet on port 3306 to either A or B. so I assume both DBs are up and running and available for connections.

The only problem is when I'm trying to connect from the production tomcat (running on A) to either A or B db. I get the error:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

From similar cases of this error, I saw the majority of errors were due to invalid user/pass credentials or illegal IPs. I also tried to change the ip to localhost , 127.0.0.1 and 10.90.41.155 (which is the actual ip of the machine). Nothing helps, and also keep in mind that connection only fails from the specific tomcat installation on A.

I also tried the following:1) Added "-Djava.net.preferIPv4Stack=true" to catalina startup2) Tried to connect to illegal addresses/ports - this changed (as expected) the final error from "permission denied" into "no route to host".3) Added the following to my connection string: "autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8" - this resulted in the same error ("Attempted reconnect 3 times. Giving up." + "Permission denied (connect failed)"4) Tried both DB urls. for the local DB tried with localhost, 127.0.0.1 and the actual ip. tried both with and without ":3306" in the connection string

Attached is the hibernate.cfg file +full server trace of the exception:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://10.90.41.151/boonder
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
   XXXXXXXXXX
   </property>

   <!-- List of XML mapping files      -->
   <mapping resource="User.hbm.xml"/>
   <mapping resource="Issue.hbm.xml"/>
   <mapping resource="TraderIssue.hbm.xml"/>

</session-factory>
</hibernate-configuration>

Exception is:

        type Exception report

        message Handler dispatch failed; nested exception is java.lang.ExceptionInInitializerError

        description The server encountered an internal error that prevented it from fulfilling this request.

        exception

        org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.ExceptionInInitializerError
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:982)
            org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
            org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
            org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
            org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
            com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause

        java.lang.ExceptionInInitializerError
            com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:54)
            com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
            com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
            com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            java.lang.reflect.Method.invoke(Method.java:498)
            org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
            org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
            org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
            org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
            org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
            com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause

        org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
            org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
            org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
            org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
            org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
            org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
            com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
            com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
            com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
            com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            java.lang.reflect.Method.invoke(Method.java:498)
            org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
            org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
            org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
            org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
            org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
            com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause

        org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
            org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
            org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:101)
            org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
            org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
            org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
    ...
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
            com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
            com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
            com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
            com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            java.lang.reflect.Method.invoke(Method.java:498)
            org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
            org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
            org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
            org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
            org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
            com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause

        com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

        The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
            com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:166)
            com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:58)
            com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:853)
            com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
            com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
            com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
            org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
            org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)

    ...

    org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
            org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
            org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
            com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
            com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
            com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
            com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            java.lang.reflect.Method.invoke(Method.java:498)
            org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
            org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
            org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
            org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
            org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
            com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause

        com.mysql.cj.core.exceptions.CJCommunicationsException: Communications link failure

        The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
            sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
            com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
            com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
            com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
            com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83)
            com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:204)
            com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:975)
            com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
            com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
            com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
            com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
            org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
            org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
            org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
            org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
            org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
     org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
            org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
            org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
            org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
            org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
            com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
            com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
            com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
            com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            java.lang.reflect.Method.invoke(Method.java:498)
            org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
            org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
            org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
            org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
            org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
            com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause

        java.net.SocketException: Permission denied (connect failed)
            java.net.PlainSocketImpl.socketConnect(Native Method)
            java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
            java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
            java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
            java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
            java.net.Socket.connect(Socket.java:589)
            com.mysql.cj.core.io.StandardSocketFactory.connect(StandardSocketFactory.java:202)
            com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:57)
            com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:204)
            com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:975)
            com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
            com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
            com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
            com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
            org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
            org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
            org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
            org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
            org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
            org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
            org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
            org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
            org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
            org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
            org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)

...
    org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
            org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
            com.aldondy.boonder.bl.DataSingleton.<init>(DataSingleton.java:48)
            com.aldondy.boonder.bl.DataSingleton.instance(DataSingleton.java:74)
            com.aldondy.boonder.web.controllers.LoginCtrl.loginInner(LoginCtrl.java:62)
            com.aldondy.boonder.web.controllers.LoginCtrl.login(LoginCtrl.java:41)
            sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            java.lang.reflect.Method.invoke(Method.java:498)
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
            org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
            org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
            org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
            org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
            org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
            com.aldondy.boonder.web.controllers.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:27)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        note The full stack trace of the root cause is available in the Apache Tomcat/7.0.76 logs.
解决方案

I just also tracked down this issue. My symptoms were the exact same as yours and I even tried the exact things you tried. In summary my problem only occured because I was using tomcat on centos with SELinux.

Some folks helped me diagnose using the following commands to look at security events and why some actions were not permittted:

sudo ausearch -m avc
sudo grep "tcp_socket" /var/log/audit/audit.log | audit2why // translates log into some human readable reasons why the audit record was generated

I saw entries like:

And then found this article about tomcat and SELinux https://noobient.com/post/165972214381/selinux-woes-with-tomcat-on-centos-74

You can run the following command to see if tomcat is running in the tomcat_t security context.

$ps auxZ | grep tomcat

For the purposes of truly determining if my error was SELinux, I disabled SELinux

$ sestatus // shows: SELinux status:                 enabled
$ sudo vim /etc/selinux/config // set "SELINUX=disabled"
$ sudo shutdown -r now
$ sestatus // shows: SELinux status:                 disabled

restarted my machine and saw that tomcat was now making the outbound connection.

Great, now I at least know WHY that was happening. Of course if you need SELinux, disabling it is NOT a solution. Another workaround which is probably not a solution is to install tomcat without going through yum. Yum will installed tomcat using the security context.

I think the course of action now is to modify the security context or set of rules governing tomcat to allow it to make outgoing connections.

这篇关于Tomcat / Hibernate与MySql的连接失败,出现“通信链接失败” &安培; “许可被拒绝”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:49