Finding cause of orphaned database connections between tomcat with hikari pool and PostgreSQL server

385 Views Asked by At

I have many unclosed connections between my tomcat server and the postgresql server. I'm using a hikari pool to manage the connections. I know that I can configure hikari to take care of orphaned connections and also postgres is able to close them after a certain configurable time of beeing idle. But all this is only good to work agains the symptoms instead of solving the actual problem.

An idea I had was to create a memory dump from tomcat and to follow the PgConnection classes to Spring, Tomcat until I find the right DataAccessObject. But the amount of classes and layers that are involved because of tomcat, spring, hikari is huge.

Question: Is it possible to analyze the stack trace from our business code to the open PgConnection objects in MAT for example and to search for a pattern? For example: "Follow the references from PgConnection until you find a class where the package name starts with "com.blabla.dao.MyDAO".

Or do you have a better idea how to achive this?

Many thanks.

1

There are 1 best solutions below

1
On

You can configure hikari to detect and log connection leaks by setting leakDetectionThreshold property. The value is in milliseconds and should be set to a reasonable number to not trigger false positives.

The other option is to configure how long an idle can stay. Note, that a DB connection is a valid state, as you use connection pool. By default hikari will keep all configured connections alive. This can be changed with minimumIdle property.

See: https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby