我有一个应用程序设置为与BoneCP共享mysql连接。现在,这个应用程序并没有得到大量的使用,因此连接也不是那么频繁。经过一段时间后,曾经有效的查询开始失败,我收到类似如下的消息:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 2,618,063 milliseconds ago. The last packet sent successfully to the server was 44,734 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3567)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3456)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
at com.jolbox.bonecp.PreparedStatementHandle.execute(PreparedStatementHandle.java:138)我是这样设置BoneCP的:
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:mysql://" + hostname + "/" + database);
config.setUsername(username);
config.setPassword(password);
config.setMinConnectionsPerPartition(minPoolSize);
config.setMaxConnectionsPerPartition(maxPoolSize);
config.setIdleConnectionTestPeriodInMinutes(60);
config.setIdleMaxAgeInMinutes(240);
config.setPartitionCount(1);
connectionPool = new BoneCP(config);我不确定如何找出mysql服务器的超时时间(无论默认值是什么,它都没有改变),但在大约5到10分钟没有连接池活动后,我得到了这个错误,这似乎非常短。
发布于 2012-08-14 13:23:13
要么必须配置interactive_timeout空闲超时(设置mysql=X/mysql= X),要么可以配置连接池发出keep-alive语句:
config.setIdleConnectionTestPeriodInMinutes(10);
config.setConnectionTestStatement("/* ping */ SELECT 1"):https://stackoverflow.com/questions/11945833
复制相似问题