springboot-How to specify hikari connection pool in spring 1.x applications?

1. The purpose of this post

We know that springboot use the tomcat connection pool by default in spring boot 1.x applications. What should we do if we want to change to hikari connection pool? It’s very easy ,let’s start.

2. Environments

  • SpringBoot 1.x
  • Java 1.8+

3. The solution

3.1 Remove tomcat connection pool dependency from your POM.xml

If springboot find the tomcat connection pool jar file in the classpath, then it would use it as the default connection pool. We must remove it.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3.2 Change your application.properties like this

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.jdbcUrl=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver=com.mysql.jdbc.Driver

Pay attention to the spring.datasource.jdbcUrl, the original configuration is spring.datasource.url.

3.3 Write an aspect to verify that we use the hikariCP

Refer to this article to debug and view the internal connection pool that springboot uses.

3.4 Run the code

we should get this:

2019-05-12 16:12:08.357  INFO 14842 --- [nio-8080-exec-1] c.sb1jt.config.ConnectionAspect   : ds type:com.zaxxer.hikari.HikariDataSource

4. Conclusion

As you can see, change the default connection pool in spring boot 1.x is very easy. If you have any question,just contact me.