The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received

----The source cannot be found----

2015-11-25 11:30:58,258 [Druid-ConnectionPool-Create-519227234] ERROR com.alibaba.druid.pool.DruidDataSource- create connection error, url: jdbc:mysql://xxxx:3306/xxxx?useUnicode=true&characterEncoding=utf-8

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

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

        at sun.reflect.GeneratedConstructorAccessor161.newInstance(Unknown Source)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)

        at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)

        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)

        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)

        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)

        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)

        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)

        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)

        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)

        at sun.reflect.GeneratedConstructorAccessor159.newInstance(Unknown Source)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)

        at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)

        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)

        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)

        at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1375)

        at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1431)

        at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:1849)

Caused by: java.net.ConnectException: Connection refused

        at java.net.PlainSocketImpl.socketConnect(Native Method)

        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)

        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)

        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)

        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)

        at java.net.Socket.connect(Socket.java:579)

        at java.net.Socket.connect(Socket.java:528)

        at java.net.Socket.<init>(Socket.java:425)

        at java.net.Socket.<init>(Socket.java:241)

        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)

        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)

 

 

上述问题是由mysql5数据库的配置引起的。mysql5将其连接的等待时间(wait_timeout)缺省为8小时。在其客户程序中可以这样来查看其值: 

mysql﹥ 

mysql﹥ show global variables like 'wait_timeout'; 

+---------------+---------+ 

| Variable_name | Value | 

+---------------+---------+ 

| wait_timeout | 28800 | 

+---------------+---------+ 

1 row in set (0.00 sec) 

28800 seconds, or 8 hours.

 

If during wait_timeout seconds, the database connection (java.sql.Connection) has been in a waiting state, mysql5 will close the connection. At this point, your Java application's connection pool still legally holds a reference to that connection. When using this connection for database operations, the above error is encountered. This explains the problem why my program cannot log in the next day. 

 

You may think that there is a way to solve it in the data source configuration of tomcat? Indeed, in the configuration of the jdbc connection url, you can attach "autoReconnect=true", but this only works for versions before mysql5. Adding "validation query" doesn't seem to help either. 

I think the easiest way is to prescribe the right medicine: since the problem is caused by the default value of the global variable wait_timeout of mysql5 being too small, we can make it bigger. 

Check the manual of mysql5 and find that the maximum value of wait_timeout is 24 days/365 days (windows/linux). Taking windows as an example, assuming we want to set it to 21 days, we only need to modify the mysql5 configuration file "my.ini" (mysql5 installation dir) and add a line: wait_timeout=1814400

mysql5 needs to be restarted. 

Linux system configuration file: /etc/my.cnf 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325003415&siteId=291194637