Tag Archives: Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

[Solved] Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

1. Today’s learning content

ERROR [com.alibaba.druid.pool.DruidDataSource] – create connection error, url: jdbc: mysql://127.0.0.1:3306/db_ local?serverTimezone=UTC& useSSL=false& autoReconnect=true, errorCode 0, state 08001 java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

This is actually caused by the database not being opened. Just open it

However, the default “wait_timeout” of the MySQL server is 28800 seconds, that is, 8 hours, which means that if a connection is idle for more than 8 hours, MySQL will automatically disconnect the connection, but the connection pool thinks the connection is still valid (because the validity of the connection is not verified). When the application applies for the connection, the above error will be caused.

So we’d better revise the time

Problem solving steps
1. Versions before mysql5 can directly add “autoreconnect = true” to the configuration of JDBC connection URL.

2. Set the MySQL global variable Change the value of wait_timeout to the maximum. Check the manual of mysql5 and find the wait under windows and the maximum value of Linux_timeout is 24 days and 365 days respectively.

Add a line at the end of the file my.ini: wait_timeout=1814400. (the file is in MySQL installation directory under windows and/etc/my.ini under Linux) the author’s directory is MySQL\data\MySQL server 8.0

Restart MySQL.

The specific exceptions are as follows:

[2019-04-20 18:05:24.161] [Druid-ConnectionPool-Create-1967852205] ERROR [com.alibaba.druid.pool.DruidDataSource] - create connection error, url: jdbc:mysql://127.0.0.1:3306/db_local?serverTimezone=UTC&useSSL=false&autoReconnect=true, errorCode 0, state 08001
java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
    at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:932)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:857)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
    at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:148)
    at com.alibaba.druid.filter.stat.StatFilter.connection_connect(StatFilter.java:211)
    at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:142)
    at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1410)
    at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1464)
    at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:1969)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure