← Fidelity Interview Insights

Fidelity·Backend Engineer·Technical Phone Screen·Senior

Senior
Apr 2026

Summary

Technical screen at Fidelity for a backend role, pretty deep on Spring Boot internals. The whole conversation basically lived in multi-datasource JPA territory, which I hadn't fully reviewed.

Questions Asked (5)

Q1

How would you configure two separate datasources in a single Spring Boot application?

System DesignTechnical Trade-offs
Author's notes

I started okay, talked about defining two DataSource beans and using @ConfigurationProperties to bind each one from separate property prefixes.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the need for multiple datasources and the general configuration approach using @Configuration classes and @ConfigurationProperties. Then detail how to set up each datasource with its own EntityManagerFactory and TransactionManager, and discuss how to route repositories or JPA entities to the correct datasource. Finally, mention trade-offs and best practices for maintainability and transaction management.

Pro tip: Emphasize the importance of marking one datasource as @Primary to avoid ambiguity and using separate packages for entities and repositories to simplify configuration. Also, mention that transaction management becomes complex with multiple datasources, so consider using ChainedTransactionManager or avoiding distributed transactions if possible.

1. Identify the need and approach

Explain why multiple datasources are needed (e.g., different databases, read/write splitting) and the general strategy: define separate DataSource beans, each with its own configuration properties.

2. Configure DataSource beans

Show how to create @Configuration classes with @Bean methods for each DataSource, using @ConfigurationProperties to bind properties from application.yml or application.properties. Mark one as @Primary.

3. Set up EntityManagerFactory and TransactionManager

For each datasource, configure a LocalContainerEntityManagerFactoryBean and a JpaTransactionManager, specifying the packages to scan for entities and repositories.

4. Route repositories and entities

Use @EnableJpaRepositories with basePackages and entityManagerFactoryRef to associate repositories with the correct datasource. Similarly, specify packages for entity scanning.

5. Discuss trade-offs and best practices

Mention challenges like transaction management across datasources, increased complexity, and potential solutions (e.g., ChainedTransactionManager, avoiding distributed transactions). Highlight the importance of clear separation and testing.

Key Points to Mention

  • Use of @ConfigurationProperties to bind datasource-specific properties.
  • Marking one datasource as @Primary to resolve autowiring conflicts.
  • Configuring separate EntityManagerFactory and TransactionManager for each datasource.
  • Using @EnableJpaRepositories with basePackages and entityManagerFactoryRef to route repositories.
  • Transaction management challenges and potential solutions like ChainedTransactionManager.
  • Best practices: separate packages for entities/repositories, clear naming conventions, and thorough testing.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Walk me through setting up separate EntityManagerFactory and TransactionManager beans for each datasource.

System DesignAPI & Integrations
Author's notes

This is where I started losing ground.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the need for multiple datasources in a Spring application, then walk through the configuration of separate DataSource, EntityManagerFactory, and TransactionManager beans for each datasource, highlighting the importance of @Primary and @Qualifier annotations to avoid conflicts. Conclude with how to use these beans in your DAOs or repositories, ensuring transaction boundaries are correctly managed.

Pro tip: Emphasize that transaction managers are tied to specific EntityManagerFactory beans, so you must explicitly qualify which transaction manager to use in each service or repository to prevent cross-datasource transaction issues.

1. Define DataSource beans

Create separate DataSource beans for each database, using @ConfigurationProperties to externalize connection details and mark one as @Primary if needed.

2. Configure EntityManagerFactory beans

For each DataSource, define a LocalContainerEntityManagerFactoryBean, specifying the data source, JPA vendor adapter, and packages to scan for entities.

3. Set up TransactionManager beans

Create a JpaTransactionManager for each EntityManagerFactory, ensuring each transaction manager is linked to its corresponding EntityManagerFactory.

4. Qualify bean usage

Use @Qualifier annotations on injection points to specify which EntityManagerFactory or TransactionManager to use in repositories or services.

5. Handle transactions and testing

Ensure @Transactional annotations specify the correct transaction manager, and configure tests to use the appropriate datasource and transaction manager.

Key Points to Mention

  • Use of @Primary to designate a default bean when multiple candidates exist
  • Importance of @Qualifier to resolve ambiguity during injection
  • Separate transaction managers per datasource to maintain transaction isolation
  • Configuration of multiple DataSources with @ConfigurationProperties
  • Package scanning for entities to avoid overlap between datasources
  • Testing strategies for multiple datasources, such as @DataJpaTest with @AutoConfigureTestDatabase

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

How do you scope JPA repositories so each one uses the correct datasource and transaction manager?

System DesignTechnical Trade-offs
Author's notes

Used @EnableJpaRepositories with basePackages, entityManagerFactoryRef, and transactionManagerRef.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining that in Spring, each JPA repository is tied to an EntityManagerFactory, which in turn is configured with a specific DataSource and transaction manager. Then describe how to scope repositories to the correct datasource by using separate configuration classes, @EnableJpaRepositories with basePackages, entityManagerFactoryRef, and transactionManagerRef, and how to ensure transactional boundaries use the right manager.

Pro tip: Mention that you must also align @Transactional annotations with the correct transaction manager, either by using the transactionManager attribute or by structuring your services so that each service only interacts with one datasource, avoiding distributed transactions unless absolutely necessary.

1. Explain the default single-datasource setup

Describe how Spring Boot auto-configures a single DataSource, EntityManagerFactory, and JpaTransactionManager, and how @EnableJpaRepositories by default uses these beans.

2. Introduce multiple datasources

Explain that for multiple datasources, you need to define separate DataSource, EntityManagerFactory, and PlatformTransactionManager beans, typically in separate @Configuration classes.

3. Scope repositories to datasources

Show how to use @EnableJpaRepositories with basePackages, entityManagerFactoryRef, and transactionManagerRef to bind specific repository interfaces to the correct EntityManagerFactory and transaction manager.

4. Handle transactions correctly

Discuss how to ensure @Transactional uses the correct transaction manager, either by specifying the transactionManager attribute or by using separate service layers for each datasource.

5. Address cross-datasource concerns

Mention challenges like distributed transactions, and suggest patterns like ChainedTransactionManager or avoiding cross-datasource operations in a single transaction.

Key Points to Mention

  • Spring's @EnableJpaRepositories annotation with basePackages, entityManagerFactoryRef, and transactionManagerRef
  • Separate @Configuration classes for each datasource, each defining DataSource, LocalContainerEntityManagerFactoryBean, and JpaTransactionManager
  • The importance of @Transactional's transactionManager attribute or using qualifiers to specify the correct manager
  • Potential pitfalls: default autoconfiguration conflicts, entity scanning overlaps, and transaction propagation across multiple datasources
  • Strategies for cross-datasource transactions: ChainedTransactionManager, XA transactions, or eventual consistency patterns
  • Testing considerations: using @DataJpaTest with multiple datasources requires explicit configuration

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

What are common pitfalls when running two datasources alongside Spring Boot's auto-configuration?

Technical Trade-offsRoot Cause Analysis
Author's notes

Auto-config conflicts were the main thing I hit on.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by acknowledging that Spring Boot's auto-configuration is designed for a single datasource, so adding a second one requires overriding defaults and carefully managing bean definitions. Then, walk through the common pitfalls in a structured way, emphasizing how each can lead to runtime failures or subtle bugs. Finally, mention strategies to avoid these pitfalls, such as explicit configuration and testing.

Pro tip: Highlight that the @Primary annotation is your best friend for designating the default datasource, but be explicit about which beans need it—ambiguity here is a frequent source of startup failures. Also, consider using separate @Configuration classes for each datasource to keep things clean and avoid cross-contamination.

1. Acknowledge auto-configuration limitations

Explain that Spring Boot's DataSourceAutoConfiguration expects a single datasource and will back off if you define your own, but it won't automatically handle multiple datasources.

2. Identify bean conflicts and ambiguity

Discuss how multiple DataSource, EntityManagerFactory, and TransactionManager beans can cause NoUniqueBeanDefinitionException unless one is marked @Primary or qualified.

3. Highlight transaction management issues

Point out that @Transactional needs to know which transaction manager to use; without specifying, it may use the wrong one or fail, leading to data inconsistency.

4. Address property binding and configuration

Mention that spring.datasource.* properties apply to only one datasource; the second requires custom prefix (e.g., spring.second-datasource) and explicit @ConfigurationProperties binding.

5. Discuss repository and entity scanning

Explain that @EnableJpaRepositories and @EntityScan must be configured per datasource with base packages, otherwise repositories may attach to the wrong datasource or not be found.

Key Points to Mention

  • Bean ambiguity and the need for @Primary or @Qualifier
  • Transaction manager selection and @Transactional pitfalls
  • Property binding with custom prefixes and @ConfigurationProperties
  • Repository and entity scanning with @EnableJpaRepositories and @EntityScan
  • Health checks and metrics for multiple datasources (e.g., Actuator)
  • Testing strategies to verify both datasources work independently

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q5

When would you need distributed transactions across two datasources, and how would you handle that in Spring?

System DesignTechnical Trade-offs
Author's notes

Honestly I knew the answer was JTA but I couldn't remember a concrete implementation name under pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining when distributed transactions are needed, such as when an operation must atomically update data across two separate databases (e.g., order and inventory). Then discuss Spring's support for distributed transactions, focusing on JTA with a transaction manager like Atomikos or Bitronix, and mention alternatives like the Saga pattern for microservices. Conclude with trade-offs and best practices.

Pro tip: Emphasize that distributed transactions should be a last resort due to complexity and performance overhead; prefer eventual consistency and compensating transactions where possible. This shows you understand real-world constraints and modern architectural patterns.

1. Identify the need

Explain scenarios where a single business operation must atomically update two different data sources, such as transferring funds between accounts in separate databases or updating an order and inventory system.

2. Discuss Spring's JTA support

Describe how Spring can manage distributed transactions using JTA with a transaction manager (e.g., Atomikos, Bitronix) and the @Transactional annotation, ensuring ACID properties across resources.

3. Mention alternatives

Highlight that in distributed systems, patterns like Saga (with compensating transactions) or eventual consistency are often preferred over XA transactions due to scalability and performance.

4. Address trade-offs

Discuss the overhead, complexity, and potential failure points of distributed transactions, and when it's acceptable to use them (e.g., strict consistency requirements).

5. Provide a practical example

Walk through a code snippet or configuration example showing how to set up JTA in Spring, or describe a Saga implementation using Spring State Machine or event-driven architecture.

Key Points to Mention

  • XA protocol and two-phase commit (2PC) for distributed transactions
  • Spring's JtaTransactionManager and integration with application servers or standalone JTA providers
  • The @Transactional annotation and its limitations across multiple datasources
  • Saga pattern and compensating transactions for microservices
  • Eventual consistency and BASE vs ACID trade-offs
  • Performance and complexity implications of distributed transactions

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.