Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Hikari rather than Commons DBCP in DataSource configuration examples #42971

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.boot.docs.howto.dataaccess.configuretwodatasources;

import org.apache.commons.dbcp2.BasicDataSource;
import com.zaxxer.hikari.HikariDataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand All @@ -30,8 +30,8 @@ public class MyAdditionalDataSourceConfiguration {
@Qualifier("second")
@Bean(defaultCandidate = false)
@ConfigurationProperties("app.datasource")
public BasicDataSource secondDataSource() {
return DataSourceBuilder.create().type(BasicDataSource.class).build();
public HikariDataSource secondDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.boot.docs.howto.dataaccess.configuretwodatasources;

import org.apache.commons.dbcp2.BasicDataSource;
import com.zaxxer.hikari.HikariDataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
Expand All @@ -36,10 +36,10 @@ public DataSourceProperties secondDataSourceProperties() {

@Qualifier("second")
@Bean(defaultCandidate = false)
@ConfigurationProperties("app.datasource.configuration")
public BasicDataSource secondDataSource(
@ConfigurationProperties("app.datasource.hikari")
public HikariDataSource secondDataSource(
@Qualifier("secondDataSourceProperties") DataSourceProperties secondDataSourceProperties) {
return secondDataSourceProperties.initializeDataSourceBuilder().type(BasicDataSource.class).build();
return secondDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.boot.docs.howto.testing.slicetests;

import org.apache.commons.dbcp2.BasicDataSource;
import com.zaxxer.hikari.HikariDataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
Expand All @@ -36,8 +36,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti

@Bean
@ConfigurationProperties("app.datasource.second")
public BasicDataSource secondDataSource() {
return DataSourceBuilder.create().type(BasicDataSource.class).build();
public HikariDataSource secondDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.boot.docs.howto.testing.slicetests;

import org.apache.commons.dbcp2.BasicDataSource;
import com.zaxxer.hikari.HikariDataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
Expand All @@ -28,8 +28,8 @@ public class MyDatasourceConfiguration {

@Bean
@ConfigurationProperties("app.datasource.second")
public BasicDataSource secondDataSource() {
return DataSourceBuilder.create().type(BasicDataSource.class).build();
public HikariDataSource secondDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.boot.docs.howto.dataaccess.configuretwodatasources

import org.apache.commons.dbcp2.BasicDataSource
import com.zaxxer.hikari.HikariDataSource

import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.context.properties.ConfigurationProperties
Expand All @@ -30,8 +30,8 @@ class MyAdditionalDataSourceConfiguration {
@Qualifier("second")
@Bean(defaultCandidate = false)
@ConfigurationProperties("app.datasource")
fun secondDataSource(): BasicDataSource {
return DataSourceBuilder.create().type(BasicDataSource::class.java).build()
fun secondDataSource(): HikariDataSource? {
return DataSourceBuilder.create().type(HikariDataSource::class.java).build()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.boot.docs.howto.dataaccess.configuretwodatasources

import org.apache.commons.dbcp2.BasicDataSource
import com.zaxxer.hikari.HikariDataSource

import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
Expand All @@ -36,9 +36,9 @@ class MyCompleteAdditionalDataSourceConfiguration {

@Qualifier("second")
@Bean(defaultCandidate = false)
@ConfigurationProperties("app.datasource.configuration")
fun secondDataSource(secondDataSourceProperties: DataSourceProperties): BasicDataSource {
return secondDataSourceProperties.initializeDataSourceBuilder().type(BasicDataSource::class.java).build()
@ConfigurationProperties("app.datasource.hikari")
fun secondDataSource(secondDataSourceProperties: DataSourceProperties): HikariDataSource? {
return secondDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource::class.java).build()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.sql.DataSource;

import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -34,8 +35,10 @@
* Tests for {@link MyCompleteAdditionalDataSourceConfiguration}.
*
* @author Stephane Nicoll
* @author Raja Kolli
*/
@SpringBootTest
@SpringBootTest(properties = { "app.datasource.url=jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1",
"app.datasource.hikari.maximum-pool-size=42" })
@Import(MyCompleteAdditionalDataSourceConfiguration.class)
class MyCompleteDataSourcesConfigurationTests {

Expand All @@ -55,7 +58,11 @@ void validateConfiguration() throws SQLException {
assertThat(this.context.getBean("dataSource")).isSameAs(this.dataSource);
assertThat(this.dataSource.getConnection().getMetaData().getURL()).startsWith("jdbc:h2:mem:");
assertThat(this.context.getBean("secondDataSource")).isSameAs(this.secondDataSource);
assertThat(this.secondDataSource.getConnection().getMetaData().getURL()).startsWith("jdbc:h2:mem:");
assertThat(this.secondDataSource).extracting((dataSource) -> ((HikariDataSource) dataSource).getJdbcUrl())
.isEqualTo("jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1");
assertThat(this.secondDataSource)
.extracting((dataSource) -> ((HikariDataSource) dataSource).getMaximumPoolSize())
.isEqualTo(42);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -36,7 +35,7 @@
*
* @author Stephane Nicoll
*/
@SpringBootTest(properties = { "app.datasource.url=jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1", "app.datasource.max-total=42" })
@SpringBootTest(properties = { "app.datasource.jdbcUrl=jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1" })
@Import(MyAdditionalDataSourceConfiguration.class)
class MyDataSourcesConfigurationTests {

Expand All @@ -56,10 +55,7 @@ void validateConfiguration() throws SQLException {
assertThat(this.context.getBean("dataSource")).isSameAs(this.dataSource);
assertThat(this.dataSource.getConnection().getMetaData().getURL()).startsWith("jdbc:h2:mem:");
assertThat(this.context.getBean("secondDataSource")).isSameAs(this.secondDataSource);
assertThat(this.secondDataSource).extracting((dataSource) -> ((BasicDataSource) dataSource).getUrl())
.isEqualTo("jdbc:h2:mem:bar;DB_CLOSE_DELAY=-1");
assertThat(this.secondDataSource).extracting((dataSource) -> ((BasicDataSource) dataSource).getMaxTotal())
.isEqualTo(42);
assertThat(this.secondDataSource.getConnection().getMetaData().getURL()).startsWith("jdbc:h2:mem:");
}

}