我间歇性地收到此异常,并且很难跟踪如何修复它。
此服务器上的负载非常低,应用程序与通过localhost(MySQL 5.6)连接的MySQL服务器位于同一主机上。对于' connect_timeout'我有合理的价值观。 (10)," interactive_timeout' (28800)和' wait_timeout' (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
答案 0 :(得分:0)
如果负载较低,您可以尝试使用testConnectionOnCheckout
为true
testConnectionOnCheckout必须在c3p0.properties中设置,C3P0默认值:false 如果设置为true,则将在每次连接检出时执行操作以验证连接是否有效。
实际上它很贵。
更好的选择是使用c3p0.idleConnectionTestPeriod定期验证连接。尝试降低价值并检查其工作原理