|
54 | 54 | pool-size: 30
|
55 | 55 | ----
|
56 | 56 |
|
57 |
| -However, there is a catch. |
58 |
| -Because the actual type of the connection pool is not exposed, no keys are generated in the metadata for your custom `DataSource` and no completion is available in your IDE (because the `DataSource` interface exposes no properties). |
59 |
| -Also, if you happen to have Hikari on the classpath, this basic setup does not work, because Hikari has no `url` property (but does have a `jdbcUrl` property). |
60 |
| -In that case, you must rewrite your configuration as follows: |
| 57 | +However, there is a catch due to the method's `DataSource` return type. |
| 58 | +This hides the actual type of the connection pool so no configuration property metadata is generated for your custom `DataSource` and no auto-completion is available in your IDE. |
| 59 | +To address this problem, use the builder's `type(Class)` method to specify the type of `DataSource` to be built and update the method's return type. |
| 60 | +For example, the following shows how to create a `HikariDataSource` with `DataSourceBuilder`: |
| 61 | + |
| 62 | +include-code::simple/MyDataSourceConfiguration[] |
| 63 | + |
| 64 | +Unfortunately, this basic setup does not work because Hikari has no `url` property. |
| 65 | +Instead, it has a `jdbc-url` property which means that you must rewrite your configuration as follows: |
61 | 66 |
|
62 | 67 | [configprops%novalidate,yaml]
|
63 | 68 | ----
|
|
69 | 74 | pool-size: 30
|
70 | 75 | ----
|
71 | 76 |
|
72 |
| -You can fix that by forcing the connection pool to use and return a dedicated implementation rather than `DataSource`. |
73 |
| -You cannot change the implementation at runtime, but the list of options will be explicit. |
74 |
| - |
75 |
| -The following example shows how to create a `HikariDataSource` with `DataSourceBuilder`: |
76 |
| - |
77 |
| -include-code::simple/MyDataSourceConfiguration[] |
78 |
| - |
79 |
| -You can even go further by leveraging what `DataSourceProperties` does for you -- that is, by providing a default embedded database with a sensible username and password if no URL is provided. |
80 |
| -You can easily initialize a `DataSourceBuilder` from the state of any `DataSourceProperties` object, so you could also inject the DataSource that Spring Boot creates automatically. |
81 |
| -However, that would split your configuration into two namespaces: `url`, `username`, `password`, `type`, and `driver` on `spring.datasource` and the rest on your custom namespace (`app.datasource`). |
82 |
| -To avoid that, you can redefine a custom `DataSourceProperties` on your custom namespace, as shown in the following example: |
| 77 | +To address this problem, make use of `DataSourceProperties` which will handle the `url` to `jdbc-url` translation for you. |
| 78 | +You can initialize a `DataSourceBuilder` from the state of any `DataSourceProperties` object using its `initializeDataSourceBuilder()` method. |
| 79 | +You could inject the `DataSourceProperties` that Spring Boot creates automatically, however, that would split your configuration across `+spring.datasource.*+` and `+app.datasource.*+`. |
| 80 | +To avoid this, define a custom `DataSourceProperties` with a custom configuration properties prefix, as shown in the following example: |
83 | 81 |
|
84 | 82 | include-code::configurable/MyDataSourceConfiguration[]
|
85 | 83 |
|
86 |
| -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 `app.datasource.configuration` sub namespace. |
87 |
| -Because `DataSourceProperties` is taking care of the `url`/`jdbcUrl` translation for you, you can configure it as follows: |
| 84 | +This setup is equivalent to what Spring Boot does for you by default, except that the pool's type is specified in code and its settings are exposed as `app.datasource.configuration.*` properties. |
| 85 | +`DataSourceProperties` takes care of the `url` to `jdbc-url` translation, so you can configure it as follows: |
88 | 86 |
|
89 | 87 | [configprops%novalidate,yaml]
|
90 | 88 | ----
|
|
97 | 95 | maximum-pool-size: 30
|
98 | 96 | ----
|
99 | 97 |
|
| 98 | +Note that, as the custom configuration specifies in code that Hikari should be used, `app.datasource.type` will have no effect. |
| 99 | + |
| 100 | +As described in xref:reference:data/sql.adoc#data.sql.datasource.connection-pool[], `DataSourceBuilder` supports several different connection pools. |
| 101 | +To use a pool other than Hikari, add it to the classpath, use the `type(Class)` method to specify the pool class to use, and update the `@Bean` method's return type to match. |
| 102 | +This will also provide you with configuration property metadata for the specific connection pool that you've chosen. |
| 103 | + |
100 | 104 | TIP: Spring Boot will expose Hikari-specific settings to `spring.datasource.hikari`.
|
101 | 105 | This example uses a more generic `configuration` sub namespace as the example does not support multiple datasource implementations.
|
102 | 106 |
|
103 |
| -NOTE: Because your custom configuration chooses to go with Hikari, `app.datasource.type` has no effect. |
104 |
| -In practice, the builder is initialized with whatever value you might set there and then overridden by the call to `.type()`. |
105 |
| - |
106 |
| -See xref:reference:data/sql.adoc#data.sql.datasource[] in the "`Spring Boot Features`" section and the {code-spring-boot-autoconfigure-src}/jdbc/DataSourceAutoConfiguration.java[`DataSourceAutoConfiguration`] class for more details. |
| 107 | +See xref:reference:data/sql.adoc#data.sql.datasource[] and the {code-spring-boot-autoconfigure-src}/jdbc/DataSourceAutoConfiguration.java[`DataSourceAutoConfiguration`] class for more details. |
107 | 108 |
|
108 | 109 |
|
109 | 110 |
|
@@ -149,6 +150,9 @@ include-code::MyCompleteAdditionalDataSourceConfiguration[]
|
149 | 150 | The preceding example configures the additional data source with the same logic as Spring Boot would use in auto-configuration.
|
150 | 151 | Note that the `app.datasource.configuration.*` properties provide advanced settings based on the chosen implementation.
|
151 | 152 |
|
| 153 | +As with xref:how-to:data-access.adoc#howto.data-access.configure-custom-datasource[configuring a single custom `DataSource`], the type of one or both of the `DataSource` beans can be customized using the `type(Class)` method on `DataSourceBuilder`. |
| 154 | +See xref:reference:data/sql.adoc#data.sql.datasource.connection-pool[] for details of the supported types. |
| 155 | + |
152 | 156 |
|
153 | 157 |
|
154 | 158 | [[howto.data-access.spring-data-repositories]]
|
|
0 commit comments