How to use 2 or more databases with spring?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Using more than one database in Spring is mostly a wiring problem: you need separate connection beans and a clear rule for which repositories and transactions belong to which database. The difficult part is not creating two DataSource objects, but keeping the boundaries explicit so the wrong repository does not silently talk to the wrong database.
Decide What “Multiple Databases” Means
There are several different scenarios that people call multi-database support:
- one application reads and writes two unrelated databases
- one database is primary and another is used for audit data
- one database is legacy and one is new
- read and write traffic are split across separate data sources
The Spring configuration pattern is similar, but the transaction strategy may differ a lot. A local transaction manager for database A does not magically coordinate with database B.
Start with Separate Properties
A clean setup begins with separate configuration prefixes:
Using distinct namespaces keeps the configuration understandable and avoids confusion with Spring Boot's default single-datasource conventions.
Define Separate DataSource Beans
Create one DataSource bean per database:
@Primary matters because many Spring components expect one default candidate. Without it, you often run into ambiguous-bean errors.
Separate JPA Repositories by Database
If you are using Spring Data JPA, each database should usually have:
- its own repository package
- its own entity package
- its own entity manager factory
- its own transaction manager
Example for the primary database:
You repeat the pattern for the second database with different bean names and package paths.
Service Layer Usage
At the service layer, inject only the repositories that belong to the operation you are performing and specify the correct transaction manager when necessary:
Being explicit here is often better than relying on default behavior.
Think Carefully About Cross-Database Consistency
The biggest design mistake is assuming that using two datasources is only a Spring configuration issue. If one business action must update both databases atomically, that is an architectural concern.
In many applications, the better design is:
- keep local transactions separate
- use messaging or an outbox pattern
- avoid pretending that two unrelated databases form one simple unit of work
Distributed transactions exist, but they add complexity and are usually not the first answer for ordinary application design.
Common Pitfalls
- Putting repositories for both databases in the same scanned package makes ownership unclear.
- Forgetting
@Primaryleads to ambiguous bean resolution. - Using one transaction manager everywhere can make the wrong database participate in a transaction.
- Mixing entities from different databases in one persistence unit creates confusing runtime failures.
- Treating cross-database consistency as just a configuration problem usually leads to fragile designs.
Summary
- Each database should have its own
DataSourceand usually its own repository and transaction configuration. - Use separate property prefixes so the connection settings stay explicit.
- With JPA, keep repository packages and entity packages clearly separated per database.
- Use explicit transaction managers when a service interacts with more than one persistence layer.
- Multi-database support is partly a Spring problem and partly an architecture problem.
Related reading
- How to use aggregate functions in Amazon Dynamodb
- How to use an existing database with an Android application
- How to use an existing database with an Android application
- How to use an init container to check if MySQL is ready for connections?
- How to use Ack or Nack in Spring AMQP
- How to use Apache kafka with Spring mvc ? Is it possible?
- How to use async Mysql query with PHP PDO
- How to use auto increment for primary key id in dynamodb

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.