Druid connection pool leak caused by blood. Druid connection pool leak caused by blood.

The exception log is as follows:

ERROR - com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 60000, active 50, maxActive 50, creating 0
    at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1512)
    at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:1255)
    at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:5007)
    at com.alibaba.druid.filter.stat.StatFilter.dataSource_getConnection(StatFilter.java:680)
    at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:5003)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1233)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1225)
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:90)Copy the code

The number of connections in the connection pool has reached its maximum value. Failed to create a connection after 60 seconds.

The problem was obvious. The application must have used a connection somewhere but did not release it. The global troubleshooting code was difficult.

Add the following configuration:

<! <property name="removeAbandoned" value="true" /> <! - timeout, unit: ms - > < property name = "removeAbandonedTimeoutMillis" value = "600000" / > <! --> <property name="logAbandoned" value="true" />Copy the code

This is connection leak configuration items, if the connection for a long time are not returned to removeAbandoned open, than removeAbandonedTimeoutMillis set timeout, compulsory recycling will be connected.

When the connection pool is initialized, a thread is started to check and reclaim connections.

Reference source:

com.alibaba.druid.pool.DruidDataSource#createAndStartDestroyThread

When logAbandoned is open, closing the connection generates a stack log that can be used to determine which code opened the connection but didn’t close it.

abandon connection, owner thread: https-jsse-nio-4443-exec-9, connected at : In 1573521883837, open stackTrace at java.lang.Thread.getStackTrace(Thread.java:1589) at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:1305) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4619) at com.alibaba.druid.filter.stat.StatFilter.dataSource_getConnection(StatFilter.java:680) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4615) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1225) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1217) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:90) at org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource.getConnection(AbstractRoutingDataSource.java:162) .Copy the code

This configuration item can be used to check connection pool release problems, but use this item with caution in the production environment. If some services take a long time to execute transactions, they will be reclaimed by mistake, which may cause another fatal accident.

Read more on my blog:

1.Java JVM, Collections, Multithreading, new features series tutorials

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Java, backend, architecture, Alibaba and other big factory latest interview questions

Life is good. See you tomorrow