You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you need to connect to multiple databases, the configuration is different. You need to create a `@Configuration` class for each database connection, extending class `LcR2dbcEntityOperationsBuilder`. Instead of declaring `@EnableR2dbcRepositories` on your application, you will declare it to each configuration class.
public class BookConfig extends LcR2dbcEntityOperationsBuilder {
215
+
216
+
@Bean
217
+
@Qualifier("bookDatabaseConnectionFactory")
218
+
public ConnectionFactory bookDatabaseConnectionFactory(@Value("${database.book}") String databaseUrl) {
219
+
return ConnectionFactories.get(databaseUrl);
220
+
}
221
+
222
+
@Bean
223
+
@Qualifier("bookOperations")
224
+
public LcR2dbcEntityTemplate bookOperations(@Qualifier("bookDatabaseConnectionFactory") ConnectionFactory connectionFactory) {
225
+
return buildEntityOperations(connectionFactory);
226
+
}
227
+
228
+
}
229
+
```
230
+
231
+
- define a bean to create a `ConnectionFactory` (here we get a url from the application configuration, but you can create it in another way)
232
+
- define a bean `LcR2dbcEntityTemplate` with the connection factory
233
+
- add the annotation `@EnableR2dbcRepositories` with the packages containing the repositories that will use this database, and the attribute `entityOperationsRef` set to the qualifier of the `LcR2dbcEntityTemplate` bean
234
+
235
+
A complete example illustrating a Spring Boot application connecting to different database is available in the repository [lc-spring-data-r2dbc-sample](https://github.com/lecousin/lc-spring-data-r2dbc-sample).
236
+
205
237
## JUnit 5
206
238
207
239
For your tests, using JUnit 5, you can use the annotation `@DataR2dbcTest` provided by Spring, and add the annotation `@EnableR2dbcRepositories(repositoryFactoryBeanClass = LcR2dbcRepositoryFactoryBean.class)`.
@@ -212,7 +244,7 @@ In order to make sure the initializer is launched before any test class is loade
Copy file name to clipboardExpand all lines: src/README.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -202,6 +202,38 @@ entities:
202
202
- SubEntity3
203
203
```
204
204
205
+
### Multiple databases
206
+
207
+
If you need to connect to multiple databases, the configuration is different. You need to create a `@Configuration` class for each database connection, extending class `LcR2dbcEntityOperationsBuilder`. Instead of declaring `@EnableR2dbcRepositories` on your application, you will declare it to each configuration class.
public class BookConfig extends LcR2dbcEntityOperationsBuilder {
215
+
216
+
@Bean
217
+
@Qualifier("bookDatabaseConnectionFactory")
218
+
public ConnectionFactory bookDatabaseConnectionFactory(@Value("${database.book}") String databaseUrl) {
219
+
return ConnectionFactories.get(databaseUrl);
220
+
}
221
+
222
+
@Bean
223
+
@Qualifier("bookOperations")
224
+
public LcR2dbcEntityTemplate bookOperations(@Qualifier("bookDatabaseConnectionFactory") ConnectionFactory connectionFactory) {
225
+
return buildEntityOperations(connectionFactory);
226
+
}
227
+
228
+
}
229
+
```
230
+
231
+
- define a bean to create a `ConnectionFactory` (here we get a url from the application configuration, but you can create it in another way)
232
+
- define a bean `LcR2dbcEntityTemplate` with the connection factory
233
+
- add the annotation `@EnableR2dbcRepositories` with the packages containing the repositories that will use this database, and the attribute `entityOperationsRef` set to the qualifier of the `LcR2dbcEntityTemplate` bean
234
+
235
+
A complete example illustrating a Spring Boot application connecting to different database is available in the repository [lc-spring-data-r2dbc-sample](https://github.com/lecousin/lc-spring-data-r2dbc-sample).
236
+
205
237
## JUnit 5
206
238
207
239
For your tests, using JUnit 5, you can use the annotation `@DataR2dbcTest` provided by Spring, and add the annotation `@EnableR2dbcRepositories(repositoryFactoryBeanClass = LcR2dbcRepositoryFactoryBean.class)`.
0 commit comments