0

I am trying to set up a second data source in my Spring application. Below are the 2 configuration classes for the 2 data sources:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.XYXYale.persistence.XY",
        entityManagerFactoryRef = "awEntityManagerFactory",
        transactionManagerRef= "awTransactionManager"
)
public class Datasource1DataSourceConfig {

    @Value("${spring.first-datasource.url}")
    private String url;

    @Value("${spring.first-datasource.username}")
    private String username;

    @Value("${spring.first-datasource.password}")
    private String pw;

    @Value("${spring.first-datasource.driver-class-name}")
    private String driver;


    @Bean
    @Primary
    @ConfigurationProperties("spring.first-datasource")
    public DataSourceProperties awDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @Qualifier("awEntityManagerFactory")
    public DataSource awDataSource() {

        DriverManagerDataSource dataSource
                = new DriverManagerDataSource();
        dataSource.setDriverClassName(
                driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(pw);

        return dataSource;
    }

    @Primary
    @Bean(name = "awEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean awEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(awDataSource());
        em.setPersistenceUnitName("XY");



");
        em.setPackagesToScan(new String[] { "com.XY.XY.domain.XY" });
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }

    @Primary
    @Bean
    public PlatformTransactionManager awTransactionManager() {
        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                awEntityManagerFactory().getObject());
        return transactionManager;
    }

}

The second Config class:


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.XYXYale.persistence.YX",
        entityManagerFactoryRef = "sndEntityManagerFactory",
        transactionManagerRef= "sndTransactionManager"
)
public class SndDataSourceConfig {

    @Value("${spring.second-datasource.jdbcUrl}")
    private String url;

    @Value("${spring.second-datasource.username}")
    private String username;

    @Value("${spring.second-datasource.password}")
    private String pw;

    @Value("${spring.second-datasource.driver-class-name}")
    private String driver;

    @Bean
    @ConfigurationProperties("spring.second-datasource")
    public DataSourceProperties sndDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Qualifier("sndEntityManagerFactory")
    public DataSource sndDataSource() {
        DriverManagerDataSource dataSource
                = new DriverManagerDataSource();
        dataSource.setDriverClassName(
                driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(pw);

        return dataSource;
    }

    @Bean(name = "sndEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean sndEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(sndDataSource());
        em.setPersistenceUnitName("snd");
        em.setPackagesToScan(new String[] { "com.XY.XY.domain.YX" });
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }

    @Bean
    public PlatformTransactionManager sndTransactionManager() {
        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                sndEntityManagerFactory().getObject());
        return transactionManager;
    }

}

I have in com.XYXYale.persistence.XY a Spring data JPA Repo defined as follows

DemoRepo


@Repository
public interface DemoRepo extends CrudRepository<Demo, String>, DemoRepoCustom{
}

DemoRepoCustom

@NoRepositoryBean
public interface DemoRepoCustom {
    Demo returnDemoContent();
}

DemoRepoImpl

public class DemoRepoImpl extends QuerydslRepositorySupport implements DemoRepoCustom {

    public DemoRepoImpl() {
        super(Demo.class);
    }

    public Demo returnDemoContent(){
        return something;
    }

}

The Repo is used in the following way

@Autowired
DemoRepo demoRepo;

I am getting this exception:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRepositoryImpl': Unsatisfied dependency expressed through method 'setEntityManager' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2: org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:678)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1255)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1175)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:595)
    ... 55 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2: org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:221)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1233)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1175)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:670)
    ... 70 more

Anyone having a suggestion on how to solve that? I could think of injecting the correct entitymanager to each repo, however I have no idea how to do that.

Thanks in advance. Cant find any solution on here or other sites.

GreeceTom
  • 35
  • 1
  • 6
  • Possible duplicate of https://stackoverflow.com/questions/59002892/no-qualifying-bean-of-type-javax-sql-datasource-available-more-than-one-prim#comment104256413_59002892 – Ramu Nov 24 '19 at 02:47
  • @Ramu Nope, already looked through answers on here, otherwise I wouldn’t have asked. – GreeceTom Nov 24 '19 at 02:50
  • You could try using a qualifier annotation to specify the bean name explicitly, since spring is finding two beans and does not know which one to inject. – S B Nov 24 '19 at 02:53
  • Sorry about it. Your requirement looked a lot similar to that one. See if the following one helps you? https://stackoverflow.com/questions/45663025/spring-data-jpa-multiple-enablejparepositories – Ramu Nov 24 '19 at 02:57
  • Thanks @SB Do you have an example how a qualifier is correctly implemented in spring Config classes as well as the impl classes of the repos? – GreeceTom Nov 24 '19 at 03:23
  • @GreeceTom - do see if this helps - https://stackoverflow.com/questions/49127000/using-qualifier-and-bean-together-in-java-config-spring – S B Nov 24 '19 at 04:21
  • What’s the `entityManagerFactoryRef` on the second one? A [complete example](/help/mcve) would be useful here. – Andy Wilkinson Nov 24 '19 at 07:44
  • @SB Thanks but this doesnt really show how to inject it into repositories. – GreeceTom Nov 24 '19 at 13:11
  • GreeceTom - when marking the repository classes/methods with @Transactional, you can specify the transaction manager e.g. @Transactional("awTransactionManager") – S B Nov 24 '19 at 14:35
  • @SB Thanks for the suggestion but still getting the same error – GreeceTom Nov 24 '19 at 14:46
  • @AndyWilkinson Just added the other config class. – GreeceTom Nov 24 '19 at 15:08
  • @GreeceTom - please check an example in this link - https://www.baeldung.com/spring-data-jpa-multiple-databases – S B Nov 25 '19 at 16:05
  • @SB This is what I did but it didn`t work. That`s why I posted that question here... – GreeceTom Nov 25 '19 at 17:27
  • @GreeceTom, not sure if your error is resolved now, but the above configuration deviates a bit from that in the reference. Bean and Primary do not need Qualifier at the time of declaration. – S B Nov 28 '19 at 14:32

1 Answers1

-1

if you will need to connect to more than one data source.
and assuming that you have already your spring app, then use this code :
in your application.properties add this code:

# DEFAULT CONFIG
spring.main.banner-mode=off
server.port=2020
# CUSTOM PREFIX FOR SQL SERVER
sqlserver.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
sqlserver.datasource.jdbcUrl=jdbc:sqlserver://192.168.9.44;databaseName=myAwesomeDB
sqlserver.datasource.username=someUser
sqlserver.datasource.password=somePass
# DEFAULT PREFIX FOR POSTGRES
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://192.168.6.125:5432/wilmer
spring.datasource.username=admWinter
spring.datasource.password=wilmer43nlp

now create a class called DataSourceDBConfig.java and add this code:

package com.example.demo.config;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;

/**
 * @author Wilmer Villca
 * @version 0.0.1
 */

@Configuration
public class DataSourceDBConfig {

    private final Environment env;

    @Autowired
    public DataSourceDBConfig(Environment env) {
        this.env = env;
    }

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

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public HikariDataSource dataSourceFromPostgres() {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("org.postgresql.Driver");
        hikariConfig.setJdbcUrl(env.getProperty("spring.datasource.url"));
        hikariConfig.setUsername(env.getProperty("spring.datasource.username"));
        hikariConfig.setPassword(env.getProperty("spring.datasource.password"));
        return new HikariDataSource(hikariConfig);
    }

    @Bean
    @Qualifier("jdbcTemplateSqlServer")
    public JdbcTemplate jdbcTemplateSqlServer(@Qualifier("dataSourceFromSqlServer") DataSource ds) {
        return new JdbcTemplate(ds);
    }

    @Bean
    @Qualifier("jdbcTemplatePostgres")
    public JdbcTemplate jdbcTemplatePostgres(@Qualifier("dataSourceFromPostgres") DataSource ds) {
        return new JdbcTemplate(ds);
    }

}

that is all you need to to access multiple databases

  NOTE: Do not forget and remember that this only is an alternative, exist anothers many ways

i hope you understand,

Wilmer Villca
  • 552
  • 4
  • 5