Connecting to multiple database in spring boot
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Spring Boot can connect to multiple databases, but only if each persistence module is configured explicitly. Most issues come from ambiguous bean wiring or repository packages that accidentally point to the wrong datasource. A maintainable setup defines separate properties, datasources, entity managers, and transaction managers for each database.
Define Separate Configuration Properties
Do not overload one default datasource block. Use dedicated prefixes per database.
Clear prefixes make wiring intent explicit and easier to debug.
Create Distinct DataSource Beans
Bind each property group to a separate datasource bean.
Use explicit bean names and qualifiers to avoid ambiguous injection.
Split JPA Configuration by Module
Each datasource should have its own entity manager and transaction manager, then repositories must be bound to the correct pair.
Create a parallel config for audit packages with distinct refs.
Use Explicit Transaction Managers in Services
When methods target one database, annotate with the matching transaction manager name.
This prevents accidental writes to wrong datasource under default transaction manager.
Keep Package Boundaries Strict
Organize entities and repositories by database ownership:
com.example.main.entityandcom.example.main.repocom.example.audit.entityandcom.example.audit.repo
Loose package boundaries are a common cause of cross-database repository wiring bugs.
Test Wiring with Realistic Integration Tests
Unit tests are not enough for multi-database wiring. Add integration tests that verify each repository writes to intended database.
A practical approach is running two test databases with Testcontainers and checking row presence separately after service calls.
This catches misconfiguration before deployment.
Observe Datasource Health Separately
Expose and monitor health and pool metrics per datasource so incidents are isolated quickly.
If one database slows down, dashboards should show which pool is saturated and which remains healthy.
Migration and Ownership Discipline
Keep migration scripts separate per datasource. Each schema should have independent migration history and ownership.
During release, apply migrations with datasource-specific config and log target datasource names. This avoids accidental schema changes in wrong database.
Consider Read and Write Routing Explicitly
Some systems use one datasource for writes and another for read replicas. Keep routing logic explicit in service design and never hide it behind ambiguous defaults.
If read replicas are eventually consistent, document freshness expectations so callers do not assume immediate read-after-write behavior.
Common Pitfalls
- Using one default datasource config in multi-database applications.
- Letting repositories scan overlapping packages.
- Omitting transaction manager names in multi-database service methods.
- Injecting datasource beans without qualifiers.
- Skipping integration tests that validate actual datasource routing.
Summary
- Multi-database Boot setup requires explicit, separate wiring for each datasource.
- Define dedicated properties, datasources, entity managers, and transaction managers.
- Keep repository packages strictly separated by database ownership.
- Use named transaction managers in service methods.
- Add integration and observability checks to keep routing correct in production.

