When a connection is created, the following error occurs: com. mysql. jdbc. exceptions. jdbc4.CommunicationsException: Unknown, 979millisecondsago. Thelastpacketsentsuccessfullytotheserverw.
An error occurred while calling to create a connection: com. mysql. jdbc. exceptions. jdbc4.CommunicationsException: Communications link failure The last packet successfully encoded ed from the server was 141,979 milliseconds ago. the last packet sent successfully to the server w
An error occurred while calling to create a connection:
Com. mysql. jdbc. exceptions. jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 141,979 milliseconds ago.The last packet sent successfully to the server was 1 milliseconds ago.
I have been searching for this problem for a long time, because the source code is not written by myself, and I am not very familiar with this item, because it is too urgent to find the reason. Apache commons. the database connection of the BasicDataSource In the dbcp package uses the connection pool. When you close the connection, you do not actually close the connection, but just recycle the connection to the connection pool, directly called when there are other requirements, but the default is not to check the validity of the data connection, some connections may have expired due to timeout and other reasons, so sometimes an invalid connection may be obtained during the call. Therefore, in the above case, modify the method: When initializing basicdatasource,
BasicDataSource ds = new BasicDataSource ();
...... // Other Initialization
Ds. setTestOnBorrow (true); // check the validity of the connection.
Ds. setTestOnReturn (true );
Ds. setTestWhileIdle (true );
Ds. setValidationQuery ("select 1 from dual"); // The method used to verify the connection validity. This step cannot save
Ps: however, this error has never occurred in windows. Why is it unclear. If you remotely access the database on a linux server in windows, an error may be returned. The error may be related to the database. However, the database version on linux is still high. Is it a connected jar file? But I don't know the reason why I say it is more than 5.0 common on the Internet.
There are also two parameters, timeBetweenEvictionRunsMIllis and minEvictableIdleTimeMillIs, they work together to continuously update the connection objects in the connection pool, when timeBetweenEvictionRunsMWhen illis is greater than 0, every timeBetweenEvictionRunsMThe illis time starts a thread to verify that the idle time in the connection pool exceeds minEvictableIdleTimeMill.Is connection object.
For detailed settings and operating principles, see grouping jdbc-pool for high-concurrency.