4

I'm using version: 2.1.6.RELEASE form Spring Boot in my pom.xml-dependencies. To connect to my database I put following in application.properties:

spring.datasource.url= jdbc:postgresql://
spring.datasource.username=
spring.datasource.password=

When checking the amount of connections in postgresql with:

SELECT * FROM pg_stat_activity;

I see each time I start the application exactly 10 connections are made. They almost all have the same query:

SET application_name = 'PostgreSQL JDBC Driver'

Is there a way to prevent the application of making that many connections? Should I make my own pool-configuration? What resource in my Java-application does initialize these connections?

The only thing I can think of is that I create EntityManager(s) with the @Autowired annotation, EntityManager from:

javax.persistence.EntityManager;

But I read you should only close the connection this makes when using an EntityManagerFactory. The annotation should close the connection.

If you would require more info, I could edit my post

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
SimonartM
  • 668
  • 2
  • 11
  • 26

3 Answers3

7

HikariCP opens 10 idle connections by default,

Default: 10

You can override minimumIdle which is less recommended

HikariCP will make a best effort to add additional connections quickly and efficiently. However, for maximum performance and responsiveness to spike demands, we recommend not setting this value and instead allowing HikariCP to act as a fixed size connection pool. Default: same as maximumPoolSize

Or maximumPoolSize, but it effect the maximum size of connection pool

This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections.

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
5

That's no a connection leak but the desired behavior. Spring Boot Data uses HikariCP as a connection pool.

You can configure the max pool size as property. For example:

spring.datasource.hikari.maximum-pool-size=5
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
1

HikariCP has a maximum of 10 connections by default.

You can configure the maximum pool size like so:

spring.datasource.hikari.maximum-pool-size=5

To enable tracing logs:

logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
logging.level.com.zaxxer.hikari=TRACE
Carl Düvel
  • 1,029
  • 9
  • 17
Amit Nayak
  • 41
  • 2