Merge branch '2.0.x'

pull/14824/merge
Stephane Nicoll 6 years ago
commit ffe5e88d8e

@ -1731,19 +1731,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
@ -1811,18 +1798,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()`.
@ -1858,10 +1849,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
@ -1876,7 +1869,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.

@ -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 {
@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 {
} }
@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[]

@ -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();

@ -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 {
@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

@ -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 {

Loading…
Cancel
Save