Skip to content

Instantly share code, notes, and snippets.

@fastnsilver
Created September 2, 2015 13:37
Show Gist options
  • Save fastnsilver/8f1c27215c2e7c996b6c to your computer and use it in GitHub Desktop.
Save fastnsilver/8f1c27215c2e7c996b6c to your computer and use it in GitHub Desktop.
How to have Hikari take precendence as DataSource connection pool in a Sprint Boot app
spring:
profiles: h2
datasource:
url: jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
driver-class-name: org.h2.Driver
data-source-class-name: org.h2.jdbcx.JdbcDataSource
username: sa
password: sa
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.codahale.metrics.MetricRegistry;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
public class DataSourceConfig {
@Value("${spring.datasource.data-source-class-name}")
private String dataSourceClassName;
@Autowired
private DataSourceProperties dataSourceProperties;
@Autowired
private MetricRegistry metricRegistry;
@Bean
public HikariDataSource dataSource() {
final HikariDataSource ds = new HikariDataSource();
ds.setMaximumPoolSize(25);
ds.setDataSourceClassName(dataSourceClassName);
ds.addDataSourceProperty("url", dataSourceProperties.getUrl());
ds.addDataSourceProperty("user", dataSourceProperties.getUsername());
ds.addDataSourceProperty("password", dataSourceProperties.getPassword());
ds.setMetricRegistry(metricRegistry);
return ds;
}
}
<!-- Connection Pooling -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.4.0</version>
</dependency>
@fastnsilver
Copy link
Author

Spring Boot's DataSourceBuilder (i.e., org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder) does not instantiate HikariDataSource when both Tomcat and Hikari are found on classpath.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment