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
99a45bde
Commit
99a45bde
authored
Oct 15, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix datasource prefix in multiple-datasource configuration doc
Closes gh-13195
parent
0fedf8d2
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
32 additions
and
33 deletions
+32
-33
howto.adoc
...oot-project/spring-boot-docs/src/main/asciidoc/howto.adoc
+15
-21
CompleteTwoDataSourcesExample.java
...amework/boot/docs/jdbc/CompleteTwoDataSourcesExample.java
+10
-7
ConfigurableDataSourceExample.java
...amework/boot/docs/jdbc/ConfigurableDataSourceExample.java
+1
-1
SimpleTwoDataSourcesExample.java
...framework/boot/docs/jdbc/SimpleTwoDataSourcesExample.java
+5
-3
ConfigurableDataSourceExampleTests.java
...rk/boot/docs/jdbc/ConfigurableDataSourceExampleTests.java
+1
-1
No files found.
spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc
View file @
99a45bde
...
@@ -1688,19 +1688,6 @@ username, and the pool size, these settings are bound automatically before the
...
@@ -1688,19 +1688,6 @@ username, and the pool size, these settings are bound automatically before the
(so the relevant sub-set of `spring.datasource.*` can still be used with your custom
(so the relevant sub-set of `spring.datasource.*` can still be used with your custom
configuration).
configuration).
You can apply the same principle if you configure a custom JNDI `DataSource`, as shown in
the following example:
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----
@Bean(destroyMethod="")
@ConfigurationProperties(prefix="app.datasource")
public DataSource dataSource() throws Exception {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
return dataSourceLookup.getDataSource("java:comp/env/jdbc/YourDS");
}
----
Spring Boot also provides a utility builder class, called `DataSourceBuilder`, that can
Spring Boot also provides a utility builder class, called `DataSourceBuilder`, that can
be used to create one of the standard data sources (if it is on the classpath). The
be used to create one of the standard data sources (if it is on the classpath). The
builder can detect the one to use based on what's available on the classpath. It also
builder can detect the one to use based on what's available on the classpath. It also
...
@@ -1768,18 +1755,22 @@ include::{code-examples}/jdbc/ConfigurableDataSourceExample.java[tag=configurati
...
@@ -1768,18 +1755,22 @@ include::{code-examples}/jdbc/ConfigurableDataSourceExample.java[tag=configurati
----
----
This setup puts you _in sync_ with what Spring Boot does for you by default, except that
This setup puts you _in sync_ with what Spring Boot does for you by default, except that
a dedicated connection pool is chosen (in code) and its settings are exposed in the
same
a dedicated connection pool is chosen (in code) and its settings are exposed in the
namespace. Because `DataSourceProperties` is taking care of the `url`/`jdbcUrl`
`app.datasource.configuration` sub namespace. Because `DataSourceProperties` is taking
translation for you, you can configure it as follows:
care of the `url`/`jdbcUrl`
translation for you, you can configure it as follows:
[source,properties,indent=0]
[source,properties,indent=0]
----
----
app.datasource.url=jdbc:mysql://localhost/test
app.datasource.url=jdbc:mysql://localhost/test
app.datasource.username=dbuser
app.datasource.username=dbuser
app.datasource.password=dbpass
app.datasource.password=dbpass
app.datasource.maximum-pool-size=30
app.datasource.
configuration.
maximum-pool-size=30
----
----
TIP: Spring Boot will expose Hikari-specific settings to `spring.datasource.hikari`. This
example uses a more generic `configuration` sub namespace as the example does not support
multiple datasource implementations.
NOTE: Because your custom configuration chooses to go with Hikari, `app.datasource.type`
NOTE: Because your custom configuration chooses to go with Hikari, `app.datasource.type`
has no effect. In practice, the builder is initialized with whatever value you
has no effect. In practice, the builder is initialized with whatever value you
might set there and then overridden by the call to `.type()`.
might set there and then overridden by the call to `.type()`.
...
@@ -1815,10 +1806,12 @@ configure them as follows:
...
@@ -1815,10 +1806,12 @@ configure them as follows:
[source,properties,indent=0]
[source,properties,indent=0]
----
----
app.datasource.first.type=com.zaxxer.hikari.HikariDataSource
app.datasource.first.url=jdbc:mysql://localhost/first
app.datasource.first.maximum-pool-size=30
app.datasource.first.username=dbuser
app.datasource.first.password=dbpass
app.datasource.first.configuration.maximum-pool-size=30
app.datasource.second.url=jdbc:mysql://localhost/
test
app.datasource.second.url=jdbc:mysql://localhost/
second
app.datasource.second.username=dbuser
app.datasource.second.username=dbuser
app.datasource.second.password=dbpass
app.datasource.second.password=dbpass
app.datasource.second.max-total=30
app.datasource.second.max-total=30
...
@@ -1833,7 +1826,8 @@ include::{code-examples}/jdbc/CompleteTwoDataSourcesExample.java[tag=configurati
...
@@ -1833,7 +1826,8 @@ include::{code-examples}/jdbc/CompleteTwoDataSourcesExample.java[tag=configurati
----
----
The preceding example configures two data sources on custom namespaces with the same
The preceding example configures two data sources on custom namespaces with the same
logic as Spring Boot would use in auto-configuration.
logic as Spring Boot would use in auto-configuration. Note that each `configuration` sub
namespace provides advanced settings based on the chosen implementation.
...
...
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/CompleteTwoDataSourcesExample.java
View file @
99a45bde
...
@@ -16,7 +16,8 @@
...
@@ -16,7 +16,8 @@
package
org
.
springframework
.
boot
.
docs
.
jdbc
;
package
org
.
springframework
.
boot
.
docs
.
jdbc
;
import
javax.sql.DataSource
;
import
com.zaxxer.hikari.HikariDataSource
;
import
org.apache.commons.dbcp2.BasicDataSource
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
...
@@ -48,9 +49,10 @@ public class CompleteTwoDataSourcesExample {
...
@@ -48,9 +49,10 @@ public class CompleteTwoDataSourcesExample {
@Bean
@Bean
@Primary
@Primary
@ConfigurationProperties
(
"app.datasource.first"
)
@ConfigurationProperties
(
"app.datasource.first.configuration"
)
public
DataSource
firstDataSource
()
{
public
HikariDataSource
firstDataSource
()
{
return
firstDataSourceProperties
().
initializeDataSourceBuilder
().
build
();
return
firstDataSourceProperties
().
initializeDataSourceBuilder
()
.
type
(
HikariDataSource
.
class
).
build
();
}
}
@Bean
@Bean
...
@@ -60,9 +62,10 @@ public class CompleteTwoDataSourcesExample {
...
@@ -60,9 +62,10 @@ public class CompleteTwoDataSourcesExample {
}
}
@Bean
@Bean
@ConfigurationProperties
(
"app.datasource.second"
)
@ConfigurationProperties
(
"app.datasource.second.configuration"
)
public
DataSource
secondDataSource
()
{
public
BasicDataSource
secondDataSource
()
{
return
secondDataSourceProperties
().
initializeDataSourceBuilder
().
build
();
return
secondDataSourceProperties
().
initializeDataSourceBuilder
()
.
type
(
BasicDataSource
.
class
).
build
();
}
}
// end::configuration[]
// end::configuration[]
...
...
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExample.java
View file @
99a45bde
...
@@ -49,7 +49,7 @@ public class ConfigurableDataSourceExample {
...
@@ -49,7 +49,7 @@ public class ConfigurableDataSourceExample {
}
}
@Bean
@Bean
@ConfigurationProperties
(
"app.datasource"
)
@ConfigurationProperties
(
"app.datasource
.configuration
"
)
public
HikariDataSource
dataSource
(
DataSourceProperties
properties
)
{
public
HikariDataSource
dataSource
(
DataSourceProperties
properties
)
{
return
properties
.
initializeDataSourceBuilder
().
type
(
HikariDataSource
.
class
)
return
properties
.
initializeDataSourceBuilder
().
type
(
HikariDataSource
.
class
)
.
build
();
.
build
();
...
...
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/SimpleTwoDataSourcesExample.java
View file @
99a45bde
...
@@ -18,6 +18,7 @@ package org.springframework.boot.docs.jdbc;
...
@@ -18,6 +18,7 @@ package org.springframework.boot.docs.jdbc;
import
javax.sql.DataSource
;
import
javax.sql.DataSource
;
import
com.zaxxer.hikari.HikariDataSource
;
import
org.apache.commons.dbcp2.BasicDataSource
;
import
org.apache.commons.dbcp2.BasicDataSource
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
;
...
@@ -51,9 +52,10 @@ public class SimpleTwoDataSourcesExample {
...
@@ -51,9 +52,10 @@ public class SimpleTwoDataSourcesExample {
@Bean
@Bean
@Primary
@Primary
@ConfigurationProperties
(
"app.datasource.first"
)
@ConfigurationProperties
(
"app.datasource.first.configuration"
)
public
DataSource
firstDataSource
()
{
public
HikariDataSource
firstDataSource
()
{
return
firstDataSourceProperties
().
initializeDataSourceBuilder
().
build
();
return
firstDataSourceProperties
().
initializeDataSourceBuilder
()
.
type
(
HikariDataSource
.
class
).
build
();
}
}
@Bean
@Bean
...
...
spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExampleTests.java
View file @
99a45bde
...
@@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
...
@@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith
(
SpringRunner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
properties
=
{
@SpringBootTest
(
properties
=
{
"app.datasource.url=jdbc:h2:mem:configurable;DB_CLOSE_DELAY=-1"
,
"app.datasource.url=jdbc:h2:mem:configurable;DB_CLOSE_DELAY=-1"
,
"app.datasource.maximum-pool-size=42"
})
"app.datasource.
configuration.
maximum-pool-size=42"
})
@Import
(
ConfigurableDataSourceExample
.
ConfigurableDataSourceConfiguration
.
class
)
@Import
(
ConfigurableDataSourceExample
.
ConfigurableDataSourceConfiguration
.
class
)
public
class
ConfigurableDataSourceExampleTests
{
public
class
ConfigurableDataSourceExampleTests
{
...
...
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