Skip to content

Commit cf2fee8

Browse files
committed
๐Ÿฎ FlightTracker now supports DataSourceRouting for READ and WRITE operations
1 parent 9c0bebb commit cf2fee8

File tree

14 files changed

+17342
-29
lines changed

14 files changed

+17342
-29
lines changed

โ€ŽREADME.mdโ€Ž

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,52 @@ To enable automatic badge updates and coverage reports, ensure the following Git
144144
- "Read and write permissions"
145145
- "Allow GitHub Actions to create and approve pull requests"
146146

147+
## Read Write Datasource Routing
148+
149+
![Read Write Deployment](docs/arch_diagram_datasource_read_write.png)
150+
151+
The application supports read-write splitting for database operations. This feature is disabled by default but can be enabled through configuration.
152+
153+
### Configuration
154+
155+
```yaml
156+
spring:
157+
datasource:
158+
writer:
159+
jdbcUrl: jdbc:postgresql://localhost:5432/flighttracker
160+
username: flighttracker
161+
password: flighttracker
162+
driverClassName: org.postgresql.Driver
163+
type: com.zaxxer.hikari.HikariDataSource
164+
reader:
165+
jdbcUrl: jdbc:postgresql://localhost:5433/flighttracker
166+
username: flighttracker
167+
password: flighttracker
168+
driverClassName: org.postgresql.Driver
169+
type: com.zaxxer.hikari.HikariDataSource
170+
171+
app:
172+
read-write-routing:
173+
enabled: false # Set to true to enable read-write splitting
174+
```
175+
176+
### Important Notes
177+
178+
1. When enabled, you must configure both write and read data sources
179+
2. The routing is based on Spring's `@Transactional` annotation:
180+
- Read operations: Use `@Transactional(readOnly = true)`
181+
- Write operations: Use `@Transactional` or `@Transactional(readOnly = false)`
182+
183+
![Read Write DataSource Routing](docs/arch_diagram_datasource_routing.png)
184+
185+
3. If read-write splitting is enabled but not properly configured, the application will fail to start
186+
4. For development and testing, it's recommended to keep this feature disabled
187+
5. The routing is handled by:
188+
- `DatasourceConfig`: Configures the data sources and routing
189+
- `RoutingDataSource`: Routes requests to the appropriate data source
190+
- `ReadWriteRoutingAspect`: Sets the context based on transaction type
191+
- `DbContextHolder`: Thread-local holder for the current context
192+
147193
## Project Structure
148194

149195
```
@@ -171,6 +217,12 @@ To enable automatic badge updates and coverage reports, ensure the following Git
171217
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Mediator.java
172218
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ SpringMediator.java
173219
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ infrastructure/
220+
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ datasource/
221+
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ DbContextHolder.java
222+
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ReadWriteRoutingAspect.java
223+
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ReadWriteRoutingProperties.java
224+
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ RoutingDataSource.java
225+
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ DatasourceConfig.java
174226
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ KafkaConfig.java
175227
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ OpenApiConfig.java
176228
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ flightdata/

0 commit comments

Comments
ย (0)