Reputation: 13
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?
Upvotes: 0
Views: 69
Reputation: 72
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
Upvotes: 0
Reputation: 72
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
Upvotes: 1