Solve the Problem of connection failure of C3P0 to MySQL,

Source: Internet
Author: User

Solve the Problem of connection failure of C3P0 to MySQL,

Yesterday, I mounted a small project to the server and tested that the project can run. Open the project the next day. Once you open the page on which you want to use the database, the page will go down. Jdbc exception is reported, and a message is prompted to modify the wait_timeout field. After searching online, the wait_timeout variable is used to manage the MySQL link validity period. You can use the following statement to find out the MySQL link timeout time.

  

Here, wait_timeout is 28800 seconds, that is, 8 hours.

As a connection pool, C3P0 manages links as its biggest task. However, it does not know whether the link is valid, when the creation time of a MySQL link exceeds wait_timeout, the link is no longer in use. While C3P0 does not know, it is equivalent to C3P0 holding a link that cannot be used.

There are two ways to solve this problem.

  1. Extended MySQL wait_timeout

2. Let C3P0 actively test whether the held link is valid

Considering the actual application, the first approach is actually not very good, because C3P0, as a connection pool, should manage everything related to the link, so it is more appropriate to start with the second approach.

Regarding the link failure issue, the official documents of C3P0 provide this solution.

  

Begin by settingTestConnectionOnCheckoutToTrueAnd get your application to run correctly and stably. If you are happy with your application's performance,You can stop here!This is the simplest, most reliable form of Connection-testing, but it does have a client-visible performance cost.

If you 'd like to improve performance by eliminating Connection testing from clients 'Code path:

SetTestConnectionOnCheckoutToFalse

SetTestConnectionOnCheckinToTrue

SetIdleConnectionTestPeriodTo30, Fire up you application and observe. this is a pretty robust setting, all Connections will tested on check-in and every 30 seconds thereafter while in the pool. your application shocould experience broken or stale Connections only very rarely, and the pool shocould recover from a database shutdown and restart quickly. but there is some overhead associated with all that Connection testing.

Simply put, when you configure C3P0, if you want to prevent link failure, you can configure a testConnectionOnCheckout attribute, which will greatly affect the performance.

If you care about performance, set testConnectionOnCheckout to false, testConnectionOnCheckin to true, and idleConnectionTestPeriod to 30 seconds.

Configure data sources in spring

  

Properties File

  

Start the project. The output log shows that the connection pool automatically tests the connection every 30 seconds.

  

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.