0

I want to use two databases in spring boot application and each databases contains 20 tables.

I referred below solutions:

  1. https://www.baeldung.com/spring-data-jpa-multiple-databases
  2. https://www.javadevjournal.com/spring-boot/multiple-data-sources-with-spring-boot/
  3. Spring Boot Configure and Use Two DataSources
  4. Spring Boot, Spring Data JPA with multiple DataSources

Note: in all above solutions performed configuration only for two tables. such as below:

1. Customer Configuration:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
 entityManagerFactoryRef = "customerEntityManagerFactory",
 transactionManagerRef = "customerTransactionManager",
 basePackages = {
  "com.javadevjournal.customer.repo"
 }
)
public class CustomerConfig {

 @Primary
 @Bean(name = "customerDataSource")
 @ConfigurationProperties(prefix = "spring.datasource")
 public DataSource customerDataSource() {
  return DataSourceBuilder.create().build();
 }

 @Primary
 @Bean(name = "customerEntityManagerFactory")
 public LocalContainerEntityManagerFactoryBean
 entityManagerFactory(
  EntityManagerFactoryBuilder builder,
  @Qualifier("customerDataSource") DataSource dataSource
 ) {
  return builder
   .dataSource(dataSource)
   .packages("com.javadevjournal.customer.data")
   .persistenceUnit("db1")
   .build();
 }

 @Primary
 @Bean(name = "customerTransactionManager")
 public PlatformTransactionManager customerTransactionManager(
  @Qualifier("customerEntityManagerFactory") EntityManagerFactory customerEntityManagerFactory
 ) {
  return new JpaTransactionManager(customerEntityManagerFactory);
 }
}

2. Product Configuration

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
 entityManagerFactoryRef = "productEntityManagerFactory",
 transactionManagerRef = "productTransactionManager",
 basePackages = {
  "com.javadevjournal.product.repo"
 }
)
public class ProductConfig {

 @Bean(name = "productDataSource")
 @ConfigurationProperties(prefix = "db2.datasource")
 public DataSource dataSource() {
  return DataSourceBuilder.create().build();
 }

 @Bean(name = "productEntityManagerFactory")
 public LocalContainerEntityManagerFactoryBean
 barEntityManagerFactory(
  EntityManagerFactoryBuilder builder,
  @Qualifier("productDataSource") DataSource dataSource
 ) {
  return
  builder
   .dataSource(dataSource)
   .packages("com.javadevjournal.product.data")
   .persistenceUnit("db2")
   .build();
 }

 @Bean(name = "productTransactionManager")
 public PlatformTransactionManager productTransactionManager(
  @Qualifier("productEntityManagerFactory") EntityManagerFactory productEntityManagerFactory
 ) {
  return new JpaTransactionManager(productEntityManagerFactory);
 }
}
  1. Created a Customer Repository by Extending JpaReposity<Customer,Integer>
  2. Created a Product Repository by Extending JpaReposity<Product,Integer>

Questions:

  1. what if my each database has 20 tables? how to configure them like above? do we need to include package in which all those entities reside
  2. do we need to create all 20 repositories for those entities by extending Jparepository (i think this is not good), then how can we do that?
  3. if i don't want to use JpaRepository, then how can configure Entitymanager per Datasource?
James Z
  • 12,104
  • 10
  • 27
  • 43
Bharath
  • 81
  • 7

0 Answers0