Quick revision
The short answer
We bind each database's settings under its own prefix, such as app.datasource.main and app.datasource.reporting, and create two HikariDataSource beans from them, marking the main one @Primary so JPA, Flyway and anything else that expects a single DataSource gets it. The reporting DataSource is injected by name with @Qualifier into its own JdbcClient. Once we define any DataSource bean, Boot's DataSource auto-configuration backs off, so spring.datasource.* no longer configures anything by itself and we must bind the properties ourselves; if both databases are written transactionally, each also needs its own transaction manager.
What to remember
In this answer, we saw that Spring Boot builds one DataSource from spring.datasource.* only while we have not defined our own, and backs off completely once we do. For two databases, we bind settings under separate prefixes, build each HikariDataSource from a DataSourceProperties bean with initializeDataSourceBuilder(), mark the main one @Primary so JPA and Flyway still work, and inject the reporting one by @Qualifier into its own JdbcClient. If both databases are written transactionally, each needs its own transaction manager, and binding directly onto HikariDataSource means using jdbc-url.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We bind each database's settings under its own prefix, such as app.datasource.main and app.datasource.reporting, and create two HikariDataSource beans from them, marking the main one @Primary so JPA, Flyway and anything else that expects a single DataSource gets it. The reporting DataSource is injected by name with @Qualifier into its own JdbcClient. Once we define any DataSource bean, Boot's DataSource auto-configuration backs off, so spring.datasource.* no longer configures anything by itself and we must bind the properties ourselves; if both databases are written transactionally, each also needs its own transaction manager.
Why this question is asked
Most services start with a single database, and Spring Boot makes that effortless: we set spring.datasource.url and everything else appears. The moment a second database arrives, for reporting, a legacy system or a read replica, that convenience turns into confusion, because the automatic setup quietly stops working. Interviewers ask this to check that we understand what Boot does for us and what we take over when we define our own beans. In this answer, we will recap what auto-configuration does for one DataSource, see what it stops doing once we define our own, write the full configuration for two DataSources, use the second one with @Qualifier, and finish with transaction managers.
1A Quick Recap of the Single DataSource Setup
A DataSource is the object our code asks for database connections. In Spring Boot, DataSourceAutoConfiguration creates one for us when there is a JDBC driver on the classpath. It reads spring.datasource.url, username and password into a DataSourceProperties object, builds a HikariDataSource from them, and applies spring.datasource.hikari.* to the pool. Other auto-configurations then build on that bean: JdbcTemplate and JdbcClient, the JPA EntityManagerFactory, Flyway or Liquibase, and a transaction manager.
All of this is conditional. The DataSource auto-configuration only creates its bean if the application has not defined a DataSource bean already. That condition is the key to the whole question.
2What Auto-Configuration Stops Doing
As soon as we declare a DataSource bean of our own, even one, DataSourceAutoConfiguration backs off and creates nothing. So the spring.datasource.* properties are no longer applied to any pool automatically. If we leave them in application.yml and build our beans by hand, they are simply ignored, which is a common source of surprise: the URL looks right in the file, but the pool connects somewhere else or fails to start.
The beans built on top still work, as long as they can find one DataSource to use. Auto-configured JdbcTemplate, JPA and Flyway expect a single candidate, so with two DataSource beans we must tell Spring which one is the default. That is what @Primary is for.
3Configuring Two DataSources
We put each database's settings under its own prefix, so the two are clearly separate.
Then a configuration class creates the beans. For each database, a DataSourceProperties bean holds url, username and password, and initializeDataSourceBuilder() turns them into a builder. The nested configuration properties are then bound onto the resulting HikariDataSource, so pool settings such as maximum-pool-size work too.
4The Configuration Class
This is the pattern shown in the Spring Boot reference documentation for two DataSources, written out for our names.
Notice the two roles of @Primary. On the properties bean, it makes the unqualified DataSourceProperties parameter resolve to the main one. On the DataSource, it makes JPA, Flyway and auto-configured JdbcTemplate use the main database. Everything that expects a single DataSource now has an answer.
A simpler variant skips DataSourceProperties and puts @ConfigurationProperties("app.datasource.reporting") directly on a method that returns DataSourceBuilder.create().type(HikariDataSource.class).build(). The properties are then bound straight onto HikariDataSource, which has no url property but a jdbcUrl one, so the key in the file must be jdbc-url. DataSourceProperties is the class that understands url and translates it for us. The package of DataSourceProperties changed when Spring Boot 4 split auto-configuration into modules, so check your version for the import.
5Using the Reporting DataSource With @Qualifier
The reporting DataSource is never the default, so any code that wants it asks for it by name. The cleanest way is to give it its own JdbcClient bean, so the rest of the code never handles a DataSource directly.
- JPA, Flyway, default JdbcClientuse the
@PrimaryDataSource - mainDataSource (HikariPool-1)
app.datasource.main.* - reportingJdbcClientasked for by
@Qualifier - reportingDataSource (HikariPool-2)
app.datasource.reporting.*
4 layers, from the top down. Step through them one at a time.
If we forget the qualifier, Spring injects the primary DataSource, and the query runs against the main database, where daily_sales probably does not exist. The error then looks like a missing table, not a wiring mistake, so qualifiers are worth checking first when a second database misbehaves.
6One Transaction Manager per Database
A transaction manager starts and commits transactions on one DataSource. With JPA on the main database, Boot's auto-configured JpaTransactionManager covers the main DataSource only. If we only read from the reporting database, that is fine. If we write to it and need rollback, the reporting DataSource needs its own manager.
But there is a catch: defining our own transaction manager can make Boot's transaction manager auto-configuration back off too, because it is also conditional on a missing bean. If that happens, we declare the main JpaTransactionManager ourselves as well, mark it @Primary, and name the second one explicitly wherever it is used.
7Follow-up questions
With @ConfigurationProperties on HikariDataSource, why is the property jdbc-url and not url?
Because @ConfigurationProperties binds properties to the setters of the object it is placed on, and HikariDataSource, through HikariConfig, has setJdbcUrl(), not setUrl(). So the property name is jdbc-url in relaxed binding. Spring Boot's own DataSourceProperties has a url property and maps it to the right setting when it builds the pool, which is why the spring.datasource.url key works in the single DataSource case. If we bind straight onto HikariDataSource and write url, the value is ignored and HikariCP fails with an error saying jdbcUrl is required.
Is there a way to add a second DataSource without replacing the auto-configured one?
Recent versions offer one. Spring Framework 6.2 added @Bean(defaultCandidate = false), and the Spring Boot 3.4 and later reference documentation shows using it with a @Qualifier for an additional DataSource. Such a bean is only injected where it is asked for by qualifier, so it does not count as a competing candidate, and the auto-configured spring.datasource.* DataSource stays in place. Check your version before relying on it.
Do we need two EntityManagerFactories?
Only if we use JPA on both databases. Then each needs its own LocalContainerEntityManagerFactoryBean, its own JpaTransactionManager and its own @EnableJpaRepositories with basePackages pointing at its repositories. For a reporting database that is read with SQL, a JdbcClient is simpler and avoids a second JPA setup entirely.
8Conclusion
In this answer, we saw that Spring Boot builds one DataSource from spring.datasource.* only while we have not defined our own, and backs off completely once we do. For two databases, we bind settings under separate prefixes, build each HikariDataSource from a DataSourceProperties bean with initializeDataSourceBuilder(), mark the main one @Primary so JPA and Flyway still work, and inject the reporting one by @Qualifier into its own JdbcClient. If both databases are written transactionally, each needs its own transaction manager, and binding directly onto HikariDataSource means using jdbc-url.
Open with
I would put each database's settings under its own prefix, say app.datasource.main and app.datasource.reporting, and create a DataSourceProperties bean for each with @ConfigurationProperties. From each one I build the pool with initializeDataSourceBuilder, type HikariDataSource, build, and I bind a nested configuration prefix onto the HikariDataSource bean so pool settings like maximum pool size apply too.
Then, if they want more
The main DataSource and its properties are marked @Primary, so JPA, Flyway and the auto-configured JdbcTemplate keep using it. For the reporting database I create a JdbcClient bean from the DataSource injected with @Qualifier reportingDataSource, and repositories take that client, so nobody accidentally queries the wrong database.
The important point about auto-configuration is that DataSourceAutoConfiguration only creates a DataSource if none is defined; as soon as I define one, it backs off and spring.datasource properties are no longer applied, so I bind everything myself. If I bound properties straight onto HikariDataSource, the key would be jdbc-url, because that class has jdbcUrl rather than url.
Finally, if I write to both databases transactionally, the reporting one needs its own DataSourceTransactionManager, used with @Transactional transactionManager equals reportingTx.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.4, Spring Boot 4, Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.sql, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot service needs a second DataSource for a reporting database next to the...": https://scrivio-interview-library.netlify.app/databases/jdbc/b7d5c36b76/