2

I'm working on a Spring MVC project (Spring version 4.1.1), and I configured HikariCP as connection pool manager. When the application is not used for a while, it seems like Hikari starts losing connections with DB, and when I try to connect to the web app, Tomcat shows this:

com.zaxxer.hikari.pool.PoolBase - HikariPool-8 - Failed to validate connection Pooled connection wrapping physical connection org.postgresql.jdbc.PgConnection@2bb0433a (Connection has been closed.). Possibily consider using a shorter maxLifetime value. 

Here is my Hikari datasource configuration for Spring:

<!-- Initialize DataSource com.zaxxer.hikari.HikariDataSource --> <bean id="hikariConfig" class="com.zaxxer.hikari.HikariDataSource"> <property name="dataSourceClassName" value="org.postgresql.ds.PGPoolingDataSource" /> <property name="dataSourceProperties"> <props> <prop key="url">${jdbc.url}</prop> <prop key="user">${jdbc.username}</prop> <prop key="password">${jdbc.password}</prop> </props> </property> <property name="maxLifetime" value="120000" /> <property name="connectionTestQuery" value="SELECT 1" /> <property name="maximumPoolSize" value="10" /> </bean> <!-- HikariCP configuration --> <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> <constructor-arg ref="hikariConfig" /> </bean> 

I already tried to use shorter maxLifetime values, as suggested by the stacktrace, but it did not make any difference.

Could anybody please help me resolving this issue? What am I doing wrong?

Other useful info:

  • HikariCP version: 3.3.1
  • Java version: 1.8
  • Tomcat version: 8.5
  • Spring MVC version: 4.1.1
  • JDBC PostgreSQL driver version: 42.2.16
  • DBMS version: PostgreSQL 13.0
8
  • You probably don't close connection to datasource so pool becomes full, can you show/check your code always close connections and if not why? or it can be wait timeout, see stackoverflow.com/questions/60310858/… and dba.stackexchange.com/questions/164419/… Commented Mar 15, 2021 at 12:06
  • Thank you @user7294900 .I can ensure we always close connections. For every method accessing the db we have try { conn = dataSource.getConnection(); ... } finally { conn.close(); }. I also noticed that the idle_in_transaction_session_timeout is disabled in my DBMS. Maybe I need to set it with a value, such as 5 min? That is greater than the maxLifetime of the application (2min)? Commented Mar 15, 2021 at 15:09
  • That can be the issue, can you check? Commented Mar 15, 2021 at 15:30
  • @user7294900 Unfortunately, nothing changed. I found that idle_in_transaction_session_timeout only affects connections whose state is idle in transaction, but in my case I have a lot of idle connections (I am using pg_stat_activity to spot them). I also found a few posts that suggest to use a cron job to programmatically kill idle connections :/ (stackoverflow.com/questions/13236160/…, stackoverflow.com/questions/12391174/…). Do you think it is worth trying this? Commented Mar 16, 2021 at 9:36
  • I would investigate more if there's a leak, you can turn on hikari's leakDetectionThreshold or other tool Commented Mar 16, 2021 at 10:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.