Solve the Link Close exception when hibernate uses the MySQL database

Source: Internet
Author: User

In a project, the customer requested to use all the Free Software except the operating system. Therefore, I used MySQL 4.0 as the database server, and its JDBC driver was version 3.0.9, after the client is installed, debugging is normal. However, the next day, the system prompts "No operations allowed after Connection closed" as soon as you log on, and the error is displayed in the browser. After some checks, I found that, in this case, it is only necessary to restart tomcat to restore normal, but the problem still exists the next day.

Find out on the internet that MySQL will automatically close the opened connection after eight hours of no use. The original Article 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 shoshould 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.

On the client's side, no one will use this system at night, which leads to this situation that was not taken into account in the system.

For this reason, I tried three methods: 1. Add autoreconnect = true to the Database URL; 2. Judge the session every time you call the getsession () method. whether isclosed () is true. If it is true, the session is called. reconnect (); 3. After two days, it turns out that the first two methods do not work. On this page, I found the third method, that is, the built-in connection pool of Hibernate is not used (hibernate strongly recommends not to use but I have been using it before). Instead, the connection pool uses the c3p0 connection pool, which will automatically handle the situation where the database connection is closed. To use c3p0 is very simple, first copy the c3p0-0.8.3.jar From Hibernate to the lib directory of the project, and then in hibernate. remove hibernate from properties. annotations of attributes starting with c3p0 (using the default value or the desired value), so that hibernate will automatically use c3p0 to replace the built-in connection pool. So far, the previous problems have not appeared.

In the past, it was not too important to warn hibernate not to use the built-in connection pool for product purposes. This is a lesson. Therefore, we should choose a more mature connection pool regardless of stability or performance.

Update: in addition to the connection pool, the previously written hibernatedao class also has a problem. In some cases, the session will be used repeatedly by multiple requests and has been corrected. Besides, isn't the name c3p0 the robot in Star Wars?

Related Article

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.