2
votes

I'm trying to connect 2 different database for my spring boot application and this error occurs :


APPLICATION FAILED TO START


Description:

Parameter 0 of method entityManagerFactory in com.SyncFibertToolSpring.SyncFibertTool.MydbDB.Config.MydbDbConfig required a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' in your configuration.

Below you will find my 2 configurations classes for the databases


package com.SyncFibertToolSpring.SyncFibertTool.MydbDB.Config;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "mydbEntityManagerFactory",
    basePackages = {"com.SyncFibertToolSpring.SyncFibertTool.MydbDB.DAO"})
public class MydbDbConfig {


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

  @Primary
  @Bean(name = "mydbEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(
      EntityManagerFactoryBuilder builder, @Qualifier("mydbDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource)
        .packages("com.SyncFibertToolSpring.SyncFibertTool.MydbDB.entities").

        build();
  }

  @Primary
  @Bean(name = "mydbTransactionManager")
  public PlatformTransactionManager transactionManager(
      @Qualifier("mydbEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
  }

}
package com.SyncFibertToolSpring.SyncFibertTool.fibertoolSyncDB.Config;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "fibertoolSyncEntityManagerFactory",
    basePackages = {"com.SyncFibertToolSpring.SyncFibertTool.fibertoolSyncDB.DAO"})
public class FibertToolDbConfig {

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

  @Primary
  @Bean(name = "fibertoolSyncEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(
      EntityManagerFactoryBuilder builder,
      @Qualifier("fibertoolSyncDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource)
        .packages("com.SyncFibertToolSpring.SyncFibertTool.fibertoolSyncDB.entities").

        build();
  }

  @Primary
  @Bean(name = "fibertoolSyncTransactionManager")
  public PlatformTransactionManager transactionManager(
      @Qualifier("fibertoolSyncEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
  }


}
1

1 Answers

3
votes

The first error here is that you defined two @Primary datasource. Remove annotation @Primary from one of the two @Configuration class and just leave it to one class