MySqlDBCP connection pool 8 hours failure solution

Recently, I have done a project of timed task type, and the access interval of this project to the database is relatively long. This will cause the test to be fine, and the problem will appear after it is put into production. Looking at the log, it turns out that the database connection pool timed out. I looked at some solutions online, but some of them didn't solve the problem. After comparing a few articles, after successful practice, I will share my connection pool configuration here.
 
I won't say more about other parameters, and I will explain several important parameters of connection pool failure.
 
1. The two parameters testOnReturn and testOnBorrow are true, which means that the connection pool test will be performed before and after each request. If the connection fails, the connection object will be destroyed and a new connection object will be created, but this time the database The operation also fails. There may be no problem with the read operation, just request it again, but it is not perfect for important write operations such as recharge and scheduled tasks. (PS: To use these two parameters, be sure to configure the validationQuery parameter so that it will take effect.)
 
2. In order to solve the above problems and ensure that each operation has a normal connection pool usage, let's take a look at the testWhileIdle parameter. When this parameter is true, it means that verification is performed when idle to check whether the object is valid. Then minEvictableIdleTimeMillis cooperates with timeBetweenEvictionRunsMillis to check the connection pool every timeBetweenEvictionRunsMillis seconds, destroy objects whose idle time exceeds minEvictableIdleTimeMillis seconds, and create new objects to replace them. This ensures that there are normal connection pool objects at all times.
 
<bean id="readDS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${slave.jdbc.driverClassName}"/>
        <property name="url" value="${slave.jdbc.url}"/>
        <property name="username" value="${slave.jdbc.username}"/>
        <property name="password" value="${slave.jdbc.password}"/>
        <property name="maxActive" value="300" />
        <property name="maxIdle" value="50" />
        <property name="minIdle" value="5" />
        <property name="maxWait" value="30000" />
        <property name="validationQuery" value="select current_date()" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="90" />
        <property name="logAbandoned" value="false" />
        <property name= "timeBetweenEvictionRunsMillis" value="30000" />  
        <property name= "numTestsPerEvictionRun" value="30" />  
        <property name="minEvictableIdleTimeMillis" value="1800000" />
</bean>
 

Guess you like

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