murali
murali

Reputation: 59

When does a Hikari pool connections work? - Spring/Spring Boot

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?

Upvotes: 4

Views: 3472

Answers (2)

will
will

Reputation: 61

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}

Upvotes: 4

Harsh
Harsh

Reputation: 972

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/

Upvotes: 1

Related Questions