Today, we will mainly describe the actual solution to link Hibernate to the MySQL database to close exceptions. We hope that you will learn the actual solution to link Hibernate to the MySQL database to close exceptions, the following is a detailed description of the article.
In a project, the customer requested to use all the Free Software except the operating system. Therefore, I used MySQL 4.0 as the MySQL database server, and its JDBC driver is 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 url of the MySQL database.
2. When the getSession () method is called each time, the system checks whether session. isClosed () is true. If it is true, the system calls session. reconnect ();
3. After two days, I found the third method on this page, that is, if you do not use Hibernate's built-in connection pool Hibernate, it is strongly recommended that you do not use it but I have been using it before.) instead, you can use the C3P0 connection pool, which will automatically handle the case where the MySQL 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. comments of attributes starting with c3p0 use 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?