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:
committed by
Phillip Webb
parent
017efda6ec
commit
b67903a04a
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user