Apache BasicDataSource database connection problems

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.