Fix customization of database name
Previously, the `spring.datasource.name` property was ignored when Spring Boot configures an embedded data source with a connection pool. `EmbeddedDatabaseConnection` is now aligned to the purely embedded case to take that property into account. Closes gh-4586
This commit is contained in:
parent
e6588dbf70
commit
9d29ab73a4
|
|
@ -205,7 +205,7 @@ public class DataSourceProperties
|
|||
if (StringUtils.hasText(this.url)) {
|
||||
return this.url;
|
||||
}
|
||||
String url = this.embeddedDatabaseConnection.getUrl();
|
||||
String url = this.embeddedDatabaseConnection.getUrl(this.name);
|
||||
if (!StringUtils.hasText(url)) {
|
||||
throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection,
|
||||
this.environment, "url");
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ 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;
|
||||
|
||||
/**
|
||||
|
|
@ -32,6 +33,7 @@ import org.springframework.util.ClassUtils;
|
|||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
* @author Stephane Nicoll
|
||||
* @see #get(ClassLoader)
|
||||
*/
|
||||
public enum EmbeddedDatabaseConnection {
|
||||
|
|
@ -45,18 +47,20 @@ public enum EmbeddedDatabaseConnection {
|
|||
* H2 Database Connection.
|
||||
*/
|
||||
H2(EmbeddedDatabaseType.H2, "org.h2.Driver",
|
||||
"jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"),
|
||||
"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"),
|
||||
|
||||
/**
|
||||
* Derby Database Connection.
|
||||
*/
|
||||
DERBY(EmbeddedDatabaseType.DERBY, "org.apache.derby.jdbc.EmbeddedDriver",
|
||||
"jdbc:derby:memory:testdb;create=true"),
|
||||
"jdbc:derby:memory:%s;create=true"),
|
||||
|
||||
/**
|
||||
* HSQL Database Connection.
|
||||
*/
|
||||
HSQL(EmbeddedDatabaseType.HSQL, "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:testdb");
|
||||
HSQL(EmbeddedDatabaseType.HSQL, "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:%s");
|
||||
|
||||
private static final String DEFAULT_DATABASE_NAME = "testdb";
|
||||
|
||||
private final EmbeddedDatabaseType type;
|
||||
|
||||
|
|
@ -88,11 +92,21 @@ public enum EmbeddedDatabaseConnection {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for the connection.
|
||||
* Returns the URL for the connection using the default database name.
|
||||
* @return the connection URL
|
||||
*/
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
return getUrl(DEFAULT_DATABASE_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for the connection using the specified {@code databaseName}.
|
||||
* @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 String.format(this.url, databaseName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright 2012-2015 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 org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link EmbeddedDatabaseConnection}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class EmbeddedDatabaseConnectionTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void h2CustomDatabaseName() {
|
||||
assertThat(EmbeddedDatabaseConnection.H2.getUrl("mydb"),
|
||||
is("jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void derbyCustomDatabaseName() {
|
||||
assertThat(EmbeddedDatabaseConnection.DERBY.getUrl("myderbydb"),
|
||||
is("jdbc:derby:memory:myderbydb;create=true"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hsqlCustomDatabaseName() {
|
||||
assertThat(EmbeddedDatabaseConnection.HSQL.getUrl("myhsql"),
|
||||
is("jdbc:hsqldb:mem:myhsql"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUrlWithNoDatabaseName() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
EmbeddedDatabaseConnection.H2.getUrl(" ");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue