Multiple data source and schema creation 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
Using multiple data sources in Spring Boot means leaving the default one-database convention and wiring each database explicitly. The main tasks are defining separate DataSource beans, wiring the matching JPA infrastructure for each one, and deciding how schemas should be created or migrated for each database independently.
Separate the Configuration Clearly
With one database, Spring Boot autoconfiguration can do most of the work. With multiple databases, clarity matters more than convenience, so each data source should have its own configuration prefix.
Example application.yml:
This keeps the two connection definitions separate instead of trying to overload the default spring.datasource properties for both.
Define Two DataSource Beans
Each database needs its own DataSource. One is usually marked @Primary so Spring knows which bean to prefer when no qualifier is supplied.
That gives Spring two explicit connection sources instead of one ambiguous global default.
Wire JPA Infrastructure Per Database
If both databases use JPA, each one needs its own entity manager factory and transaction manager, usually with separate entity packages.
You would create a similar configuration class for the reporting database. The separation of packages is important because it prevents one entity manager from accidentally scanning the other database's entities.
Schema Creation Should Be Explicit
With multiple databases, schema creation is usually clearer when managed explicitly with Flyway or Liquibase rather than relying on one global ddl-auto setting.
Why:
- each database may evolve independently,
- each schema may need different migration order or ownership,
- and automatic schema generation becomes harder to reason about once more than one entity manager is involved.
If you do use Hibernate schema generation, be deliberate about which entity manager owns which entities. Avoid one blanket global setting and assume it will do the right thing for both databases.
Common Pitfalls
- Defining multiple
DataSourcebeans without marking one as@Primarywhen the app expects a default. - Letting repositories or entities from one database be scanned by the wrong entity manager.
- Relying on one global schema-generation setting for several databases with different responsibilities.
- Mixing transactional work across databases without being explicit about which transaction manager is in use.
- Keeping all entities in one package, which makes multi-database separation harder to understand and maintain.
Summary
- Multiple data sources in Spring Boot require explicit configuration rather than relying on the single-database defaults.
- Define separate
DataSourcebeans and usually mark one as@Primary. - Give each JPA database its own entity manager factory, transaction manager, and repository package.
- Keep schema creation or migrations explicit for each database instead of depending on one global assumption.
- Clear package boundaries and bean naming are what keep multi-database Spring Boot setups manageable.

