3

I have the below doubts on the Hikari or any database pooling concept.

  1. When does the connection pool gets created?
    Suppose if I have mentioned spring.datasource.hikari.maximum-pool-size=50
    Will it create 50 database instances?

  2. As by default, Spring scopes on classes in singleton, how 50 instances are created?

HoRn
  • 1,458
  • 5
  • 20
  • 25
murali
  • 49
  • 8

2 Answers2

4

1: A connection pool will be created when the first connection is creating. For example when the first SQL is executed. {@link com.zaxxer.hikari.HikariDataSource#getConnection()}.

This is my case and maybe it's different according the ORM you are using.

Connection instances will be created by the connection pool. {@link com.zaxxer.hikari.pool.HikariPool#poolEntryCreator} {@link com.zaxxer.hikari.pool.HikariPool#postFillPoolEntryCreator}

"spring.datasource.hikari.maximum-pool-size=50" means that the connection pool would not create connection instance more than 50. So it is the limit for connection instance count in connection pool.

2: Spring Bean is singleton by default. But connection instances in the conneciton pool are not "spring bean" and they are created from "new PoolEntry". {@link com.zaxxer.hikari.pool.HikariPool#createPoolEntry}

will
  • 61
  • 3
  • 2 point is exactly what I wanted to know, thank you! and does this mean, Hikari refers to the data source properties that we defined and creates connection-ready instances and when a call to a database is initiated (i.e for example repo.saveall(user)) this bean instance acquire one of the connection instance created and save the user to the db, is that right? – murali Aug 24 '22 at 09:23
  • 1
    I have no idea about "connection-ready instances", but you are probably right. The following is my opinion. The datasource properties are loaded to create Datasource instance while spring-boot starts. There isn't any connection at this moment. And then, when a call to database is initiated, the connection pool and first batch of connections are created. So that the SQL executor can acquire one of the connections to save the user to db. – will Aug 24 '22 at 12:13
  • @will I think at least one of the connections are created during startup because of database health check. – v.ladynev Sep 01 '22 at 12:08
  • @v.ladynev It didn't check database connection during the startup of my spring-boot application. Is there any default database connection check policy in spring boot? I didn't find that from google. Could you tell me some articles or documents? Thank you. – will Sep 02 '22 at 10:31
  • @will It will be added with `spring-boot-starter-actuator` – v.ladynev Sep 02 '22 at 14:50
1

Connection pooling helps reducing in creating database connections everytime a database call is encountered.

Pool would be nothing but set of connections (ideally active) (Like we have thread pool ), when requested will return one active connection ( or create one for first database request) , instance would be a misfit word here !

To answer first part of your question, it is initialized during application startup when that spring.datasource.hikari... property is encountered.

Below link explains this concept well

https://coderstea.in/post/best-practices/jdbc-connection-pooling-explained-with-hikaricp/

Harsh
  • 812
  • 1
  • 10
  • 23