[英]com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure?
I am running Spring MVC application with hibernate.cfg as below:我正在使用 hibernate.cfg 运行 Spring MVC 应用程序,如下所示:
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="connection.url">jdbc:mysql://10.2.10.123:3306/xxx</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">xxx</property>
<property name="generate_statistics">false</property>
<property name="connection.password">xxx</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">50</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">7200</property>
<property name="current_session_context_class">thread</property>
<property name="c3p0.numHelperThreads">5</property>
<property name="show_sql">false</property>
When I run application in local environment, it runs without any problem.当我在本地环境中运行应用程序时,它运行没有任何问题。 But on server it gives exception as:但在服务器上,它给出了异常:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败
The last packet successfully received from the server was 206,756 milliseconds ago.从服务器成功接收的最后一个数据包是 206,756 毫秒前。 The last packet sent successfully to the server was 3 milliseconds ago.最后一个成功发送到服务器的数据包是 3 毫秒前。
Further I get following log:此外,我得到以下日志:
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3119)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3573)
... 90 more
DEBUG: 11:42:41.858 com.mchange.v2.resourcepool.BasicResourcePool.trace(BasicResourcePool.java:1644) - trace com.mchange.v2.resourcepool.BasicResourcePool@2b5cccf1 [managed: 7, unused: 6, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@5700187d)
DEBUG: 11:42:41.861 com.mchange.v2.resourcepool.BasicResourcePool.decrementPendingAcquires(BasicResourcePool.java:422) - decremented pending_acquires: 0
DEBUG: 11:42:41.858 com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:967) - Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@61b03322
DEBUG: 11:42:41.861 com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.finerLoggingTestPooledConnection(C3P0PooledConnectionPool.java:323) - Test of PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@c2beb14] on CHECKOUT has FAILED.
java.sql.SQLException: Connection is invalid
Can anyone help?任何人都可以帮忙吗?
我通过将 MySQL 服务器上的wait_timeout 和interactive_timeout 的值增加到3600 秒(1 小时)来解决这个问题,之前两者都设置为60 秒。
In my case, I faced this error when trying to connect to mysql-server running inside a container on a Ubuntu host VM from the same host.就我而言,当我尝试从同一主机连接到在 Ubuntu 主机 VM 上的容器内运行的 mysql-server 时遇到此错误。
Example: If my VM name is abc.company.com
, the following jdbc URL would not work :示例:如果我的 VM 名称是abc.company.com
,则以下 jdbc URL将不起作用:
jdbc:mysql://abc.company.com:3306/dbname
Above jdbc url would work fine from other machines like xyz.company.com
but just not abc.company.com
.上面的 jdbc url 可以在xyz.company.com
等其他机器上正常工作,但不能abc.company.com
。
where as the following jdbc URL would work just fine on abc.company.com
machine:以下 jdbc URL 在abc.company.com
机器上可以正常工作:
jdbc:mysql://localhost:3306/dbname
which led me to check the /etc/hosts
file.这让我检查了/etc/hosts
文件。
Adding the following line to /etc/hosts
fixed this issue:将以下行添加到/etc/hosts
修复了此问题:
127.0.1.1 abc.company.com abc
This seems to be an OS bug that requires us to add these on some Ubuntu versions.这似乎是一个操作系统错误,需要我们在某些 Ubuntu 版本上添加这些。 Reference: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution参考: https : //www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution
Before trying this, I had tried all other solutions like GRANT ALL..
, changing the bind-address
line in mysql.cnf.. None of them helped me in this case.在尝试此之前,我已经尝试了所有其他解决方案,例如GRANT ALL..
,更改 mysql.cnf 中的bind-address
行.. 在这种情况下,他们都没有帮助我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.