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:
parent
017efda6ec
commit
b67903a04a
|
@ -50,7 +50,8 @@ public class DataSourceHealthIndicatorTests {
|
|||
@Before
|
||||
public void init() {
|
||||
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);
|
||||
this.dataSource.setDriverClassName(db.getDriverClassName());
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
* 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.jdbc.DatabaseDriver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Actual DataSource configurations imported by {@link DataSourceAutoConfiguration}.
|
||||
|
@ -79,7 +80,7 @@ abstract class DataSourceConfiguration {
|
|||
public HikariDataSource dataSource(DataSourceProperties properties) {
|
||||
HikariDataSource dataSource = createDataSource(properties,
|
||||
HikariDataSource.class);
|
||||
if (properties.getName() != null) {
|
||||
if (StringUtils.hasText(properties.getName())) {
|
||||
dataSource.setPoolName(properties.getName());
|
||||
}
|
||||
return dataSource;
|
||||
|
|
|
@ -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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -58,9 +58,9 @@ public class DataSourceProperties
|
|||
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.
|
||||
|
|
|
@ -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");
|
||||
* 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.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Configuration for embedded data sources.
|
||||
|
@ -56,7 +57,10 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
|
|||
public EmbeddedDatabase dataSource() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
||||
.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();
|
||||
return this.database;
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
* 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.sql.SQLException;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.MalformedObjectNameException;
|
||||
import javax.management.ObjectInstance;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
@ -68,6 +70,23 @@ public class DataSourceJmxConfigurationTests {
|
|||
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
|
||||
public void hikariAutoConfiguredUsesJmsFlag() throws MalformedObjectNameException {
|
||||
String poolName = UUID.randomUUID().toString();
|
||||
|
|
|
@ -62,7 +62,8 @@ public class DataSourcePropertiesTests {
|
|||
properties.afterPropertiesSet();
|
||||
assertThat(properties.getUrl()).isNull();
|
||||
assertThat(properties.determineUrl())
|
||||
.isEqualTo(EmbeddedDatabaseConnection.H2.getUrl());
|
||||
.isEqualTo(EmbeddedDatabaseConnection.H2.getUrl(
|
||||
EmbeddedDatabaseConnection.DEFAULT_DATABASE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -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.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.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.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.
|
||||
|
|
|
@ -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");
|
||||
* 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.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Connection details for {@link EmbeddedDatabaseType embedded databases}.
|
||||
|
@ -60,7 +60,10 @@ public enum EmbeddedDatabaseConnection {
|
|||
*/
|
||||
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;
|
||||
|
||||
|
@ -92,21 +95,15 @@ public enum EmbeddedDatabaseConnection {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for the connection using the default database name.
|
||||
* @return the connection URL
|
||||
*/
|
||||
public String getUrl() {
|
||||
return getUrl(DEFAULT_DATABASE_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for the connection using the specified {@code databaseName}.
|
||||
* Returns the URL for the connection using the specified {@code databaseName} or
|
||||
* {@value DEFAULT_DATABASE_NAME} if {@code databaseName} is empty or {@code null}.
|
||||
* @param databaseName the name of the database
|
||||
* @return the connection URL
|
||||
*/
|
||||
public String getUrl(String databaseName) {
|
||||
Assert.hasText(databaseName, "DatabaseName must not be null.");
|
||||
return (this.url != null ? String.format(this.url, databaseName) : null);
|
||||
String name = (StringUtils.hasText(databaseName)
|
||||
? databaseName : DEFAULT_DATABASE_NAME);
|
||||
return (this.url != null ? String.format(this.url, name) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -51,9 +51,15 @@ public class EmbeddedDatabaseConnectionTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void getUrlWithNoDatabaseName() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
EmbeddedDatabaseConnection.H2.getUrl(" ");
|
||||
public void getUrlWithNullDatabaseName() {
|
||||
assertThat(EmbeddedDatabaseConnection.HSQL.getUrl(null))
|
||||
.isEqualTo("jdbc:hsqldb:mem:testdb");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUrlWithEmptyDatabaseName() {
|
||||
assertThat(EmbeddedDatabaseConnection.HSQL.getUrl(" "))
|
||||
.isEqualTo("jdbc:hsqldb:mem:testdb");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue