Hibernate configures the c3p0 connection pool (LP)

Source: Internet
Author: User

An exception occurs when a newly developed website is opened every morning to access the database. The database is MySQL. That is to say, an exception occurs during a long idle period of time. The Exception Code is ** begin nested exception **

Com. MySQL. JDBC. communicationsexception
Message: communications link failure due to underlying exception:

** Begin nested exception **

Java.net. socketexception
Message: Software caused connection abort: Socket write error

Stacktrace:

Java.net. socketexception: Software caused connection abort: Socket write error
At java.net. socketoutputstream. socketwrite0 (native method)
At java.net. socketoutputstream. socketwrite (socketoutputstream. Java: 92)
At java.net. socketoutputstream. Write (socketoutputstream. Java: 136)
At java. Io. bufferedoutputstream. flushbuffer (bufferedoutputstream. Java: 65)
At java. Io. bufferedoutputstream. Flush (bufferedoutputstream. Java: 123)
At com. MySQL. JDBC. mysqlio. Send (mysqlio. Java: 2590)
At com. MySQL. JDBC. mysqlio. Send (mysqlio. Java: 2523)
At com. MySQL. JDBC. mysqlio. sendcommand (mysqlio. Java: 1517)
At com. MySQL. JDBC. mysqlio. sqlquerydirect (mysqlio. Java: 1626)
At com.mysql.jdbc.connection.exe csql (connection. Java: 3031)
At com.mysql.jdbc.preparedstatement.exe cuteinternal (preparedstatement. Java: 943)
At com.mysql.jdbc.preparedstatement.exe cutequery (preparedstatement. Java: 1049)
At org. hibernate. JDBC. abstractbatcher. getresultset (abstractbatcher. Java: 139)
At org. hibernate. loader. loader. getresultset (loader. Java: 1669)
At org. hibernate. loader. loader. docquery (loader. Java: 662)
At org. hibernate. loader. loader. doqueryandinitializenonlazycollections (loader. Java: 224)
At org. hibernate. loader. loader. dolist (loader. Java: 2145)
At org. hibernate. loader. loader. listignorequerycache (loader. Java: 2029)
At org. hibernate. loader. loader. List (loader. Java: 2024)
At org. hibernate. loader. hql. queryloader. List (fig. Java: 375)
At org. hibernate. hql. Ast. querytranslatorimpl. List (querytranslatorimpl. Java: 308)
At org. hibernate. Engine. query. hqlqueryplan. Sort MList (hqlqueryplan. Java: 153)
At org. hibernate. impl. sessionimpl. List (sessionimpl. Java: 1106)
At org. hibernate. impl. queryimpl. List (queryimpl. Java: 79)
At org. hibernate. hopedao. studentdbentity. findstudent (studentdbentity. Java: 75)
At com.hope.student.action.studentaction.exe cute (studentaction. Java: 483)
At org. Apache. Struts. Action. requestprocessor. processactionperform (requestprocessor. Java: 419)
At org. Apache. Struts. Action. requestprocessor. Process (requestprocessor. Java: 224)
At org. Apache. Struts. Action. actionservlet. Process (actionservlet. Java: 1194)
At org. Apache. Struts. Action. actionservlet. dopost (actionservlet. Java: 432)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 710)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 803)
At org. Apache. Catalina. Core. applicationfilterchain. internaldofilter (applicationfilterchain. Java: 290)
At org. Apache. Catalina. Core. applicationfilterchain. dofilter (applicationfilterchain. Java: 206)
At org. Apache. Catalina. Core. standardwrappervalve. Invoke (standardwrappervalve. Java: 230)
At org. Apache. Catalina. Core. standardcontextvalve. Invoke (standardcontextvalve. Java: 175)
At org. Apache. Catalina. Core. standardhostvalve. Invoke (standardhostvalve. Java: 128)
At org. Apache. Catalina. Valves. errorreportvalve. Invoke (errorreportvalve. Java: 104)
At org. Apache. Catalina. Core. standardenginevalve. Invoke (standardenginevalve. Java: 109)
At org. Apache. Catalina. connector. coyoteadapter. Service (coyoteadapter. Java: 261)
At org. Apache. Coyote. http11.http11processor. Process (http11processor. Java: 844)
At org. Apache. Coyote. http11.http11protocol $ http11connectionhandler. Process (http11protocol. Java: 581)
At org.apache.tomcat.util.net. jioendpoint $ worker. Run (jioendpoint. Java: 447)
At java. Lang. thread. Run (thread. Java: 595)

** End nested exception **

Last packet sent to the server was 1 MS ago.

Stacktrace:

Com. MySQL. JDBC. communicationsexception: communications link failure due to underlying exception:

I checked the MySQL documentation, the connector/J documentation, and the online description. The cause of this exception is that the default "wait_timeout" of the MySQL server is 8 hours, that is to say, if a connection is idle for more than 8 hours, MySQL will automatically disconnect the connection. This is the problem. If c3ons in c3p0 pools is idle for more than 8 hours, MySQL disconnects it, while c3p0 does not know that the connection has expired. If a client requests a connection, c3p0 provides the invalid connection to the client, which will cause the above exception. There are three solutions: 1. Increase the wait_timeout time. 2. Reduce the connection life time in connection pools. 3. test the validity of the connection in connection pools. Of course, the best way is to use the above three methods at the same time. Next we will explain how to use c3p0 for hibernate, assuming that wait_timeout is the default 8 hours. Hibernate uses the connection pool of c3p0 to configure c3p0. 1. Import the c3p0. jar package. You can also download it from the Internet. 2. hibernate configuration file. cfg. add <property name = "c3p0. acquire_increment "> 1 </property> <property name =" c3p0. idle_test_period "> 100 </property> <property name =" c3p0. max_size "> 5 </property> <property name =" c3p0. max_statements "> 0 </property> <property name =" c3p0. min_size "> 2 </property> <property name =" c3p0. timeout "> 90 </property> <property name =" c3p0. preferredtestquery "> select 1 from user where id = 1 </PR Operty> <property name = "c3p0. idleconnectiontestperiod "> 18000 </property> <property name =" c3p0. maxidletime "> 25000 </property> <property name =" c3p0. testconnectioncheckout "> true </property> 3. restart tomcat to solve the problem.

 

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.