简体   繁体   中英

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure?

I am running Spring MVC application with hibernate.cfg as below:

    <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

The last packet successfully received from the server was 206,756 milliseconds ago. The last packet sent successfully to the server was 3 milliseconds ago.

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.

Example: If my VM name is abc.company.com , the following jdbc URL would not work :

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 .

where as the following jdbc URL would work just fine on abc.company.com machine:

jdbc:mysql://localhost:3306/dbname

which led me to check the /etc/hosts file.

Adding the following line to /etc/hosts fixed this issue:

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. Reference: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM