# Spring Boot Using Hikari Connection Pool
Connection Pool
In software engineering, a connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database. Opening and maintaining a database connection for each user, especially requests made to a dynamic database-driven website application, is costly and wastes resources. In connection pooling, after a connection is created, it is placed in the pool and it is used again so that a new connection does not have to be established. If all the connections are being used, a new connection is made and is added to the pool. Connection pooling also cuts down on the amount of time a user must wait to establish a connection to the database. Wikipedia
I’m Googling for connection pool java implementation. HikariCP is good choice for your connection pooling. how do i implementation HikariCp integrated with spring boot application.
Let’s check Step by Step :
Create Spring Project
Add dependency :
– spring-boot-starter-web
– spring-boot-starter-data-jpa
– mysql-connector-java
– lombok
Add Dependency HikariCP
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.2</version>
</dependency>
Exclude default Spring Pooling
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
Add the Properties
application.yml
spring:
datasource:
url: jdbc:mysql://localhost/training_hikaricp
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
# Hikari Properties
hikari:
idle-timeout: 10000
maximum-pool-size: 50
minimum-idle: 20
pool-name: HikariPool
jpa:
database: mysql
hibernate:
ddl-auto: update
# show-sql: true
jackson:
serialization:
indent-output: true
#Set Logging hikari to DEBUG mode
logging:
level:
com.zaxxer.hikari: debug
# org.hibernate: off
Notes :
– Create database training_hikaricp
– Edit username and password mysql. (if difference)
For Complete Java Code you cant download or clone from my Github
Run Project
mvn spring-boot:run
How do i test ?
- Insert 1000 data data-product.sql
- Run JMeter Thread Group.jmx
Test Result
- Thread Group Setting ( 5000 request in 10 seconds)
- HTTP Request
- HTTP Request
- Graph Response Time
- HikariCP create Connection max 50
- Log create connection and close the connection when idle
- HikariCP create Connection max 50
Check the full code on my github
Happy Coding!