Spring Boot + HikariCP

spring-boot-starter-jdbc

spring-boot-starter-data-jpa

spring.datasource.type

HikariDataSource

application.properties

Technologies Used

HikariCP Dependency

<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.1.0</version> </dependency>

pom.xml

build.gradle

spring-boot-starter-jdbc

spring-boot-starter-data-jpa

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.0.3.RELEASE</version> </dependency>

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.0.3.RELEASE</version> </dependency>

pom.xml

build.gradle

HikariCP Configurations

spring.datasource.type

application.properties

spring.datasource.type = com.zaxxer.hikari.HikariDataSource

HikariDataSource

spring.datasource.hikari.*

application.properties

connectionTimeout

spring.datasource.hikari.connection-timeout=20000

minimumIdle

spring.datasource.hikari.minimum-idle=5

maximumPoolSize

spring.datasource.hikari.maximum-pool-size=12

idleTimeout

spring.datasource.hikari.idle-timeout=300000

maxLifetime

spring.datasource.hikari.max-lifetime=1200000

autoCommit

spring.datasource.hikari.auto-commit=true

Spring Boot Data + HikariCP + MySQL Example

CrudRepository

RestTemplate

CREATE TABLE `articles` ( `article_id` INT(5) NOT NULL AUTO_INCREMENT, `title` VARCHAR(200) NOT NULL, `category` VARCHAR(100) NOT NULL, PRIMARY KEY (`article_id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB;

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.concretepage</groupId> <artifactId>spring-boot-app</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-app</name> <description>Spring Boot Application</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> </parent> <properties> <java.version>9</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.5</version> </dependency> <!-- Spring Boot Data 2.0 includes HikariCP by default --> <!-- dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.1.0</version> </dependency --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.1.0</version> </dependency>

spring.datasource.url=jdbc:mysql://localhost:3306/concretepage spring.datasource.username=root spring.datasource.password=cp #Spring Boot 2.0 includes HikariDataSource by default #spring.datasource.type = com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.connection-timeout=20000 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.maximum-pool-size=12 spring.datasource.hikari.idle-timeout=300000 spring.datasource.hikari.max-lifetime=1200000 spring.datasource.hikari.auto-commit=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.jpa.properties.hibernate.id.new_generator_mappings=false spring.jpa.properties.hibernate.format_sql=true

spring.datasource.type

spring.datasource.type = com.zaxxer.hikari.HikariDataSource

package com.concretepage.repository; import org.springframework.data.repository.CrudRepository; import com.concretepage.entity.Article; public interface ArticleRepository extends CrudRepository<Article, Long> { }

package com.concretepage.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="articles") public class Article implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="article_id") private long articleId; @Column(name="title") private String title; @Column(name="category") private String category; public long getArticleId() { return articleId; } public void setArticleId(long articleId) { this.articleId = articleId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } }

package com.concretepage.service; import java.util.List; import com.concretepage.entity.Article; public interface IArticleService { List<Article> getAllArticles(); void addArticle(Article article); }

package com.concretepage.service; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.concretepage.entity.Article; import com.concretepage.repository.ArticleRepository; @Service public class ArticleService implements IArticleService { @Autowired private ArticleRepository articleRepository; @Override public List<Article> getAllArticles(){ List<Article> list = new ArrayList<>(); articleRepository.findAll().forEach(e -> list.add(e)); return list; } @Override public void addArticle(Article article){ articleRepository.save(article); } }

package com.concretepage.controller; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; public class ArticleInfo { @JsonInclude(Include.NON_NULL) private long articleId; @JsonInclude(Include.NON_NULL) private String title; @JsonInclude(Include.NON_NULL) private String category; public long getArticleId() { return articleId; } public void setArticleId(long articleId) { this.articleId = articleId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } }

package com.concretepage.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.util.UriComponentsBuilder; import com.concretepage.entity.Article; import com.concretepage.service.IArticleService; @RestController @RequestMapping("user") public class ArticleController { @Autowired private IArticleService articleService; //Fetches all articles @GetMapping(value= "articles") public ResponseEntity<List<ArticleInfo>> getAllArticles() { List<ArticleInfo> responseArticleList = new ArrayList<>(); List<Article> articleList = articleService.getAllArticles(); for (int i = 0; i < articleList.size(); i++) { ArticleInfo ob = new ArticleInfo(); BeanUtils.copyProperties(articleList.get(i), ob); responseArticleList.add(ob); } return new ResponseEntity<List<ArticleInfo>>(responseArticleList, HttpStatus.OK); } //Creates a new article @PostMapping(value= "article") public ResponseEntity<Void> addArticle(@RequestBody ArticleInfo articleInfo, UriComponentsBuilder builder) { Article article = new Article(); BeanUtils.copyProperties(articleInfo, article); articleService.addArticle(article); HttpHeaders headers = new HttpHeaders(); headers.setLocation(builder.path("/article/{id}").buildAndExpand(article.getArticleId()).toUri()); return new ResponseEntity<Void>(headers, HttpStatus.CREATED); } }

package com.concretepage; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootAppStarter implements CommandLineRunner { @Autowired DataSource dataSource; public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootAppStarter.class, args); } @Override public void run(String... args) throws Exception { System.out.println("DataSource = " + dataSource); } }

DataSource = HikariDataSource (HikariPool-1)

package com.concretepage.client; import java.net.URI; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import com.concretepage.entity.Article; public class RestClientUtil { public void getAllArticlesDemo() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:8080/user/articles"; HttpEntity<String> requestEntity = new HttpEntity<String>(headers); ResponseEntity<Article[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Article[].class); Article[] articles = responseEntity.getBody(); for(Article article : articles) { System.out.println("Id:"+article.getArticleId()+", Title:"+article.getTitle() +", Category: "+article.getCategory()); } } public void addArticleDemo() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:8080/user/article"; Article objArticle = new Article(); objArticle.setTitle("Spring REST Security using Hibernate"); objArticle.setCategory("Spring"); HttpEntity<Article> requestEntity = new HttpEntity<Article>(objArticle, headers); URI uri = restTemplate.postForLocation(url, requestEntity); System.out.println(uri.getPath()); } public static void main(String args[]) { RestClientUtil util = new RestClientUtil(); util.addArticleDemo(); util.getAllArticlesDemo(); } }

Id:1, Title:Spring REST Security using Hibernate, Category: Spring

Test Application

application.properties

mvn spring-boot:run

mvn clean eclipse:eclipse

SpringBootAppStarter

mvn clean package

java -jar target/spring-boot-app-0.0.1-SNAPSHOT.jar

RestClientUtil

References

Download Source Code

This page will walk through Spring Boot and HikariCP example. HikariCP is fast, simple, reliable and production ready JDBC connection pool. In Spring Boot 2.0 release, default database pooling technology has been switched from Tomcat Pool to HikariCP. This is because HikariCP offers superior performance. Now since Spring Boot 2.0 release,andresolve HikariCP dependency by default andproperty hasas default value. Spring boot prefers HikariCP on first place then Tomcat pooling and then Commons DBCP2 on the basis of availability. Here on this page we will provide complete example of HikariCP with Spring Boot Data and MySQL. We will create a demo application in which we will perform create and read operation in database. We will configure HikariCP properties for example connectionTimeout, minimumIdle, maximumPoolSize, idleTimeout, maxLifetime and autoCommit infile.Find the technologies being used in our example.1. Java 92. Spring 5.0.7.RELEASE3. Spring Boot 2.0.3.RELEASE4. Maven 3.5.25. MySQL 5.56. Eclipse OxygenBefore using HikariCP, we need to make sure that we have resolved the HikariCP dependency. If we are using Maven we can use following dependency.In case we are using Spring Boot 2.0 and onwards, weto include HikariCP dependency inorourselves becauseandresolve it by default. It means if we are using dependencies eitherOrThen weto include HikariCP dependency in ourorFor the Hikari connection pool configuration, we enable it by usingand assigning it fully qualified name of the connection pool implementation infile as following.If we are using Spring Boot 2.0 and onwards, Spring Boot selectsby default and weto configure above line.Now to configure Hikari specific connection pool settings, Spring Boot providesprefix to be used infile. We will discuss here some frequently used configurations.is the maximum number of milliseconds that a client will wait for a connection from connection pool. We need to configure it as following.is the minimum number of idle connections that is maintained by HikariCP in connection pool. It is configured as following.configures the maximum pool size. It is configured as following.is the maximum amount of time in milliseconds that a connection is allowed to sit idle in connection pool. It is configured as following.is the maximum life time in milliseconds of a connection in pool after it is closed. It is configured as following.An in-use connection will never be retired, only when it is closed will it then be removed after maximum lifetime.configures the default auto-commit behavior of connections returned from pool. Default value isWe will create a Spring Boot REST web service with Spring Boot Data, HikariCP and MySQL. We will useto query database. We will also create a REST client usingto test our application. First find the project structure of the demo application.Find the MySQL table structure used in our example.Find the Maven file to resolve dependencies.If we are using Spring Boot version below Spring Boot 2.0 then we need to include HikariCP dependency as following.HikariCP 3.1.0 is suitable for Java 8 and Java 9.Now find the property file to configure datasource and other properties. Connection pool will be configured using HikariCP.If we are using Spring Boot version below Spring Boot 2.0 then we need to includeproperty for HikariCP as following.Now find the other files used in the demo application.Now find the Main Java class to run the application. To ensure that we are using HikariCP, we are printing datasource name.When we start our application, we can see following message in console at the end of server log.Now find the REST client to test the application.When we run the client we will get following output.To test the application, first create table in MySQL as given in the article and configure your database credentials infile. Then we can run REST web service in following ways.: Download the project source code. Go to the root folder of the project using command prompt and run the command.Tomcat server will be started.: Download the project source code using the download link given at the end of article. Import the project into eclipse. Using command prompt, go to the root folder of the project and run.and then refresh the project in eclipse. Run Main classby clicking. Tomcat server will be started.: Using command prompt, go to the root folder of the project and run the command.We will get executable JARin target folder. Run this JAR asTomcat server will be started.Now we are ready to test the application. To run web service client, go to theclass in eclipse and click on1. Spring Boot Reference Guide 2. HikariCP 3. Spring Boot 2.0 Release Notes