Java MySQL:Interttant com.MySQL.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障

Java MySQL:Interttant com.MySQL.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障,java,mysql,hibernate,jdbc,Java,Mysql,Hibernate,Jdbc,我时断时续地遇到这个异常,很难找到解决方法 此服务器上的负载非常低,并且应用程序与通过localhost(MySQL 5.6)连接的MySQL服务器位于同一主机上。我对“连接超时”(10)、“交互超时”(28800)和“等待超时”(28800)有合理的值,并且没有引起问题的防火墙 然而,这种错误时不时地出现: Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failur

我时断时续地遇到这个异常,很难找到解决方法

此服务器上的负载非常低,并且应用程序与通过localhost(MySQL 5.6)连接的MySQL服务器位于同一主机上。我对“连接超时”(10)、“交互超时”(28800)和“等待超时”(28800)有合理的值,并且没有引起问题的防火墙

然而,这种错误时不时地出现:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    Last packet sent to the server was 1 ms ago.
        at sun.reflect.GeneratedConstructorAccessor206.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
    at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4874)
    at com.mchange.v2.c3p0.impl.NewProxyConnection.setAutoCommit(NewProxyConnection.java:881)
    at org.hibernate.c3p0.internal.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:94)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
    ... 76 more
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:2431)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2882)
这就是我的Hibernate配置:

   .setProperty("hibernate.hbm2ddl.auto", "validate")
      .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect")
      .setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider")
      .setProperty("hibernate.c3p0.idle_test_period", "1000")
      .setProperty("hibernate.c3p0.min_size", "1")
      .setProperty("hibernate.c3p0.max_size", "20")
      .setProperty("hibernate.c3p0.timeout", "1800")
      .setProperty("hibernate.c3p0.max_statements", "50")
      .setProperty("hibernate.c3p0.debugUnreturnedConnectionStackTraces", "true")
      .setProperty("hibernate.c3p0.unreturnedConnectionTimeout", "20")
这是我的MySQL配置:

[mysqld]

## General
ignore-db-dir                        = lost+found
datadir                              = /var/lib/mysql
socket                               = /var/lib/mysql/mysql.sock
tmpdir                               = /var/lib/mysqltmp

## Cache
table-definition-cache               = 4096
table-open-cache                     = 4096
#table-open-cache-instances          = 1
#thread-cache-size                   = 16
#query-cache-size                    = 32M
#query-cache-type                    = 1

## Per-thread Buffers
#join-buffer-size                    = 512K
#read-buffer-size                    = 512K
#read-rnd-buffer-size                = 512K
#sort-buffer-size                    = 512K

## Temp Tables
#max-heap-table-size                 = 64M
#tmp-table-size                      = 32M

## Networking
#interactive-timeout                 = 3600
max-connections                      = 400
max-connect-errors                   = 1000000
max-allowed-packet                   = 16M
skip-name-resolve
wait-timeout                         = 600

## MyISAM
key-buffer-size                      = 32M
#myisam-recover                      = FORCE,BACKUP
myisam-sort-buffer-size              = 128M

## InnoDB
#innodb-buffer-pool-size             = 256M
innodb-file-format                   = Barracuda
#innodb-file-per-table               = 1
#innodb-flush-method                 = O_DIRECT
#innodb-log-file-size                = 128M

## Replication and PITR
#binlog-format                       = ROW
expire-logs-days                     = 7
#log-bin                             = /var/log/mysql/bin-log
#log-slave-updates                   = 1
#max-binlog-size                     = 128M
#read-only                           = 1
#relay-log                           = /var/log/mysql/relay-log
relay-log-space-limit                = 16G
server-id                            = 1

## Logging
#log-output                          = FILE
#log-slow-admin-statements
#log-slow-slave-statements
#log-warnings                        = 0
#long-query-time                     = 2
#slow-query-log                      = 1
#slow-query-log-file                 = /var/log/mysql/slow-log

[mysqld_safe]
log-error                            = /var/log/mysqld.log
#malloc-lib                          = /usr/lib64/libjemalloc.so.1
open-files-limit                     = 65535

[mysql]
no-auto-rehash

如果负载较低,您可以尝试使用
testConnectionOnCheckout
设置为true

必须在c3p0.properties中设置TestConnectionOnOnCheckout,c3p0默认值:false 如果设置为true,将在每次连接签出时执行操作,以验证连接是否有效

实际上很贵


更好的选择是使用c3p0.idleConnectionTestPeriod定期验证连接。尝试减小该值并检查其工作方式

您知道idelConnectionTestPeriod是否与hibernate.c3p0.idle\u test\u period相同。我将后者设置为1000