Skip to content

Commit 9dac803

Browse files
committed
v0.6.0 - support for multiple database connections
1 parent 220400e commit 9dac803

File tree

10 files changed

+80
-16
lines changed

10 files changed

+80
-16
lines changed

README.md

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
net.lecousin.reactive-data-relational
44
[![Maven Central](https://img.shields.io/maven-central/v/net.lecousin.reactive-data-relational/core.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.lecousin.reactive-data-relational%22%20AND%20a%3A%22core%22)
5-
[![Javadoc](https://img.shields.io/badge/javadoc-0.5.2-brightgreen.svg)](https://www.javadoc.io/doc/net.lecousin.reactive-data-relational/core/0.5.2)
5+
[![Javadoc](https://img.shields.io/badge/javadoc-0.6.0-brightgreen.svg)](https://www.javadoc.io/doc/net.lecousin.reactive-data-relational/core/0.6.0)
66
![Build status](https://github.com/lecousin/lc-spring-data-r2dbc/actions/workflows/maven.yml/badge.svg?branch=master)
77
[![Codecov](https://codecov.io/gh/lecousin/lc-spring-data-r2dbc/branch/master/graph/badge.svg)](https://codecov.io/gh/lecousin/lc-spring-data-r2dbc/branch/master)
88

@@ -60,13 +60,13 @@ Maven
6060
<dependency>
6161
<groupId>net.lecousin.reactive-data-relational</groupId>
6262
<artifactId>h2</artifactId>
63-
<version>0.5.2</version>
63+
<version>0.6.0</version>
6464
</dependency>
6565
```
6666

6767
Gradle
6868
```groovy
69-
implementation group: 'net.lecousin.reactive-data-relational', name: 'h2', version: '0.5.2'
69+
implementation group: 'net.lecousin.reactive-data-relational', name: 'h2', version: '0.6.0'
7070
```
7171

7272
### Postgres
@@ -76,13 +76,13 @@ Maven
7676
<dependency>
7777
<groupId>net.lecousin.reactive-data-relational</groupId>
7878
<artifactId>postgres</artifactId>
79-
<version>0.5.2</version>
79+
<version>0.6.0</version>
8080
</dependency>
8181
```
8282

8383
Gradle
8484
```groovy
85-
implementation group: 'net.lecousin.reactive-data-relational', name: 'postgres', version: '0.5.2'
85+
implementation group: 'net.lecousin.reactive-data-relational', name: 'postgres', version: '0.6.0'
8686
```
8787

8888
### MySql
@@ -92,13 +92,13 @@ Maven
9292
<dependency>
9393
<groupId>net.lecousin.reactive-data-relational</groupId>
9494
<artifactId>mysql</artifactId>
95-
<version>0.5.2</version>
95+
<version>0.6.0</version>
9696
</dependency>
9797
```
9898

9999
Gradle
100100
```groovy
101-
implementation group: 'net.lecousin.reactive-data-relational', name: 'mysql', version: '0.5.2'
101+
implementation group: 'net.lecousin.reactive-data-relational', name: 'mysql', version: '0.6.0'
102102
```
103103

104104
## Spring Boot configuration
@@ -202,6 +202,38 @@ entities:
202202
- SubEntity3
203203
```
204204
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.
208+
209+
Here is an example of such a configuration class:
210+
211+
```java
212+
@Configuration
213+
@EnableR2dbcRepositories(repositoryFactoryBeanClass = LcR2dbcRepositoryFactoryBean.class, basePackages = "com.example.book.dao.repository", entityOperationsRef = "bookOperations")
214+
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+
205237
## JUnit 5
206238

207239
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
212244
<dependency>
213245
<groupId>net.lecousin.reactive-data-relational</groupId>
214246
<artifactId>test-junit-5</artifactId>
215-
<version>0.5.2</version>
247+
<version>0.6.0</version>
216248
<scope>test</scope>
217249
</dependency>
218250
```

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>net.lecousin.reactive-data-relational</groupId>
55
<artifactId>parent</artifactId>
6-
<version>0.5.2</version>
6+
<version>0.6.0</version>
77
</parent>
88
<artifactId>core</artifactId>
99

h2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>net.lecousin.reactive-data-relational</groupId>
55
<artifactId>parent</artifactId>
6-
<version>0.5.2</version>
6+
<version>0.6.0</version>
77
</parent>
88
<artifactId>h2</artifactId>
99

jacoco-report-aggregate/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>net.lecousin.reactive-data-relational</groupId>
55
<artifactId>parent</artifactId>
6-
<version>0.5.2</version>
6+
<version>0.6.0</version>
77
</parent>
88
<artifactId>jacoco-report-aggregate</artifactId>
99
<packaging>pom</packaging>

mysql/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>net.lecousin.reactive-data-relational</groupId>
55
<artifactId>parent</artifactId>
6-
<version>0.5.2</version>
6+
<version>0.6.0</version>
77
</parent>
88
<artifactId>mysql</artifactId>
99

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>net.lecousin.reactive-data-relational</groupId>
44
<artifactId>parent</artifactId>
5-
<version>0.5.2</version>
5+
<version>0.6.0</version>
66
<packaging>pom</packaging>
77

88
<name>net.lecousin.reactive-data-relational aka lc-spring-data-r2dbc</name>

postgres/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>net.lecousin.reactive-data-relational</groupId>
55
<artifactId>parent</artifactId>
6-
<version>0.5.2</version>
6+
<version>0.6.0</version>
77
</parent>
88
<artifactId>postgres</artifactId>
99

src/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,38 @@ entities:
202202
- SubEntity3
203203
```
204204
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.
208+
209+
Here is an example of such a configuration class:
210+
211+
```java
212+
@Configuration
213+
@EnableR2dbcRepositories(repositoryFactoryBeanClass = LcR2dbcRepositoryFactoryBean.class, basePackages = "com.example.book.dao.repository", entityOperationsRef = "bookOperations")
214+
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+
205237
## JUnit 5
206238

207239
For your tests, using JUnit 5, you can use the annotation `@DataR2dbcTest` provided by Spring, and add the annotation `@EnableR2dbcRepositories(repositoryFactoryBeanClass = LcR2dbcRepositoryFactoryBean.class)`.

test-junit-5/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>net.lecousin.reactive-data-relational</groupId>
55
<artifactId>parent</artifactId>
6-
<version>0.5.2</version>
6+
<version>0.6.0</version>
77
</parent>
88
<artifactId>test-junit-5</artifactId>
99

test-spring-boot/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>net.lecousin.reactive-data-relational</groupId>
55
<artifactId>parent</artifactId>
6-
<version>0.5.2</version>
6+
<version>0.6.0</version>
77
</parent>
88
<artifactId>test-spring-boot</artifactId>
99

0 commit comments

Comments
 (0)