Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
03961e66
Commit
03961e66
authored
Oct 06, 2016
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish contribution
Closes gh-7004
parent
7fcb1970
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
78 additions
and
32 deletions
+78
-32
DataSourceProperties.java
...amework/boot/autoconfigure/jdbc/DataSourceProperties.java
+20
-7
EmbeddedDataSourceConfiguration.java
...t/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java
+2
-7
DataSourcePropertiesTests.java
...rk/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java
+13
-0
EmbeddedDataSourceConfigurationTests.java
...oconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java
+34
-18
appendix-application-properties.adoc
...cs/src/main/asciidoc/appendix-application-properties.adoc
+1
-0
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+8
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java
View file @
03961e66
...
...
@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.jdbc;
import
java.nio.charset.Charset
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.UUID
;
import
javax.sql.DataSource
;
...
...
@@ -58,9 +59,9 @@ public class DataSourceProperties
private
String
name
=
"testdb"
;
/**
*
If <code>true</code> the database name is randomly generated
.
*
Generate a random datasource name
.
*/
private
boolean
generate
Name
=
fals
e
;
private
boolean
generate
UniqueNam
e
;
/**
* Fully qualified name of the connection pool implementation to use. By default, it
...
...
@@ -153,6 +154,8 @@ public class DataSourceProperties
private
Xa
xa
=
new
Xa
();
private
String
uniqueName
;
@Override
public
void
setBeanClassLoader
(
ClassLoader
classLoader
)
{
this
.
classLoader
=
classLoader
;
...
...
@@ -188,12 +191,12 @@ public class DataSourceProperties
this
.
name
=
name
;
}
public
boolean
isGenerateName
()
{
return
this
.
generateName
;
public
boolean
isGenerate
Unique
Name
()
{
return
this
.
generate
Unique
Name
;
}
public
void
setGenerate
Name
(
boolean
generat
eName
)
{
this
.
generate
Name
=
generat
eName
;
public
void
setGenerate
UniqueName
(
boolean
generateUniqu
eName
)
{
this
.
generate
UniqueName
=
generateUniqu
eName
;
}
public
Class
<?
extends
DataSource
>
getType
()
{
...
...
@@ -281,7 +284,7 @@ public class DataSourceProperties
if
(
StringUtils
.
hasText
(
this
.
url
))
{
return
this
.
url
;
}
String
url
=
this
.
embeddedDatabaseConnection
.
getUrl
(
this
.
name
);
String
url
=
this
.
embeddedDatabaseConnection
.
getUrl
(
determineDatabaseName
()
);
if
(!
StringUtils
.
hasText
(
url
))
{
throw
new
DataSourceBeanCreationException
(
this
.
embeddedDatabaseConnection
,
this
.
environment
,
"url"
);
...
...
@@ -289,6 +292,16 @@ public class DataSourceProperties
return
url
;
}
private
String
determineDatabaseName
()
{
if
(
this
.
generateUniqueName
)
{
if
(
this
.
uniqueName
==
null
)
{
this
.
uniqueName
=
UUID
.
randomUUID
().
toString
();
}
return
this
.
uniqueName
;
}
return
this
.
name
;
}
/**
* Return the configured username or {@code null} if none was configured.
* @return the configured username
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java
View file @
03961e66
...
...
@@ -55,13 +55,8 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
public
EmbeddedDatabase
dataSource
()
{
EmbeddedDatabaseBuilder
builder
=
new
EmbeddedDatabaseBuilder
()
.
setType
(
EmbeddedDatabaseConnection
.
get
(
this
.
classLoader
).
getType
());
if
(
this
.
properties
.
isGenerateName
())
{
builder
.
generateUniqueName
(
true
);
}
else
{
builder
.
setName
(
this
.
properties
.
getName
());
}
this
.
database
=
builder
.
build
();
this
.
database
=
builder
.
setName
(
this
.
properties
.
getName
())
.
generateUniqueName
(
this
.
properties
.
isGenerateUniqueName
()).
build
();
return
this
.
database
;
}
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java
View file @
03961e66
...
...
@@ -66,6 +66,19 @@ public class DataSourcePropertiesTests {
assertThat
(
properties
.
determineUrl
()).
isEqualTo
(
"jdbc:mysql://mydb"
);
}
@Test
public
void
determineUrlWithGenerateUniqueName
()
throws
Exception
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
properties
.
setGenerateUniqueName
(
true
);
properties
.
afterPropertiesSet
();
assertThat
(
properties
.
determineUrl
()).
isEqualTo
(
properties
.
determineUrl
());
DataSourceProperties
properties2
=
new
DataSourceProperties
();
properties2
.
setGenerateUniqueName
(
true
);
properties2
.
afterPropertiesSet
();
assertThat
(
properties
.
determineUrl
()).
isNotEqualTo
(
properties2
.
determineUrl
());
}
@Test
public
void
determineUsername
()
throws
Exception
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java
View file @
03961e66
...
...
@@ -19,14 +19,14 @@ package org.springframework.boot.autoconfigure.jdbc;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.Properties
;
import
javax.sql.DataSource
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.test.util.EnvironmentTestUtils
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.core.env.PropertiesPropertySource
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -34,32 +34,40 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link EmbeddedDataSourceConfiguration}.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
public
class
EmbeddedDataSourceConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
@After
public
void
closeContext
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
public
void
testDefaultEmbeddedDatabase
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
EmbeddedDataSourceConfiguration
.
class
);
this
.
context
.
refresh
();
public
void
defaultEmbeddedDatabase
()
{
this
.
context
=
load
();
assertThat
(
this
.
context
.
getBean
(
DataSource
.
class
)).
isNotNull
();
this
.
context
.
close
();
}
@Test
public
void
generatesUniqueDatabaseName
()
throws
Exception
{
Properties
myProps
=
new
Properties
();
myProps
.
setProperty
(
"spring.datasource.generate-name"
,
"true"
);
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
EmbeddedDataSourceConfiguration
.
class
);
this
.
context
.
getEnvironment
().
getPropertySources
().
addFirst
(
new
PropertiesPropertySource
(
"whatever"
,
myProps
));
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
getDatabaseName
(
dataSource
)).
isNotEqualToIgnoringCase
(
"testdb"
);
this
.
context
.
close
();
public
void
generateUniqueName
()
throws
Exception
{
this
.
context
=
load
(
"spring.datasource.generate-unique-name=true"
);
AnnotationConfigApplicationContext
context2
=
load
(
"spring.datasource.generate-unique-name=true"
);
try
{
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
DataSource
dataSource2
=
context2
.
getBean
(
DataSource
.
class
);
assertThat
(
getDatabaseName
(
dataSource
))
.
isNotEqualTo
(
getDatabaseName
(
dataSource2
));
System
.
out
.
println
(
dataSource2
);
}
finally
{
context2
.
close
();
}
}
private
String
getDatabaseName
(
DataSource
dataSource
)
throws
SQLException
{
...
...
@@ -78,4 +86,12 @@ public class EmbeddedDataSourceConfigurationTests {
}
}
private
AnnotationConfigApplicationContext
load
(
String
...
environment
)
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
EnvironmentTestUtils
.
addEnvironment
(
ctx
,
environment
);
ctx
.
register
(
EmbeddedDataSourceConfiguration
.
class
);
ctx
.
refresh
();
return
ctx
;
}
}
spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc
View file @
03961e66
...
...
@@ -620,6 +620,7 @@ content into your application; rather pick only the properties that you need.
spring.datasource.dbcp.*= # Commons DBCP specific settings
spring.datasource.dbcp2.*= # Commons DBCP2 specific settings
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
spring.datasource.generate-unique-name=false # Generate a random datasource name.
spring.datasource.hikari.*= # Hikari specific settings
spring.datasource.initialize=true # Populate the database using 'data.sql'.
spring.datasource.jmx-enabled=false # Enable JMX support (if provided by the underlying pool).
...
...
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
03961e66
...
...
@@ -2655,6 +2655,14 @@ http://hsqldb.org/[HSQL] and http://db.apache.org/derby/[Derby] databases. You d
to provide any connection URLs, simply include a build dependency to the embedded database
that you want to use.
[NOTE]
====
If you are using this feature in your tests, you may notice that the same database is
reused by your whole test suite regardless of the number of application contexts that
you use. If you want to make sure that each context has a separate embedded database,
you should set `spring.datasource.generate-unique-name` to `true`.
====
For example, typical POM dependencies would be:
[source,xml,indent=0]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment