SpringBoot and HikariCP relationship - spring-boot

SpringBoot already is managing dataConnection then why is Hikari CP needed?
I have just started using SpringBoot so do not know much about SpringBoot and Hikari relation, although i read about Hikari but couldn't find any explicit explanation about its relationship with Springboot in presence of Spring data connection.
I read that Hikari is used when we need heavy db operations with lots of connections, if it is true then should we not use Hikari in follwoing scenario?
Scenario:
There is a small application, having maximum 8-10 REST calls once in a month or maximum fortnightly.That application needs to perform some probability and statistics related calculation.
Users login on that app at a time are of maximum 2-3 in numbers.
Do we still need to use Hikari?

There are two ways to communicate with the database from your application. You can either open a new DB connection any time you wish execute some query there, or you have a connection pool. Connection pool is a collection of reusable connections that application uses for DB communication. As establishing a new connection is relatively expensive operation, using connection pool gives you a significant performance improvement.
HikariCP is one of the connection pools libraries available in java and SpringBoot uses it as a default. As you don't need to do anything special to have it in your application, just enjoy your free lunch :)

HikariCP is used as the default connection pool in SpringBoot2, it was TomcatJDBC in SpringBoot 1. You must be using it as a default in your settings. You can overwrite it by setting another connection pool in your setting properties if you need. Please find more details about the connection pools and the default configurations of Spring Boot versions here.

Hikari is the default DataSource implementation with Spring Boot 2. This means we need not add explicit dependency in the pom.xml. The spring-boot-starter-JDBC and spring-boot-starter-data-JPA resolve it by default. To sum up, you require no other steps with Spring Boot 2.
Compared to other implementations, it promises to be lightweight and better performing.
Tuning Hikari Configuration Parameters:
spring.datasource.hikari.connection-timeout = 20000 #maximum number
of milliseconds that a client will wait for a connection
spring.datasource.hikari.minimum-idle= 10 #minimum number of idle
connections maintained by HikariCP in a connection pool
spring.datasource.hikari.maximum-pool-size= 10 #maximum pool size
spring.datasource.hikari.idle-timeout=10000 #maximum idle time for
connection
spring.datasource.hikari.max-lifetime= 1000 # maximum lifetime in
milliseconds of a connection in the pool after it is closed.
spring.datasource.hikari.auto-commit =true #default auto-commit
behavior.
HikariCP is a reliable, high-performance JDBC connection pool. It is much faster, lightweight, and has better performance as compared to other connection pool APIs. Because of all these compelling reasons, HikariCP is now the default pool implementation in Spring Boot 2. In this article, we will have a closer look to configure Hikari with Spring Boot.

Related

AWS RDS Connection and spring boot, JPA

I connect my DB (AWS RDS) to Spring boot JPA, Then my number of connections increases dramatically.
it is 12 now, I think it works spring boot 5 + browser 5, workbench 1 +, and others?
How can I reduce my number of connections? How can I maintain this connection safely?
You should be looking for Database connection pooling.
Database connection pooling is a method used to keep database connections open so they can be reused and also it will be keeping the total number of connections within a limit that we are specifying.
The default connection pool in Spring Boot is HikariCP, all you have to do is configure it properly
Sample connection pool configuration,
spring.datasource.hikari.connection-timeout = 20000
spring.datasource.hikari.minimum-idle= 10
spring.datasource.hikari.maximum-pool-size= 10
spring.datasource.hikari.idle-timeout=10000
spring.datasource.hikari.max-lifetime= 1000
spring.datasource.hikari.auto-commit =true

Difference between Redis max-wait and timeout

I can see two properties in my spring boot application. Which uses Redis for session storage
spring.redis.jedis.pool.max-wait
spring.redis.timeout
What is the difference between these two?
From the Spring Boot reference documentation:
spring.redis.jedis.pool.max-wait: Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted.
spring.redis.timeout: Read timeout.
Since you mention session storage, you may also want to be aware of spring.session.timeout which configures the session timeout.

why is spring boot opening a lot of connections?

I'm using a cloud database at elephantsql.com. As I'm using a free plan I can have only 5 concurrent connections.
I have a spring boot application that connects with this cloud database. The problem is that even when there is just one user at the website the spring boot application opens a lot of connections and some times it exceed the 5 connections.
I checked the connections at elephantsql.com and got this:
Its seems that the following unnecessary query is increasing the amount of connections...
SET application_name = 'PostgreSQL JDBC Driver'
How could I fix this to avoid the application to open unnecessary connections?
The setting you're looking for is probably
spring.datasource.max-active=5
Default value of this property is 10.

Hikari and test on borrow option

I use spring boot 2 with Hikari connection pool, jpa and postgres.
Is there any reason to continue to use theses options
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.testWhileIdle
No, They are unknown properties to Hikari connection pool so no need ,
They exists only in Tomcat JDBC Connection Pool (used in old Spring boot) which you aren't using anymore.
Explanation of different between properties
DBCP testOnBorrow=false rollbackOnReturn=false
enableAutoCommitOnReturn=false
Issues:
testOnBorrow=false increases the likelihood of broken connections
given to your application rollbackOnReturn=false +
enableAutoCommitOnReturn=false, like the C3P0 "remediation" above can
bleed transactions between consumers or cause locks to be held for
extended periods
HikariCP Differentiators
Tests connections with isValid() before returning them from the pool,
with an optimization that bypasses the check if the connection was
utilized within the last 1000ms Tracks connection state (and
transaction state), and performs rollback() only the the case of a
non-autocommit connections with uncommitted changes

Should Hystrix replace existing JDBC/HTTP connection pools, or delegate to them?

Many applications use connection pools for both HTTP and JDBC calls for resiliency. But using and configuring these 2 types of pools is very different. This duplicates the complexity of implementing resiliency patterns that are common to both - such as timeouts, retries, caching / alerting fallbacks, circuit breaking, and monitoring.
To my mind Hystrix offers common approaches of configuring and implementing these same resiliency patterns for both HTTP and JDBC calls.
My questions are:
Could Hystrix theoretically replace existing HTTP and JDBC
connection pools entirely?
If so, what are the pros and cons of doing so?
Replacing them entirely reduces the world of complexity that surrounds these connection pools - with their attendant timeout and validation query properties etc. However I am hazy about how Hystrix could "keep alive" JDBC / HTTP connections - and therefore avoid expensive connection setup costs - without delegating to existing libraries specialized for these tasks.
For context I have a DropWizard app, which uses Tomcat DBCP for its JDBC connection pool and Apache HttpClient for its HTTP connection pools.
No, Hystrix can not replace your connection pools.
Hystrix' main features are:
Limiting the number of calls to a service by using a limited thread pool or semaphores.
The possibility to time out calls to a service to avoid application threads being locked up waiting for slow/hung services.
Adding bulkheads so that one slow service minimally affects the rest of the application.
Circuit breaking slow/hung services.
The is no support for pooling connections.
I guess you can argue that the first point is somewhat related to a connection pool in that both Hystrix and a connection pool can limit the load against an other system. However, the main reason to have a connection pool is the performance gain of pooling connection. This load-limiting behavior is basically a bonus of connection pooling.
Hystrix could however compliment connection pools by providing the fail-fast timeout behavior and bulkheads if added in front of your connection pools, as you suggest in your question.

Resources