Friday 27 September 2019

Spring boot: Hikari connection pool example


Hikari is a JDBC connection pool that is used by many applications.

How to configure hikari connection pool?

application.yml
spring:   
  datasource:
    communicationtimeout: 60000
    jpa:
      hibernate:
        ddl-auto: none
    hikari:
      connection-timeout: 250000
      idle-timeout: 300000
      max-lifetime: 1000000
      maximum-pool-size: 20
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    database-platform:  org.hibernate.dialect.H2Dialect 
---
spring:
  jpa:
    properties:
      hibernate:
        show_sql: true
        format_sql: false


Define DataSource.
@ConfigurationProperties(prefix = "spring.datasource.hikari")
@Bean
public DataSource dataSource() {

  HikariDataSource dataSource = new HikariDataSource();
  dataSource.setDriverClassName("org.h2.Driver");
  dataSource.setJdbcUrl("jdbc:h2:file:~/db/myOrg.db;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;");
  dataSource.setUsername("krishna");
  dataSource.setPassword("password123");
  return dataSource;
}

Find the below working example.

Employee.java
package com.sample.app.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "employees")
public class Employee {

  @Id
  @GeneratedValue
  private int id;

  private String firstName;

  private String lastName;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
  }

}


EmployeeRepository.java
package com.sample.app.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.sample.app.entity.Employee;

public interface EmployeeRepository extends JpaRepository<Employee, Integer>{

}


AppConfigurations.java
package com.sample.app.config;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
public class AppConfigurations {

    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    @Bean
    public DataSource dataSource() {

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setJdbcUrl("jdbc:h2:file:~/db/myOrg.db;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;");
        dataSource.setUsername("krishna");
        dataSource.setPassword("password123");
        return dataSource;
    }
}


App.java
package com.sample.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.sample.app.entity.Employee;
import com.sample.app.repository.EmployeeRepository;

@SpringBootApplication
public class App {
    @Autowired
    private EmployeeRepository empRepo;

    public static void main(String args[]) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    public CommandLineRunner demo() {
        return (args) -> {
            Employee emp1 = new Employee();
            emp1.setFirstName("Ram");
            emp1.setLastName("Ponnam");

            empRepo.save(emp1);

            empRepo.findAll().forEach(System.out::println);
        };
    }

}


application.yml
spring:   
  datasource:
    communicationtimeout: 60000
    jpa:
      hibernate:
        ddl-auto: none
    hikari:
      connection-timeout: 250000
      idle-timeout: 300000
      max-lifetime: 1000000
      maximum-pool-size: 20
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    database-platform:  org.hibernate.dialect.H2Dialect 
---
spring:
  jpa:
    properties:
      hibernate:
        show_sql: true
        format_sql: false


Total project structure looks like below.

Run App.java, you can see below messages in console.
Employee [id=1, firstName=Ram, lastName=Ponnam]

You can download complete working application from this link.

Previous                                                    Next                                                    Home

No comments:

Post a Comment