Commit b2e3c5dd authored by Stephane Nicoll's avatar Stephane Nicoll

Fix order of connection pools in DataSourceBuilder

Closes gh-6013
parent bbc37d1e
...@@ -33,8 +33,8 @@ import org.springframework.util.ClassUtils; ...@@ -33,8 +33,8 @@ import org.springframework.util.ClassUtils;
/** /**
* Convenience class for building a {@link DataSource} with common implementations and * 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 * properties. If HikariCP, Tomcat 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, * 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 * 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 * 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 * inject additional properties into the result you can downcast it, or use
...@@ -47,8 +47,8 @@ import org.springframework.util.ClassUtils; ...@@ -47,8 +47,8 @@ import org.springframework.util.ClassUtils;
public class DataSourceBuilder { public class DataSourceBuilder {
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] { private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {
"org.apache.tomcat.jdbc.pool.DataSource",
"com.zaxxer.hikari.HikariDataSource", "com.zaxxer.hikari.HikariDataSource",
"org.apache.tomcat.jdbc.pool.DataSource",
"org.apache.commons.dbcp2.BasicDataSource" }; "org.apache.commons.dbcp2.BasicDataSource" };
private Class<? extends DataSource> type; private Class<? extends DataSource> type;
......
...@@ -16,8 +16,6 @@ ...@@ -16,8 +16,6 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Driver; import java.sql.Driver;
import java.sql.DriverPropertyInfo; import java.sql.DriverPropertyInfo;
...@@ -303,26 +301,4 @@ public class DataSourceAutoConfigurationTests { ...@@ -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);
}
}
} }
/*
* 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 { ...@@ -95,9 +95,7 @@ public class DataSourceInitializerTests {
public void testTwoDataSources() throws Exception { public void testTwoDataSources() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"datasource.one.url=jdbc:hsqldb:mem:/one", "datasource.one.url=jdbc:hsqldb:mem:/one",
"datasource.one.driverClassName=org.hsqldb.Driver", "datasource.two.url=jdbc:hsqldb:mem:/two");
"datasource.two.url=jdbc:hsqldb:mem:/two",
"datasource.two.driverClassName=org.hsqldb.Driver");
this.context.register(TwoDataSources.class, DataSourceInitializer.class, this.context.register(TwoDataSources.class, DataSourceInitializer.class,
PropertyPlaceholderAutoConfiguration.class, DataSourceProperties.class); PropertyPlaceholderAutoConfiguration.class, DataSourceProperties.class);
this.context.refresh(); this.context.refresh();
......
/*
* 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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment