Skip to content

Commit 99a45bd

Browse files
committed
Fix datasource prefix in multiple-datasource configuration doc
Closes spring-projectsgh-13195
1 parent 0fedf8d commit 99a45bd

File tree

5 files changed

+32
-33
lines changed

5 files changed

+32
-33
lines changed

Diff for: spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

+15-21
Original file line numberDiff line numberDiff line change
@@ -1688,19 +1688,6 @@ username, and the pool size, these settings are bound automatically before the
16881688
(so the relevant sub-set of `spring.datasource.*` can still be used with your custom
16891689
configuration).
16901690

1691-
You can apply the same principle if you configure a custom JNDI `DataSource`, as shown in
1692-
the following example:
1693-
1694-
[source,java,indent=0,subs="verbatim,quotes,attributes"]
1695-
----
1696-
@Bean(destroyMethod="")
1697-
@ConfigurationProperties(prefix="app.datasource")
1698-
public DataSource dataSource() throws Exception {
1699-
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
1700-
return dataSourceLookup.getDataSource("java:comp/env/jdbc/YourDS");
1701-
}
1702-
----
1703-
17041691
Spring Boot also provides a utility builder class, called `DataSourceBuilder`, that can
17051692
be used to create one of the standard data sources (if it is on the classpath). The
17061693
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
17681755
----
17691756

17701757
This setup puts you _in sync_ with what Spring Boot does for you by default, except that
1771-
a dedicated connection pool is chosen (in code) and its settings are exposed in the same
1772-
namespace. Because `DataSourceProperties` is taking care of the `url`/`jdbcUrl`
1773-
translation for you, you can configure it as follows:
1758+
a dedicated connection pool is chosen (in code) and its settings are exposed in the
1759+
`app.datasource.configuration` sub namespace. Because `DataSourceProperties` is taking
1760+
care of the `url`/`jdbcUrl` translation for you, you can configure it as follows:
17741761

17751762
[source,properties,indent=0]
17761763
----
17771764
app.datasource.url=jdbc:mysql://localhost/test
17781765
app.datasource.username=dbuser
17791766
app.datasource.password=dbpass
1780-
app.datasource.maximum-pool-size=30
1767+
app.datasource.configuration.maximum-pool-size=30
17811768
----
17821769

1770+
TIP: Spring Boot will expose Hikari-specific settings to `spring.datasource.hikari`. This
1771+
example uses a more generic `configuration` sub namespace as the example does not support
1772+
multiple datasource implementations.
1773+
17831774
NOTE: Because your custom configuration chooses to go with Hikari, `app.datasource.type`
17841775
has no effect. In practice, the builder is initialized with whatever value you
17851776
might set there and then overridden by the call to `.type()`.
@@ -1815,10 +1806,12 @@ configure them as follows:
18151806

18161807
[source,properties,indent=0]
18171808
----
1818-
app.datasource.first.type=com.zaxxer.hikari.HikariDataSource
1819-
app.datasource.first.maximum-pool-size=30
1809+
app.datasource.first.url=jdbc:mysql://localhost/first
1810+
app.datasource.first.username=dbuser
1811+
app.datasource.first.password=dbpass
1812+
app.datasource.first.configuration.maximum-pool-size=30
18201813
1821-
app.datasource.second.url=jdbc:mysql://localhost/test
1814+
app.datasource.second.url=jdbc:mysql://localhost/second
18221815
app.datasource.second.username=dbuser
18231816
app.datasource.second.password=dbpass
18241817
app.datasource.second.max-total=30
@@ -1833,7 +1826,8 @@ include::{code-examples}/jdbc/CompleteTwoDataSourcesExample.java[tag=configurati
18331826
----
18341827

18351828
The preceding example configures two data sources on custom namespaces with the same
1836-
logic as Spring Boot would use in auto-configuration.
1829+
logic as Spring Boot would use in auto-configuration. Note that each `configuration` sub
1830+
namespace provides advanced settings based on the chosen implementation.
18371831

18381832

18391833

Diff for: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/CompleteTwoDataSourcesExample.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
package org.springframework.boot.docs.jdbc;
1818

19-
import javax.sql.DataSource;
19+
import com.zaxxer.hikari.HikariDataSource;
20+
import org.apache.commons.dbcp2.BasicDataSource;
2021

2122
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
2223
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -48,9 +49,10 @@ public DataSourceProperties firstDataSourceProperties() {
4849

4950
@Bean
5051
@Primary
51-
@ConfigurationProperties("app.datasource.first")
52-
public DataSource firstDataSource() {
53-
return firstDataSourceProperties().initializeDataSourceBuilder().build();
52+
@ConfigurationProperties("app.datasource.first.configuration")
53+
public HikariDataSource firstDataSource() {
54+
return firstDataSourceProperties().initializeDataSourceBuilder()
55+
.type(HikariDataSource.class).build();
5456
}
5557

5658
@Bean
@@ -60,9 +62,10 @@ public DataSourceProperties secondDataSourceProperties() {
6062
}
6163

6264
@Bean
63-
@ConfigurationProperties("app.datasource.second")
64-
public DataSource secondDataSource() {
65-
return secondDataSourceProperties().initializeDataSourceBuilder().build();
65+
@ConfigurationProperties("app.datasource.second.configuration")
66+
public BasicDataSource secondDataSource() {
67+
return secondDataSourceProperties().initializeDataSourceBuilder()
68+
.type(BasicDataSource.class).build();
6669
}
6770
// end::configuration[]
6871

Diff for: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public DataSourceProperties dataSourceProperties() {
4949
}
5050

5151
@Bean
52-
@ConfigurationProperties("app.datasource")
52+
@ConfigurationProperties("app.datasource.configuration")
5353
public HikariDataSource dataSource(DataSourceProperties properties) {
5454
return properties.initializeDataSourceBuilder().type(HikariDataSource.class)
5555
.build();

Diff for: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jdbc/SimpleTwoDataSourcesExample.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import javax.sql.DataSource;
2020

21+
import com.zaxxer.hikari.HikariDataSource;
2122
import org.apache.commons.dbcp2.BasicDataSource;
2223

2324
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
@@ -51,9 +52,10 @@ public DataSourceProperties firstDataSourceProperties() {
5152

5253
@Bean
5354
@Primary
54-
@ConfigurationProperties("app.datasource.first")
55-
public DataSource firstDataSource() {
56-
return firstDataSourceProperties().initializeDataSourceBuilder().build();
55+
@ConfigurationProperties("app.datasource.first.configuration")
56+
public HikariDataSource firstDataSource() {
57+
return firstDataSourceProperties().initializeDataSourceBuilder()
58+
.type(HikariDataSource.class).build();
5759
}
5860

5961
@Bean

Diff for: spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/jdbc/ConfigurableDataSourceExampleTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
@RunWith(SpringRunner.class)
4141
@SpringBootTest(properties = {
4242
"app.datasource.url=jdbc:h2:mem:configurable;DB_CLOSE_DELAY=-1",
43-
"app.datasource.maximum-pool-size=42" })
43+
"app.datasource.configuration.maximum-pool-size=42" })
4444
@Import(ConfigurableDataSourceExample.ConfigurableDataSourceConfiguration.class)
4545
public class ConfigurableDataSourceExampleTests {
4646

0 commit comments

Comments
 (0)