Make sure that the DBCP connection pool does not time out

I deployed a project a few days ago, but every time I open it every time I open it, the following error will be reported:

 javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed:  
Baidu, it turns out that the database connection timed out. MySQL's default connection survival time is 28800 seconds, or 8 hours. If the database connection (java.sql.Connection) has been in a waiting state during the wait_timeout period, MySQL will close the connection. At this time, the DBCP database connection pool still legally holds the connection, and the above error is reported when the connection is used for database operations.
There are three solutions:
Method 1: Set a larger wait_timeout value. 
The maximum value of wait_timeout is 24 days/365 days respectively (windows/linux, you can configure wait_timeout to a larger value by modifying my.ini or my.cnf, which can temporarily solve this problem. But if the connection wait exceeds the configured time, There will still be this problem, and this method cannot fundamentally solve the problem.
Method 2: Find a way to configure the database connection of the application, add autoReconnect\=true to the url, but unfortunately it is invalid for versions above mysql5, and this method cannot fundamentally solve the problem.
Method 3: Work hard on the connection pool configuration file. 
BasicDataSource has testOnBorrow, testOnReturn, and testWhileIdle properties, which means whether to check the validity of the object when it is obtained, returned, and idle. The default is False. As long as both are set to True, and the validationQuery statement is provided, the database connection is guaranteed to be always valid.
testOnBorrow=true
testOnReturn=true
testWhileIdle=true
validationQuery=SELECT 1 FROM DUAL
Note: When using method 3 to solve the problem, if the jar package (class="org.apache.commons.dbcp.BasicDataSource") that dbcp depends on is configured in the configuration file, the following error will be reported:
Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Invalid property 'testOnBorrow' of bean class [org.apache.commons.dbcp.BasicDataSource]:
After checking the org.apache.commons.dbcp.BasicDataSource class in the code, there is no setTestOnBorrow method. When importing the package, I found that there is a dbcp package (org.apache.tomcat.dbcp.dbcp.BasicDataSource) that comes with tomcat. There is a method of setTestOnBorrow, which simply writes the dbcp package that comes with tomcat in the configuration file. After testing, it was found that no error was reported. And the database will not have a connection timeout error.
 
Reference: https://blog.csdn.net/hunger_wang/article/details/55224726

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325489088&siteId=291194637