Handling exceptions when hibernate links to the MySQL database

Source: Internet
Author: User

Running Environment: Tomcat + MySql
The project uses the data layer combination of hibernate3.0 C3PO MySQL, and the development and deployment are smooth. However, every morning when an application is accessed, the cocould not open hibernate session for transaction exception is thrown, caused by: COM. mySQL. JDBC. communicationsexception: communications link failure due to underlying exception, but after several accesses, it returns to normal.
It turns out that MySQL will automatically close the opened connection after eight hours of no use. The original excerpt is as follows:

5.4.

I have a Servlet/application that works fine for a day, and then stops working overnight

MySQL closes connections after 8 hours of inactivity. you either need to use a connection pool that handles stale connections or use the "autoreconnect" parameter (see "developing applications with MySQL connector/J ").

also, you shocould be catching sqlexceptions in your application and dealing with them, rather than propagating them all the way until your application exits, this is just good programming practice. mySQL connector/J will set the sqlstate (see Java. SQL. sqlexception. getsqlstate () in your apidocs) to "08s01" when it encounters network-connectivity issues during the processing of a query. your application code shocould then attempt to re-connect to MySQL at this point.

Through the above information collection, we can see the problem (MySQL closes the idle link after 28800 seconds, that is, 8 hours, while c3p0 disconnects the link after 6000000 seconds, the connection maintained by C3PO may have been shut down by MySQL. Naturally, Hibernate cannot open the session and errors occur early the next day.) to verify my ideas, I decided to reproduce this error on the development machine. First, in the MySQL configuration file my. INI added wait_timeout = 30 so that MySQL would turn off the link after 30, restart the application, the first access was successful, and the same error occurred after one minute of access, indicating that the problem is correctly diagnosed, modify the configuration as follows:
<Property name = "maxidletime">
<Value> 28000 </value>
</Property>
<Property name = "idleconnectiontestperiod">
<Value> 28000 </value>
</Property>

Configuration explanation: 28000 <28800 enable c3p0 to close its own link before MySQL is disconnected. Configure the idleconnectiontestperiod parameter to enable C3PO to check whether existing connections are available at intervals of 28000, in this way, we should ensure that all the connections we get are available. If we are not at ease, we can add the testconnectioncheckout parameter to check whether the connection can be used whenever we pull out the connection. This may cause MySQL to have a certain performance sacrifice;
<Property name = "testconnectiononcheckout">
<Value> true </value>
</Property>

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.