This project demonstrates how to dynamically switch between multiple databases at runtime using Spring Boot, JPA, and a custom AbstractRoutingDataSource.
A MainMember belongs to a Team, and each team has its own separate database. When a request comes in with a member code:
- The app looks up the member from the main database.
- It extracts DB credentials from the member's
Team. - It dynamically registers and switches to the appropriate branch database.
- Then it queries data (like
GameScore) from that branch DB. - Finally, it clears the context after the request is processed.
com.example.dynamicdb ├── main # Main DB domain & repository │ ├── domain # MainMember, Team entities │ ├── repository # MainMemberRepository, TeamRepository │ └── service # MainMemberService ├── branch # Branch DB domain & repository │ ├── domain # BranchMember, GameScore entities │ ├── repository # BranchMemberRepository, GameScoreRepository │ ├── service # BranchMemberService, GameScoreService │ └── controller # GameScoreController ├── config │ └── db # Routing datasource, context holder, config │ ├── DatabaseContextHolder.java │ ├── DynamicRoutingDataSource.java │ ├── RoutingDataSourceConfig.java │ ├── MainDbConfig.java │ ├── BranchDbConfig.java │ └── BranchDatabaseService.java └── DynamicDbApplication.java # Main application class
- Spring Boot 3+
- Spring Data JPA (Hibernate)
- MySQL 8
- Docker & Docker Compose
Make sure you're using JDK 17.
./gradlew clean buildThis creates the JAR at
build/libs/dynamic-db-0.0.1-SNAPSHOT.jar.
docker-compose up --buildThis spins up:
- ✅
main-db(port: 13305) - ✅
real-madrid-db(port: 13306) - ✅
manchester-city-db(port: 13307) - ✅
dynamic-db-app(Spring Boot API on port: 8080)
Each database is initialized using the SQL files in the /init directory.