Keep "testdb" default datasource name internal

Previously, Hikari's pool name was auto-configured with the value of
`spring.datasource.name` that defaults  to `testdb`, which brings some
confusion.

This commit removes the default `testdb` value on
`spring.datasource.name` as it is a sane default only for an embedded
datasource. It is applied whenever applicable instead.

Closes gh-11719
This commit is contained in:
Stephane Nicoll 2018-01-24 15:23:10 +01:00 committed by Phillip Webb
parent 017efda6ec
commit b67903a04a
9 changed files with 57 additions and 28 deletions

View File

@ -50,7 +50,8 @@ public class DataSourceHealthIndicatorTests {
@Before @Before
public void init() { public void init() {
EmbeddedDatabaseConnection db = EmbeddedDatabaseConnection.HSQL; EmbeddedDatabaseConnection db = EmbeddedDatabaseConnection.HSQL;
this.dataSource = new SingleConnectionDataSource(db.getUrl() + ";shutdown=true", this.dataSource = new SingleConnectionDataSource(db.getUrl(
EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME) + ";shutdown=true",
"sa", "", false); "sa", "", false);
this.dataSource.setDriverClassName(db.getDriverClassName()); this.dataSource.setDriverClassName(db.getDriverClassName());
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DatabaseDriver; import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;
/** /**
* Actual DataSource configurations imported by {@link DataSourceAutoConfiguration}. * Actual DataSource configurations imported by {@link DataSourceAutoConfiguration}.
@ -79,7 +80,7 @@ abstract class DataSourceConfiguration {
public HikariDataSource dataSource(DataSourceProperties properties) { public HikariDataSource dataSource(DataSourceProperties properties) {
HikariDataSource dataSource = createDataSource(properties, HikariDataSource dataSource = createDataSource(properties,
HikariDataSource.class); HikariDataSource.class);
if (properties.getName() != null) { if (StringUtils.hasText(properties.getName())) {
dataSource.setPoolName(properties.getName()); dataSource.setPoolName(properties.getName());
} }
return dataSource; return dataSource;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -58,9 +58,9 @@ public class DataSourceProperties
private Environment environment; private Environment environment;
/** /**
* Name of the datasource. * Name of the datasource. Default to "testdb" when using an embedded database.
*/ */
private String name = "testdb"; private String name;
/** /**
* Whether to generate a random datasource name. * Whether to generate a random datasource name.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,6 +25,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.util.StringUtils;
/** /**
* Configuration for embedded data sources. * Configuration for embedded data sources.
@ -56,7 +57,10 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
public EmbeddedDatabase dataSource() { public EmbeddedDatabase dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseConnection.get(this.classLoader).getType()); .setType(EmbeddedDatabaseConnection.get(this.classLoader).getType());
this.database = builder.setName(this.properties.getName()) String name = (StringUtils.hasText(this.properties.getName())
? this.properties.getName()
: EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME);
this.database = builder.setName(name)
.generateUniqueName(this.properties.isGenerateUniqueName()).build(); .generateUniqueName(this.properties.isGenerateUniqueName()).build();
return this.database; return this.database;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,10 +18,12 @@ package org.springframework.boot.autoconfigure.jdbc;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException; import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName; import javax.management.ObjectName;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
@ -68,6 +70,23 @@ public class DataSourceJmxConfigurationTests {
validateHikariMBeansRegistration(mBeanServer, poolName, true); validateHikariMBeansRegistration(mBeanServer, poolName, true);
} }
@Test
public void hikariAutoConfiguredWithoutDataSourceName()
throws MalformedObjectNameException {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectInstance> existingInstances = mBeanServer.queryMBeans(
new ObjectName("com.zaxxer.hikari:type=*"), null);
load("spring.datasource.type=" + HikariDataSource.class.getName(),
"spring.datasource.hikari.register-mbeans=true");
assertThat(this.context.getBeansOfType(HikariDataSource.class)).hasSize(1);
assertThat(this.context.getBean(HikariDataSource.class).isRegisterMbeans())
.isTrue();
// We can rely on the number of MBeans so we're checking that the pool and pool
// config mBeans were registered
assertThat(mBeanServer.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"),
null).size()).isEqualTo(existingInstances.size() + 2);
}
@Test @Test
public void hikariAutoConfiguredUsesJmsFlag() throws MalformedObjectNameException { public void hikariAutoConfiguredUsesJmsFlag() throws MalformedObjectNameException {
String poolName = UUID.randomUUID().toString(); String poolName = UUID.randomUUID().toString();

View File

@ -62,7 +62,8 @@ public class DataSourcePropertiesTests {
properties.afterPropertiesSet(); properties.afterPropertiesSet();
assertThat(properties.getUrl()).isNull(); assertThat(properties.getUrl()).isNull();
assertThat(properties.determineUrl()) assertThat(properties.determineUrl())
.isEqualTo(EmbeddedDatabaseConnection.H2.getUrl()); .isEqualTo(EmbeddedDatabaseConnection.H2.getUrl(
EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME));
} }
@Test @Test

View File

@ -689,7 +689,7 @@ content into your application. Rather, pick only the properties that you need.
spring.datasource.initialization-mode=embedded # Initialize the datasource with available DDL and DML scripts. spring.datasource.initialization-mode=embedded # Initialize the datasource with available DDL and DML scripts.
spring.datasource.jmx-enabled=false # Whether to enable JMX support (if provided by the underlying pool). spring.datasource.jmx-enabled=false # Whether to enable JMX support (if provided by the underlying pool).
spring.datasource.jndi-name= # JNDI location of the datasource. Class, url, username & password are ignored when set. spring.datasource.jndi-name= # JNDI location of the datasource. Class, url, username & password are ignored when set.
spring.datasource.name=testdb # Name of the datasource. spring.datasource.name= # Name of the datasource. Default to "testdb" when using an embedded database.
spring.datasource.password= # Login password of the database. spring.datasource.password= # Login password of the database.
spring.datasource.platform=all # Platform to use in the DDL or DML scripts (such as schema-${platform}.sql or data-${platform}.sql). spring.datasource.platform=all # Platform to use in the DDL or DML scripts (such as schema-${platform}.sql or data-${platform}.sql).
spring.datasource.schema= # Schema (DDL) script resource references. spring.datasource.schema= # Schema (DDL) script resource references.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,8 +25,8 @@ import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/** /**
* Connection details for {@link EmbeddedDatabaseType embedded databases}. * Connection details for {@link EmbeddedDatabaseType embedded databases}.
@ -60,7 +60,10 @@ public enum EmbeddedDatabaseConnection {
*/ */
HSQL(EmbeddedDatabaseType.HSQL, "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:%s"); HSQL(EmbeddedDatabaseType.HSQL, "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:%s");
private static final String DEFAULT_DATABASE_NAME = "testdb"; /**
* Default database name.
*/
public static final String DEFAULT_DATABASE_NAME = "testdb";
private final EmbeddedDatabaseType type; private final EmbeddedDatabaseType type;
@ -92,21 +95,15 @@ public enum EmbeddedDatabaseConnection {
} }
/** /**
* Returns the URL for the connection using the default database name. * Returns the URL for the connection using the specified {@code databaseName} or
* @return the connection URL * {@value DEFAULT_DATABASE_NAME} if {@code databaseName} is empty or {@code null}.
*/
public String getUrl() {
return getUrl(DEFAULT_DATABASE_NAME);
}
/**
* Returns the URL for the connection using the specified {@code databaseName}.
* @param databaseName the name of the database * @param databaseName the name of the database
* @return the connection URL * @return the connection URL
*/ */
public String getUrl(String databaseName) { public String getUrl(String databaseName) {
Assert.hasText(databaseName, "DatabaseName must not be null."); String name = (StringUtils.hasText(databaseName)
return (this.url != null ? String.format(this.url, databaseName) : null); ? databaseName : DEFAULT_DATABASE_NAME);
return (this.url != null ? String.format(this.url, name) : null);
} }
/** /**

View File

@ -51,9 +51,15 @@ public class EmbeddedDatabaseConnectionTests {
} }
@Test @Test
public void getUrlWithNoDatabaseName() { public void getUrlWithNullDatabaseName() {
this.thrown.expect(IllegalArgumentException.class); assertThat(EmbeddedDatabaseConnection.HSQL.getUrl(null))
EmbeddedDatabaseConnection.H2.getUrl(" "); .isEqualTo("jdbc:hsqldb:mem:testdb");
}
@Test
public void getUrlWithEmptyDatabaseName() {
assertThat(EmbeddedDatabaseConnection.HSQL.getUrl(" "))
.isEqualTo("jdbc:hsqldb:mem:testdb");
} }
} }