1

In my application i need to integrate two datasource but when i integrate the second database using JdbcTemplate then the previous one not working, instead all table created in the second datasource

#1 DataSource

@Configuration @Profile("mariadb4j") @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class EmbeddedMariaDBConfig { private static final Logger L = LoggerFactory.getLogger(EmbeddedMariaDBConfig.class); private static final String DB_SERVICE = "dbServiceBean"; @Bean(name = {DB_SERVICE}) MariaDB4jSpringService mariaDB4jSpringService() { L.info("Initializing MariaDB4j service"); return new MariaDB4jSpringService(); } @Bean(name = "adminDataSource") @Primary @DependsOn(DB_SERVICE) DataSource dataSource(MariaDB4jSpringService mdb, DataSourceProperties dataSourceProperties) throws ManagedProcessException { String dbName = dataSourceProperties.getName(); L.debug("Embedded MariaDB datasource properties from spring: [{}]", dataSourceProperties); mdb.getDB().createDB(dbName); if(L.isDebugEnabled()) { DBConfigurationBuilder ecfg = mdb.getConfiguration(); L.debug("JDBC URL for embedded MariaDB as reported by driver: [{}]", ecfg.getURL(dbName)); L.debug("JDBC URL from spring config: [{}]", dataSourceProperties.getUrl()); L.debug("JDBC Username: [{}]", dataSourceProperties.getUsername()); L.debug("JDBC Password: [{}]", dataSourceProperties.getPassword()); } return DataSourceBuilder .create() .username(dataSourceProperties.getUsername()) .password(dataSourceProperties.getPassword()) .url(dataSourceProperties.getUrl()) .driverClassName(dataSourceProperties.getDriverClassName()) .build(); } } 

Yaml Configuration

spring: profiles: mariadb4j datasource: username: root password: password driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/t1 mariaDB4j: dataDir: /tmp/mariadb port: 3900 

2 DataSource

@Configuration public class ExoDBConfig { @Bean(name="user") public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/t2"); dataSource.setUsername("root"); dataSource.setPassword("password"); return dataSource; } @Bean public JdbcTemplate jdbcTemplate(@Qualifier("user")DataSource dataSource) { return new JdbcTemplate(dataSource); } } 

When i use only one DataSource the 1st one then it's working fine and tables are creating in t1 database but when i integrate the 2nd DataSource then it points to second DataSource means all the tables is creating in the t2 database.

1
  • Did you check out stackoverflow.com/questions/30337582/…? That worked for me. You basically have to build up noth EntityManagers manually and define which package contains the entities for source1 and which for source2 Commented Jun 13, 2018 at 19:29

1 Answer 1

2

Just Create DataSource object in the JdbcTemplate itself, working for me, and you can create separate properties file for t2 database

 @Bean public JdbcTemplate jdbcTemplate() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/t2"); dataSource.setUsername("user"); dataSource.setPassword("password"); return new JdbcTemplate(dataSource); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.