Created
September 2, 2015 13:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Connection Pooling --> | |
<dependency> | |
<groupId>com.zaxxer</groupId> | |
<artifactId>HikariCP</artifactId> | |
<version>2.4.0</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Spring Boot's
DataSourceBuilder
(i.e.,org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
) does not instantiateHikariDataSource
when both Tomcat and Hikari are found on classpath.