5 min read.
This blog is regarding the multiple connection with a single Spring Boot Application. This blog is written for the Spring Developers with moderate experience.
We are going to use MySQL database for this demo, any other database can also be configured we just have to change its driver name in configurations.
Lets start with configuration in application.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
jdbc.driver-class-name=com.mysql.jdbc.Driver jdbc.show-sql=true spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect #First Datasource (DB1) db1.datasource.url=jdbc:mysql://localhost:3306/db1?useSSL=false db1.datasource.username={db_user_name} db1.datasource.password={db_user_password} #Second Datasource (DB2) db2.datasource.url=jdbc:mysql://localhost:3306/db2?useSSL=false db2.datasource.username={db_user_name} db2.datasource.password={db_user_password} |
In above code snippet, we can observe that for different database we have got different configuration. Use your database name instead of db1 and similarly for database 2, provide the name.
Now let’s configure the datasource for our different database. Notice that for each database configuration we will have a separate Spring Config File. Let’s configure the datasource 1 first :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.pramati.config; import java.util.HashMap; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; 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.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableJpaRepositories(basePackages = {"com.pramati.repository.db1"}, entityManagerFactoryRef = "db1EntityManager", transactionManagerRef = "db1TransactionManager") @EnableTransactionManagement public class Db1_Datasource { @Autowired private Environment env; @Bean @Primary public LocalContainerEntityManagerFactoryBean db1EntityManager() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(db1Datasource()); em.setPackagesToScan(new String[]{"com.pramati.model.db1"}); em.setPersistenceUnitName("db1EntityManager"); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); HashMap<String, Object> properties = new HashMap<String, Object>(); properties.put("hibernate.dialect", env.getProperty("hibernate.dialect")); properties.put("hibernate.show-sql", env.getProperty("jdbc.show-sql")); em.setJpaPropertyMap(properties); return em; } @Primary @Bean public DataSource db1Datasource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName( env.getProperty("jdbc.driver-class-name")); dataSource.setUrl(env.getProperty("db1.datasource.url")); dataSource.setUsername(env.getProperty("db1.datasource.username")); dataSource.setPassword(env.getProperty("db1.datasource.password")); return dataSource; } @Primary @Bean public PlatformTransactionManager db1TransactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory( db1EntityManager().getObject()); return transactionManager; } } |
“com.pramati.repository.db1” contains the repository layer aka DAO layer for database 1, db1.
“com.pramati.model.db1” contains the model or entities for database 1, db1.
Similarly we will configure the datasource for database 2, db2 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.pramati.config; import java.util.HashMap; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableJpaRepositories(basePackages = {"com.pramati.repository.db2"}, entityManagerFactoryRef = "db2EntityManager", transactionManagerRef = "db2TransactionManager") @EnableTransactionManagement public class DB2_Datasource { @Autowired private Environment env; @Bean public LocalContainerEntityManagerFactoryBean db2EntityManager() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(db2Datasource()); em.setPackagesToScan( new String[]{"com.pramati.model.db2"}); em.setPersistenceUnitName("db2EntityManager"); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); HashMap<String, Object> properties = new HashMap<String, Object>(); properties.put("hibernate.dialect", env.getProperty("hibernate.dialect")); properties.put("hibernate.show-sql", env.getProperty("jdbc.show-sql")); em.setJpaPropertyMap(properties); return em; } @Bean public DataSource db2Datasource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName( env.getProperty("jdbc.driver-class-name")); dataSource.setUrl(env.getProperty("db2.datasource.url")); dataSource.setUsername(env.getProperty("db2.datasource.username")); dataSource.setPassword(env.getProperty("db2.datasource.password")); return dataSource; } @Bean public PlatformTransactionManager db2TransactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory( db2EntityManager().getObject()); return transactionManager; } } |
“com.pramati.repository.db2” contains the repository layer aka DAO layer for database 1, db1.
“com.pramati.model.db2” contains the model or entities for database 1, db1.
As noticed we have configured the Datasources, Entity Manager and Transaction Manager for db1 and db2. in
Now we will use these configured datasources in our application. We can just autowire the Dao class and it is ready to use anywhere in our application.
For the transactions we have to explicitly set the transaction manager for each database we can directly annotate our transaction methods, see sample below :
1 |
@Transactional(value="db1TransactionManager") |
That’s it folks. Stay tuned for more blogs.
Please visit below link to check out the working code :
https://github.com/atshvrma/SpringBoot_MultipleDatasources.git