Connection pooling is a technique used to improve performance in applications with dynamic database driven content. Opening and closing database connections may not seem like a costly expense but it can add up rather quickly. Connection pools are essentially a cache of open database connections. HikariCP is solid high-performance light-weight JDBC connection pool. HikariCP comes with the support for all versions of JVM. Its just that we need to include the required dependency.
HakariCP has set a benchmark in its performance when compared to other connection pooling frameworks such as c3p0,dbcp2,tomcat and vibur. The framework is very so fast because of the main reasons such as byte-code-level engineering, micro optimizations and most importantly, intelligent use of collection framework .
Coming to its usage in spring boot application, HikariCP internally uses tomcat-jdbc pooling by default. And most importantly, spring boot application comes with a class called DataSourceConfiguration.class which is a conditional class. This class will be defined in org.springframework.boot.autoconfigure.jdbc and will be like this.
@ConditionalOnClass({HikariDataSource.class})
@ConditionalOnProperty(
name = {"spring.datasource.type"},
havingValue = "com.zaxxer.hikari.HikariDataSource",
matchIfMissing = true
)
static class Hikari extends DataSourceConfiguration {
Hikari() {
}
@Bean
@ConfigurationProperties(
prefix = "spring.datasource.hikari"
)
public HikariDataSource dataSource(DataSourceProperties properties) {
return (HikariDataSource)this.createDataSource(properties, HikariDataSource.class);
}
}
This will autoconfigure the datasource if this class exists on the classpath. Hence, we do not require to configure the datasource manually. We just need to set the driver class name,database url and credentials as shown below in the properties file (application.properties which will be available by default if created using spring initializer site). Note that spring.datasource.hikari is default prefix stated in the DataSourceConfiguration.class for describing the properties for hikaricp.
spring.datasource.url: jdbc:mysql://localhost:3306/example_db
spring.datasource.username: root
spring.datasource.password: password
spring.datasource.driverClassName: com.mysql.jdbc.Driver
And if we want to use jpa, we need to configure it as well in the file as shown below:
spring.jpa.hibernate.ddl-auto: create-drop
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
The HikariCP documentation tells us that we can change the connection timeout property .So, in our application.properties we can simply use
spring.datasource.hikari.connection-timeout: 60000
Now, for the entity classes which we want to create table in the database, we can define as usual.
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JsonProperty("user_name")
@NotBlank
private String userName;
@JsonProperty("last_name")
@NotBlank
private String lastName;
..... and so on
We can then use the rest of the architectural elements of REST as required for our application.