0

I want to make a new MS in spring boot, my app need to connect to multiple db (for now its 40 different servers but lets say N ). i have an api that gives me the user name and password of the db i want. i want to have a map of DB. i see only configurations on

#application.properties

dbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mydb
jdbc.username = root
jdbc.password = password
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = false
hibernate.format_sql = false

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.springhibernate.example.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {


@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.springhibernate.example.model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
 }

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    return dataSource;
}

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    return properties;        
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
   HibernateTransactionManager txManager = new HibernateTransactionManager();
   txManager.setSessionFactory(s);
   return txManager;
}

I want to make an api call to get the cardential and then to init db, is it possible?

2 Answers 2

1

Try to use spring transaction manager and configure as many as you want for each db and define separate data sources then would he able to configure multiple databases using spring and hibernate.enter link description here

click here to see more

Sign up to request clarification or add additional context in comments.

5 Comments

and if i want it to be dynamically?
Could you please describe your exact usecase.
I want to make like a map that i can use for each db, lets say the map is: {db1 : 127.0.0.1, db2 : 172.16.20.177, .... dbn : 172.16.20.188} and i want to call each db by the value
0

See this also .. http://spring.io/blog/2007/01/23/dynamic-datasource-routing/

Thus could be helpful as well Also here

http://forum.spring.io/forum/spring-projects/data/80649-dynamic-database-switching

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.