Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot application performs extremely high number of SET application_name queries to postgres

I have a Spring Boot application (v2.2.5.RELEASE) that uses JPA (Hibernate) to connect to a Postgres DB. Spring Boot uses HikariCP for connection pooling. In my production environment, I see the following query executed every few seconds regardless of DB activity (almost as if they are some kind of health check?):

SET application_name = 'PostgreSQL JDBC Driver'

ps: the CPU Utilisation % goes above 350% after 3 hours of starting application in server

Are these queries necessary? Can they be avoided?

configuration of HikariCP in application.properties

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
# Hikari will use the above plus the following to setup connection pooling
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=10
spring.datasource.hikari.idleTimeout=300
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.tomcat.maxIdle=150

spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider


spring.datasource.hikari.connectionTimeout=30000 
spring.datasource.hikari.maxLifetime=30000
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
like image 561
mohamed amine salah Avatar asked Sep 08 '25 07:09

mohamed amine salah


1 Answers

This SQL statement is run whenever the JDBC driver establishes a connection to PostgreSQL. If you see that statement very often, that means that you open (and hopefully close) lots of database connections, that is, you open a database connection for each request.

Opening database connections is very expensive. If my guess is right, you should use a connection pool and persistent, long-lived database connections. That will reduce the load and improve throughput.

like image 56
Laurenz Albe Avatar answered Sep 10 '25 12:09

Laurenz Albe