Skip to content

DataSource url property is ignored when there is no connection pool #19576

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

Closed
wants to merge 1 commit into from
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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -37,6 +37,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

/**
Expand Down Expand Up @@ -73,8 +74,9 @@ protected static class PooledDataSourceConfiguration {
}

/**
* {@link AnyNestedCondition} that checks that either {@code spring.datasource.type}
* is set or {@link PooledDataSourceAvailableCondition} applies.
* {@link AnyNestedCondition} that checks that either one of
* {@code spring.datasource.type}, {@code spring.datasource.url} properties is set or
* {@link PooledDataSourceAvailableCondition} applies.
*/
static class PooledDataSourceCondition extends AnyNestedCondition {

Expand All @@ -87,6 +89,12 @@ static class ExplicitType {

}

@ConditionalOnProperty(prefix = "spring.datasource", name = "url")
@ConditionalOnClass(SimpleDriverDataSource.class)
static class ExplicitUrl {

}

@Conditional(PooledDataSourceAvailableCondition.class)
static class PooledDataSourceAvailable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand All @@ -20,13 +20,17 @@

import com.zaxxer.hikari.HikariDataSource;

import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -114,12 +118,39 @@ org.apache.commons.dbcp2.BasicDataSource dataSource(DataSourceProperties propert
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type")
@Conditional(ExplicitDataSourceTypeOrUrlCondition.class)
static class Generic {

@Bean
DataSource dataSource(DataSourceProperties properties) {
return properties.initializeDataSourceBuilder().build();
return properties.initializeDataSourceBuilder().type(determineType(properties)).build();
}

private static Class<? extends DataSource> determineType(DataSourceProperties properties) {
Class<? extends DataSource> type = properties.getType();
if (type == null) {
type = DataSourceBuilder.findType(properties.getClassLoader());
}
return (type != null) ? type : SimpleDriverDataSource.class;
}

}

static class ExplicitDataSourceTypeOrUrlCondition extends AnyNestedCondition {

ExplicitDataSourceTypeOrUrlCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}

@ConditionalOnProperty(prefix = "spring.datasource", name = "type")
static class ExplicitType {

}

@ConditionalOnProperty(prefix = "spring.datasource", name = "url")
@ConditionalOnClass(SimpleDriverDataSource.class)
static class ExplicitUrl {

}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -158,6 +158,16 @@ void explicitTypeNoSupportedDataSource() {
.run(this::containsOnlySimpleDriverDataSource);
}

@Test
void createDataSourceExplicitUrlPresentAndNoPoolDataSourceAvailable() {
this.contextRunner
.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"))
.withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb")
.run(this::containsOnlySimpleDriverDataSource);
}

@Test
void explicitTypeSupportedDataSource() {
this.contextRunner
Expand All @@ -169,7 +179,8 @@ void explicitTypeSupportedDataSource() {

private void containsOnlySimpleDriverDataSource(AssertableApplicationContext context) {
assertThat(context).hasSingleBean(DataSource.class);
assertThat(context).getBean(DataSource.class).isExactlyInstanceOf(SimpleDriverDataSource.class);
assertThat(context).getBean(DataSource.class).isExactlyInstanceOf(SimpleDriverDataSource.class)
.extracting("driver").isNotNull();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -89,6 +89,7 @@ private void bind(DataSource result) {
ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();
aliases.addAliases("url", "jdbc-url");
aliases.addAliases("username", "user");
aliases.addAliases("driver-class-name", "driver-class");
Binder binder = new Binder(source.withAliases(aliases));
binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -29,6 +29,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -68,6 +70,13 @@ void defaultToCommonsDbcp2AsLastResort() {
assertThat(this.dataSource).isInstanceOf(BasicDataSource.class);
}

@Test
void simpleDriverDataSource() {
this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(SimpleDriverDataSource.class).build();
assertThat(this.dataSource).isInstanceOf(SimpleDriverDataSource.class);
assertThat(this.dataSource).extracting("driver").isNotNull();
}

@Test
void specificTypeOfDataSource() {
HikariDataSource hikariDataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();
Expand Down