mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add support for auto-configuration of Commons DBCP2
Closes gh-1292 Closes gh-1477
This commit is contained in:
parent
8ed461947f
commit
8e9e502b6a
@ -75,6 +75,11 @@
|
||||
<artifactId>activemq-pool</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-dbcp2</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.solr</groupId>
|
||||
<artifactId>solr-solrj</artifactId>
|
||||
|
@ -43,7 +43,8 @@ public class DataSourceBuilder {
|
||||
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {
|
||||
"org.apache.tomcat.jdbc.pool.DataSource",
|
||||
"com.zaxxer.hikari.HikariDataSource",
|
||||
"org.apache.commons.dbcp.BasicDataSource" };
|
||||
"org.apache.commons.dbcp.BasicDataSource",
|
||||
"org.apache.commons.dbcp2.BasicDataSource" };
|
||||
|
||||
private Class<? extends DataSource> type;
|
||||
|
||||
|
@ -45,9 +45,11 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
@ -70,8 +72,8 @@ public class DataSourceAutoConfigurationTests {
|
||||
@After
|
||||
public void restore() {
|
||||
EmbeddedDatabaseConnection.override = null;
|
||||
if (context != null) {
|
||||
context.close();
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,28 +121,26 @@ public class DataSourceAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void testHikariIsFallback() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
||||
"spring.datasource.url:jdbc:hsqldb:mem:testdb");
|
||||
this.context.setClassLoader(new URLClassLoader(new URL[0], getClass()
|
||||
.getClassLoader()) {
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve)
|
||||
throws ClassNotFoundException {
|
||||
if (name.startsWith("org.apache.tomcat")) {
|
||||
throw new ClassNotFoundException();
|
||||
}
|
||||
return super.loadClass(name, resolve);
|
||||
}
|
||||
});
|
||||
this.context.register(DataSourceAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
DataSource bean = this.context.getBean(DataSource.class);
|
||||
HikariDataSource pool = (HikariDataSource) bean;
|
||||
HikariDataSource pool = testDataSourceFallback(HikariDataSource.class,
|
||||
"org.apache.tomcat");
|
||||
assertEquals("jdbc:hsqldb:mem:testdb", pool.getJdbcUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commonsDbcpIsFallback() throws Exception {
|
||||
BasicDataSource dataSource = testDataSourceFallback(BasicDataSource.class,
|
||||
"org.apache.tomcat", "com.zaxxer.hikari");
|
||||
assertEquals("jdbc:hsqldb:mem:testdb", dataSource.getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commonsDbcp2IsFallback() throws Exception {
|
||||
org.apache.commons.dbcp2.BasicDataSource dataSource = testDataSourceFallback(
|
||||
org.apache.commons.dbcp2.BasicDataSource.class, "org.apache.tomcat",
|
||||
"com.zaxxer.hikari", "org.apache.commons.dbcp.");
|
||||
assertEquals("jdbc:hsqldb:mem:testdb", dataSource.getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmbeddedTypeDefaultsUsername() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
@ -215,6 +215,34 @@ public class DataSourceAutoConfigurationTests {
|
||||
assertNotNull(this.context.getBean(NamedParameterJdbcOperations.class));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T extends DataSource> T testDataSourceFallback(Class<T> expectedType,
|
||||
final String... hiddenPackages) {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
||||
"spring.datasource.url:jdbc:hsqldb:mem:testdb");
|
||||
this.context.setClassLoader(new URLClassLoader(new URL[0], getClass()
|
||||
.getClassLoader()) {
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve)
|
||||
throws ClassNotFoundException {
|
||||
for (String hiddenPackage : hiddenPackages) {
|
||||
if (name.startsWith(hiddenPackage)) {
|
||||
throw new ClassNotFoundException();
|
||||
}
|
||||
}
|
||||
return super.loadClass(name, resolve);
|
||||
}
|
||||
});
|
||||
this.context.register(DataSourceAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
DataSource bean = this.context.getBean(DataSource.class);
|
||||
|
||||
assertThat(bean, instanceOf(expectedType));
|
||||
return (T) bean;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestDataSourceConfiguration {
|
||||
|
||||
|
@ -54,6 +54,7 @@
|
||||
<commons-beanutils.version>1.9.2</commons-beanutils.version>
|
||||
<commons-collections.version>3.2.1</commons-collections.version>
|
||||
<commons-dbcp.version>1.4</commons-dbcp.version>
|
||||
<commons-dbcp2.version>2.0.1</commons-dbcp2.version>
|
||||
<commons-digester.version>2.1</commons-digester.version>
|
||||
<commons-pool.version>1.6</commons-pool.version>
|
||||
<commons-pool2.version>2.2</commons-pool2.version>
|
||||
@ -484,6 +485,11 @@
|
||||
<artifactId>commons-collections</artifactId>
|
||||
<version>${commons-collections.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-dbcp2</artifactId>
|
||||
<version>${commons-dbcp2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-dbcp</groupId>
|
||||
<artifactId>commons-dbcp</artifactId>
|
||||
|
@ -1249,7 +1249,9 @@ Production database connections can also be auto-configured using a pooling
|
||||
|
||||
* We prefer the Tomcat pooling `DataSource` for its performance and concurrency, so if
|
||||
that is available we always choose it.
|
||||
* If commons-dbcp is available we will use that, but we don't recommend it in production.
|
||||
* If HikariCP is available we will use it
|
||||
* 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`
|
||||
``starter POMs'' you will automcatically get a dependency to `tomcat-jdbc`.
|
||||
|
Loading…
Reference in New Issue
Block a user