Spring Boot Hikari

Guys, I got the following properties to work, kind of. The following creates 2 pools. One connection, in the first pool, and then 20 in the second. 

spring.jpa.properties.hibernate.hikari.minimumIdle = 20
spring.jpa.properties.hibernate.hikari.maximumPoolSize = 30
spring.jpa.properties.hibernate.hikari.idleTimeout = 5000
spring.jpa.properties.hibernate.hikari.dataSource.serverName = server
spring.jpa.properties.hibernate.hikari.dataSource.portNumber = 50000
spring.jpa.properties.hibernate.hikari.dataSource.databaseName = db
spring.jpa.properties.hibernate.hikari.dataSource.clientProgramName=appname
spring.jpa.properties.hibernate.hikari.dataSource.currentSchema=schema
spring.jpa.properties.hibernate.hikari.dataSource.user=user
spring.jpa.properties.hibernate.hikari.dataSource.password=pwd
spring.jpa.properties.hibernate.hikari.dataSourceClassName=com.ibm.db2.jcc.DB2SimpleDataSource
spring.jpa.properties.hibernate.hikari.dataSource.driverType=4
spring.jpa.properties.hibernate.connection.provider_class = org.hibernate.hikaricp.internal.HikariCPConnectionProvider

spring.datasource.url=jdbc:db2://server:50000/db
spring.datasource.username=user
spring.datasource.password=pwd

https://github.com/brettwooldridge/HikariCP/issues/604

29.1.2 Connection to a production database

Production database connections can also be auto-configured using a pooling DataSource. Here’s the algorithm for choosing a specific implementation:

  • We prefer the Tomcat pooling DataSource for its performance and concurrency, so if that is available we always choose it.
  • Otherwise, if HikariCP is available we will use it.
  • If neither the Tomcat pooling datasource nor HikariCP are available and if Commons DBCP is available we will use it, but we don’t recommend it in production.
  • Lastly, if Commons DBCP2 is available we will use it.

If you use the spring-boot-starter-jdbc or spring-boot-starter-data-jpa ‘starters’ you will automatically get a dependency to tomcat-jdbc.

http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/htmlsingle/#using-boot-running-with-the-gradle-plugin

You can use the dataSourceClassName approach, here is an example with MySQL. (Tested with spring boot 1.3 and 1.4)

First you need to exclude tomcat-jdbc from the classpath as it will be picked in favor of hikaricp.

   <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>

application.properties

spring.datasource.dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
spring.datasource.dataSourceProperties.serverName=localhost
spring.datasource.dataSourceProperties.portNumber=3311
spring.datasource.dataSourceProperties.databaseName=mydb
spring.datasource.username=root
spring.datasource.password=root
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

I created a test project here: https://github.com/ydemartino/spring-boot-hikaricp

        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        DataSource datasource = ctx.getBean(DataSource.class);
        System.out.println(datasource.getClass().getCanonicalName());

https://github.com/ydemartino/spring-boot-hikaricp

 my test java config (for MySql)

@Bean(destroyMethod = "close")
public DataSource dataSource(){
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
    hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/spring-test"); 
    hikariConfig.setUsername("root");
    hikariConfig.setPassword("admin");

    hikariConfig.setMaximumPoolSize(5);
    hikariConfig.setConnectionTestQuery("SELECT 1");
    hikariConfig.setPoolName("springHikariCP");

    hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
    hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
    hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
    hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");

    HikariDataSource dataSource = new HikariDataSource(hikariConfig);

    return dataSource;
}

http://stackoverflow.com/questions/23172643/how-to-set-up-datasource-with-spring-for-hikaricp

原文地址:https://www.cnblogs.com/softidea/p/6344041.html