How to add multiple database support to Spring application

Define multiple configuration files. In each configuration file, specify the JpaVendorAdapter, datasource, enityManager, entityManagerFactory, and transactionManager. You can set a default configuration file and multiple other configuration files.

A sample goes as below:

Default configuration file:

@Configuration
@EnableTransactionManagement
@EnableAutoConfiguration
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager",
        basePackages = {"bmo.hub.bpm.foundation.appmonitor.db.repo"}
)
public class AppConfig {

    @Autowired
    private JpaVendorAdapter jpaVendorAdapter;

    @Autowired
    private DataSource dataSource;

    @Bean(name = "entityManager")
    public EntityManager entityManager() {
        return entityManagerFactory().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public EntityManagerFactory entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource);
        emf.setJpaVendorAdapter(jpaVendorAdapter);
        emf.setPackagesToScan("bmo.hub.bpm.foundation.appmonitor.db.entity");
        emf.setPersistenceUnitName("default");
        emf.afterPropertiesSet();

        return emf.getObject();
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager tm = new JpaTransactionManager();
        tm.setEntityManagerFactory(entityManagerFactory());

        return tm;
    }
}

Another configuration file:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "mortgageEntityManagerFactory",
        transactionManagerRef = "mortgageTransactionManager",
        basePackages = {"bmo.hub.bpm.foundation.appmonitor.db.repo.mortgage"}
)
public class MortgageConfig {

    @Autowired
    private JpaVendorAdapter jpaVendorAdapter;

//    @Autowired
//    private DataSource dataSource;
    @Value("${spring.datasource.mortgage.url}")
    private String databaseUrl;

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

    @Value("${spring.datasource.mortgage.password}")
    private String password;

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

    @Bean(name = "mortgageEntityManager")
    public EntityManager entityManager() {
        return entityManagerFactory().createEntityManager();
    }

    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(databaseUrl, username, password);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }

    @Primary
    @Bean(name = "mortgageEntityManagerFactory")
    public EntityManagerFactory entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource());
        emf.setJpaVendorAdapter(jpaVendorAdapter);
        emf.setPackagesToScan("bmo.hub.bpm.foundation.appmonitor.db.entity.mortgage");
        emf.afterPropertiesSet();

        return emf.getObject();
    }

    @Bean(name = "mortgageTransactionManager")
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager tm = new JpaTransactionManager();
        tm.setEntityManagerFactory(entityManagerFactory());

        return tm;
    }
}

And the properties file (application.yml):

selenium:
  grid:
    url: http://localhost:4444/wd/hub

scheduling:
  job:
    cron: 0 0/3 * * * *

spring:
  datasource:
    url: jdbc:oracle:thin:@10.193.61.167:1521:garnish
    username: row_dev
    password: Welcome#1
    driver-class: oracle.jdbc.driver.OracleDriver

    mortgage:
      url: jdbc:oracle:thin:@10.193.46.193:1521:mortgage
      username: mortgage
      password: MORTGAGE
      driver-class: oracle.jdbc.driver.OracleDriver

    hikari:
      connection-timeout: 60000
      maximum-pool-size: 5

 

Coding Practice & Competion Websites

  1. topcoder
    http://www.topcoder.com
    (a great blog: http://petr-mitrichev.blogspot.com/)
  2. hackerrank
    hackerrank.com
  3. leetcode
    leetcode.com
  4. coderbyte
    coderbyte.com
  5. geeksforgeeks
    geeksforgeeks.org
  6. codeforces
    codeforces.com
  7. codewars
    codewars.com
  8. codeeval
    http://www.codeeval.com
  9. spoj
    spoj.com
  10. devdraft
    devdraft.com
  11. codechef
    http://www.codechef.com
  12. projecteuler
    projecteuler.net
  13. hackerearth
    hackerearth.com
  14. checkio
    py.checkio.org
  15. codinggame
    codinggame.com
  16. theaigames
    theaigames.com
  17. programmr
    http://www.programmr.com

 

 

 

Intellij error: usage of api documented as @since…, Usage of API documented as @since 1.6+..

To get rid of the first one, you need to open up

File -> Project Structure -> Project Settings -> Modules -> "Your Module Name" -> Sources -> Language Level

and change that to the desired level i.e 1.8 or the Project Default language level

To fix the second one:

  1. File > Settings > Build, Execution, Deployment > Java Compiler
  2. Change Target bytecode version to 1.8 of the module that you are working for.