Fix order of connection pools in DataSourceBuilder
Closes gh-6013
This commit is contained in:
parent
bbc37d1e71
commit
b2e3c5dd61
|
|
@ -33,8 +33,8 @@ import org.springframework.util.ClassUtils;
|
|||
|
||||
/**
|
||||
* Convenience class for building a {@link DataSource} with common implementations and
|
||||
* properties. If Tomcat, HikariCP or Commons DBCP are on the classpath one of them will
|
||||
* be selected (in that order with Tomcat first). In the interest of a uniform interface,
|
||||
* properties. If HikariCP, Tomcat or Commons DBCP are on the classpath one of them will
|
||||
* be selected (in that order with Hikari first). In the interest of a uniform interface,
|
||||
* and so that there can be a fallback to an embedded database if one can be detected on
|
||||
* the classpath, only a small set of common configuration properties are supported. To
|
||||
* inject additional properties into the result you can downcast it, or use
|
||||
|
|
@ -47,8 +47,8 @@ import org.springframework.util.ClassUtils;
|
|||
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.tomcat.jdbc.pool.DataSource",
|
||||
"org.apache.commons.dbcp2.BasicDataSource" };
|
||||
|
||||
private Class<? extends DataSource> type;
|
||||
|
|
|
|||
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverPropertyInfo;
|
||||
|
|
@ -303,26 +301,4 @@ public class DataSourceAutoConfigurationTests {
|
|||
|
||||
}
|
||||
|
||||
private static final class HidePackagesClassLoader extends URLClassLoader {
|
||||
|
||||
private final String[] hiddenPackages;
|
||||
|
||||
private HidePackagesClassLoader(String... hiddenPackages) {
|
||||
super(new URL[0], DataSourceAutoConfigurationTests.class.getClassLoader());
|
||||
this.hiddenPackages = hiddenPackages;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve)
|
||||
throws ClassNotFoundException {
|
||||
for (String hiddenPackage : this.hiddenPackages) {
|
||||
if (name.startsWith(hiddenPackage)) {
|
||||
throw new ClassNotFoundException();
|
||||
}
|
||||
}
|
||||
return super.loadClass(name, resolve);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link DataSourceBuilder}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DataSourceBuilderTests {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
@After
|
||||
public void shutdownDataSource() throws IOException {
|
||||
if (this.dataSource instanceof Closeable) {
|
||||
((Closeable) this.dataSource).close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultToHikari() {
|
||||
this.dataSource = DataSourceBuilder.create()
|
||||
.url("jdbc:h2:test").build();
|
||||
assertThat(this.dataSource).isInstanceOf(HikariDataSource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultToTomcatIfHikariIsNotAvailable() {
|
||||
this.dataSource = DataSourceBuilder.create(
|
||||
new HidePackagesClassLoader("com.zaxxer.hikari"))
|
||||
.url("jdbc:h2:test").build();
|
||||
assertThat(this.dataSource).isInstanceOf(
|
||||
org.apache.tomcat.jdbc.pool.DataSource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultToCommonsDbcp2AsLastResort() {
|
||||
this.dataSource = DataSourceBuilder.create(
|
||||
new HidePackagesClassLoader("com.zaxxer.hikari",
|
||||
"org.apache.tomcat.jdbc.pool"))
|
||||
.url("jdbc:h2:test").build();
|
||||
assertThat(this.dataSource).isInstanceOf(BasicDataSource.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -95,9 +95,7 @@ public class DataSourceInitializerTests {
|
|||
public void testTwoDataSources() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"datasource.one.url=jdbc:hsqldb:mem:/one",
|
||||
"datasource.one.driverClassName=org.hsqldb.Driver",
|
||||
"datasource.two.url=jdbc:hsqldb:mem:/two",
|
||||
"datasource.two.driverClassName=org.hsqldb.Driver");
|
||||
"datasource.two.url=jdbc:hsqldb:mem:/two");
|
||||
this.context.register(TwoDataSources.class, DataSourceInitializer.class,
|
||||
PropertyPlaceholderAutoConfiguration.class, DataSourceProperties.class);
|
||||
this.context.refresh();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
/**
|
||||
* Test {@link URLClassLoader} that hides configurable packages.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
final class HidePackagesClassLoader extends URLClassLoader {
|
||||
|
||||
private final String[] hiddenPackages;
|
||||
|
||||
HidePackagesClassLoader(String... hiddenPackages) {
|
||||
super(new URL[0], HidePackagesClassLoader.class.getClassLoader());
|
||||
this.hiddenPackages = hiddenPackages;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve)
|
||||
throws ClassNotFoundException {
|
||||
for (String hiddenPackage : this.hiddenPackages) {
|
||||
if (name.startsWith(hiddenPackage)) {
|
||||
throw new ClassNotFoundException();
|
||||
}
|
||||
}
|
||||
return super.loadClass(name, resolve);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue