Spring Boot autoconfig is still taking Tomcat Datasource instead HikariCP?

14,676

Solution 1

Edit: This answer gives the way to configure Hikari in standard Spring, also valid for Boot. However, as Boot has added greater integration for Hikari since I wrote it, answers like this could be valid and more suitable for Boot.

Just provide the datasource in a @Configuration class:

@Configuration
public class HikariCPConfig {

    @Value("${hikari.driverclassname}")
    private String driverClassName;

    @Value("${hikari.jdbc.url}")
    private String jdbcUrl;

    @Value("${hikari.username}")
    private String userName;

    @Value("${hikari.password}")
    private String password;

    @Value("${hikari.pool.size}")
    private int poolSize;

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        final HikariDataSource ds = new HikariDataSource();
        ds.setMaximumPoolSize(poolSize);
        ds.setDriverClassName(driverClassName);
        ds.setJdbcUrl(jdbcUrl);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }

}

Then, in the application.properties file, declare your properties for the datasource, which can be provided by the maven build:

#Hikari
hikari.driverclassname = com.mysql.jdbc.Driver
hikari.jdbc.url = jdbc:mysql://localhost:3306/my_db
hikari.username = ${db.username}
hikari.password = ${db.password}
hikari.pool.size = 5

Then you'll need Spring to scan your HikariCPConfig class, but as you're using @SpringBootApplication which is equivalent to @Configuration @EnableAutoConfiguration and @ComponentScan it shouldn't be a problem.

See also:

Solution 2

  • Include Hikari in your pom
  • Add the following property to your application.yml

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

Solution 3

spring.datasouce.type starts with spring boot 1.3.0, so it won't work on 1.2.5

Share:
14,676
jscherman
Author by

jscherman

Updated on June 19, 2022

Comments

  • jscherman
    jscherman almost 2 years

    I have my Spring Boot 1.2.5.RELEASE service and I want to use the HikariCP datasource instead of the default tomcat-jdbc. So, according to this Spring Boot Reference I understand I just have to exclude tomcat-jdbc from the classpath and add HikariCP.

    So this is my pom.xml:

    ...
    <dependencyManagement>
        ...
        <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                    <version>${spring-boot.version}</version>
                    <exclusions>
                        <exclusion>
                            <groupId>org.apache.tomcat</groupId>
                            <artifactId>tomcat-jdbc</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
    </dependencies>
    ...
    

    maven dependency tree:

    [INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ myproject-user-manage-webservice ---
    [INFO] com.mybusiness.myproject:myproject-user-manage-webservice:jar:0.0.1-SNAPSHOT
    [INFO] +- com.mybusiness.myproject:myproject-commons:jar:0.0.1-SNAPSHOT:compile
    [INFO] |  \- com.mybusiness.myproject:myproject-core:jar:0.0.1-SNAPSHOT:compile
    [INFO] |     \- com.mybusiness.myproject:myproject-core-commons:jar:0.0.1-SNAPSHOT:compile
    [INFO] +- com.mybusiness.myproject:myproject-api:jar:0.0.1-SNAPSHOT:compile
    [INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.4:compile (version managed from 2.4.6)
    [INFO] |  \- com.mybusiness.myproject:myproject-framework:jar:0.0.1-SNAPSHOT:compile
    [INFO] |     +- org.springframework.boot:spring-boot-starter-security:jar:1.2.1.RELEASE:compile (version managed from 1.2.5.RELEASE)
    [INFO] |     +- mysql:mysql-connector-java:jar:5.1.34:compile (version managed from 5.1.35)
    [INFO] |     +- com.mybusiness.framework:despegar-fwk-logging:jar:1.4.148:compile
    [INFO] |     +- org.jenkins-ci.plugins:testInProgress-client:jar:1.4:compile
    [INFO] |     |  \- org.json:json:jar:20140107:compile
    [INFO] |     +- commons-io:commons-io:jar:1.3.2:compile
    [INFO] |     \- org.apache.commons:commons-lang3:jar:3.4:compile
    [INFO] +- com.mybusiness.myproject:myproject-user-manage-domain:jar:0.0.1-SNAPSHOT:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.2.1.RELEASE:compile
    [INFO] |  |  +- org.aspectj:aspectjrt:jar:1.8.4:compile (version managed from 1.8.2)
    [INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.4:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.2.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.2.1.RELEASE:compile
    [INFO] |  |  |  +- org.springframework:spring-jdbc:jar:4.1.4.RELEASE:compile
    [INFO] |  |  |  \- org.springframework:spring-tx:jar:4.1.4.RELEASE:compile
    [INFO] |  |  +- org.hibernate:hibernate-entitymanager:jar:4.3.7.Final:compile
    [INFO] |  |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
    [INFO] |  |  |  +- org.hibernate:hibernate-core:jar:4.3.7.Final:compile
    [INFO] |  |  |  |  +- antlr:antlr:jar:2.7.7:compile
    [INFO] |  |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
    [INFO] |  |  |  +- dom4j:dom4j:jar:1.6.1:compile
    [INFO] |  |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
    [INFO] |  |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
    [INFO] |  |  |  \- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
    [INFO] |  |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
    [INFO] |  |  +- org.springframework:spring-orm:jar:4.1.4.RELEASE:compile (version managed from 4.0.7.RELEASE)
    [INFO] |  |  +- org.springframework.data:spring-data-jpa:jar:1.7.1.RELEASE:compile
    [INFO] |  |  |  \- org.springframework.data:spring-data-commons:jar:1.9.1.RELEASE:compile
    [INFO] |  |  \- org.springframework:spring-aspects:jar:4.1.4.RELEASE:compile
    [INFO] |  +- org.springframework.security:spring-security-jwt:jar:1.0.2.RELEASE:compile (version managed from 1.0.3.RELEASE)
    [INFO] |  |  \- org.bouncycastle:bcpkix-jdk15on:jar:1.47:compile
    [INFO] |  |     \- org.bouncycastle:bcprov-jdk15on:jar:1.47:compile
    [INFO] |  \- org.flywaydb:flyway-core:jar:3.0:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.1.RELEASE:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.2.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.2.1.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.1.RELEASE:compile
    [INFO] |  |  \- org.yaml:snakeyaml:jar:1.14:runtime
    [INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.2.1.RELEASE:compile
    [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.15:compile
    [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.15:compile
    [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.15:compile
    [INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.15:compile
    [INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.4:compile
    [INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.4.4:compile
    [INFO] |  +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
    [INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
    [INFO] |  |  \- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
    [INFO] |  +- org.springframework:spring-core:jar:4.1.4.RELEASE:compile
    [INFO] |  +- org.springframework:spring-web:jar:4.1.4.RELEASE:compile
    [INFO] |  \- org.springframework:spring-webmvc:jar:4.1.4.RELEASE:compile
    [INFO] |     \- org.springframework:spring-expression:jar:4.1.4.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:1.2.1.RELEASE:compile
    [INFO] |  \- org.springframework.boot:spring-boot-actuator:jar:1.2.1.RELEASE:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.2.1.RELEASE:test (scope not updated to compile)
    [INFO] |  +- junit:junit:jar:4.12:test
    [INFO] |  +- org.mockito:mockito-core:jar:1.10.8:test
    [INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
    [INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
    [INFO] |  \- org.springframework:spring-test:jar:4.1.4.RELEASE:test
    [INFO] +- org.springframework.boot:spring-boot-starter-log4j:jar:1.2.1.RELEASE:compile
    [INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.8:compile (version managed from 1.7.7)
    [INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.8:compile
    [INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.8:compile
    [INFO] |  \- log4j:log4j:jar:1.2.17:compile
    [INFO] +- org.springframework.security.oauth:spring-security-oauth2:jar:2.0.7.RELEASE:compile
    [INFO] |  +- org.springframework:spring-beans:jar:4.1.4.RELEASE:compile
    [INFO] |  +- org.springframework:spring-context:jar:4.1.4.RELEASE:compile
    [INFO] |  +- org.springframework.security:spring-security-core:jar:3.2.5.RELEASE:compile
    [INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
    [INFO] |  +- org.springframework.security:spring-security-config:jar:3.2.5.RELEASE:compile
    [INFO] |  +- org.springframework.security:spring-security-web:jar:3.2.5.RELEASE:compile
    [INFO] |  +- commons-codec:commons-codec:jar:1.6:compile
    [INFO] |  \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
    [INFO] |     \- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
    [INFO] +- org.springframework.hateoas:spring-hateoas:jar:0.16.0.RELEASE:compile
    [INFO] |  +- org.springframework:spring-aop:jar:4.1.4.RELEASE:compile (version managed from 3.2.9.RELEASE)
    [INFO] |  +- org.objenesis:objenesis:jar:2.1:compile
    [INFO] |  \- org.slf4j:slf4j-api:jar:1.7.8:compile (version managed from 1.7.7)
    [INFO] +- com.zaxxer:HikariCP:jar:2.2.5:compile
    [INFO] |  \- org.javassist:javassist:jar:3.18.1-GA:compile
    [INFO] +- ma.glasnost.orika:orika-core:jar:1.4.5:compile
    [INFO] |  +- com.thoughtworks.paranamer:paranamer:jar:2.3:compile
    [INFO] |  +- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.2_jdk5:compile
    [INFO] |  \- com.carrotsearch:java-sizeof:jar:0.0.4:compile
    [INFO] +- io.springfox:springfox-swagger2:jar:2.1.1:compile
    [INFO] |  +- org.mapstruct:mapstruct:jar:1.0.0.Beta4:compile
    [INFO] |  +- io.swagger:swagger-annotations:jar:1.5.0:compile
    [INFO] |  +- io.swagger:swagger-models:jar:1.5.0:compile
    [INFO] |  +- io.springfox:springfox-spi:jar:2.1.1:compile
    [INFO] |  |  \- io.springfox:springfox-core:jar:2.1.1:compile
    [INFO] |  +- io.springfox:springfox-schema:jar:2.1.1:compile
    [INFO] |  +- io.springfox:springfox-swagger-common:jar:2.1.1:compile
    [INFO] |  +- io.springfox:springfox-spring-web:jar:2.1.1:compile
    [INFO] |  +- com.google.guava:guava:jar:18.0:compile
    [INFO] |  +- com.fasterxml:classmate:jar:1.2.0:compile
    [INFO] |  +- joda-time:joda-time:jar:2.5:compile
    [INFO] |  +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
    [INFO] |  \- org.springframework.plugin:spring-plugin-metadata:jar:1.2.0.RELEASE:compile
    [INFO] \- io.springfox:springfox-swagger-ui:jar:2.1.1:compile
    

    So, HikariCP is definitely in tha classpath and tomcat-jdbc is not. But when I launch the service, autoconfig is still creating org.apache.tomcat.jdbc.pool.DataSource instead of a HikariCP one.

    What I am doing wrong?


    EDIT

    This is my application launcher:

    @SpringBootApplication
    @EnableHypermediaSupport(type = {HypermediaType.HAL})
    public class ApplicationRunner {
    
        public static void main(String[] args) {
            SpringApplication.run(ApplicationRunner.class, args);
        }
    
    }
    
  • jscherman
    jscherman over 8 years
    Since this is a valid solution, i was trying to avoid this way to take advantage from Spring Boot auto configuration. But if the problem persists i am definitely going with this. Thanks!
  • jscherman
    jscherman over 8 years
    I know, but tomcat connection is failing In prod due it closes when is inactive for a while. I red that maybe this wouldn't happen with hikaricp (or I have to set test connection for each request which would reduce performance a lot)
  • jscherman
    jscherman over 8 years
    I solved the problem, (but i don't know how actually :P). Just reinstalled eclipse for another reason and all OK. Thanks! Accept you answer since that is a solution anyways
  • jscherman
    jscherman over 8 years
    This might sound a little patetic, but i don't even know what was going on... i've just reinstalled eclipse (for other reason) and it just worked... it was an eclipse-cache related problem maybe? It's weird because i tried cleaning the proyect and updating dependencies without success. Anyways, i will try to reproduce the error tomorrow and see what happen :-)
  • Aritz
    Aritz about 8 years
    @jscherman, I recently ran into the same problem with the Tomcat connection pool (it was being closed after a while, only in production). I solved it with this configuration: spring.datasource.test-on-borrow: true and spring.datasource.validation-query: SELECT 1. That way you force the pool to validate each connection.
  • jscherman
    jscherman over 7 years
    What do you mean? Where am i using spring.datasource.type?
  • martian
    martian over 7 years
    If you are working with 1.3.0+, you can specify the property spring.datasouce.type, otherwise you must define a bean to override spring-boot autoconfiguration.
  • jscherman
    jscherman over 7 years
    I assume that your comment was in respo se to @Ben comment. I get what you say but in the time i made this question, as you say, i couldnt't use the datasour r type param. The way to do it was i said the documentation mentioned: taking hikari from the classpath. The problem is that it wasn't working either. Then after reinstalling eclipse the problem was gone weirdly so i don't know what was the problem
  • martian
    martian over 7 years
    If you deploy the war to tomcat's webapps directory. tomcat-jdbc lib is already there. spring-boot will search tomcat-jdbc before hikaricp. So you configuration won't work. I had written a sample. github.com/zhanhb/spring-boot-druid-sample/blob/…