Spring Boot — Multiple DB Connect using Hibernate | Spring Boot

Donate : Link

WordPress Blog : Link

Applications… : Link

In this tutorial we’ll learn how to connect 2 DB using Spring Boot + Hibernate.

pom.xml

	<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

application.properties

jdbc.test1.driverClassName = com.mysql.jdbc.Driver
jdbc.test1.url = jdbc:mysql://localhost:3306/DATABASE1?useSSL=false
jdbc.test1.username = USERNAME1
jdbc.test1.password = PASSWORD1
jdbc.test2.driverClassName = com.mysql.jdbc.Driver
jdbc.test2.url = jdbc:mysql://localhost:3306/DATABASE2?useSSL=false
jdbc.test2.username = USERNAME2
jdbc.test2.password = PASSWORD2

MultipleDbConnectApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MultipleDbConnectApplication {
public static void main(String[] args) {
SpringApplication.run(MultipleDbConnectApplication.class, args);
System.out.println("DONE");
}
}

DBConfiguration.java

import java.util.Properties;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.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
public class DBConfiguration {
@Autowired
private Environment environment;

@Bean(name = "test1")
@Primary
public LocalSessionFactoryBean test1SessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(test1DataSource());
sessionFactory.setPackagesToScan("com.example");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource test1DataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("jdbc.test1.driverClassName"));
dataSource.setUrl(environment.getProperty("jdbc.test1.url"));
dataSource.setUsername(environment.getProperty("jdbc.test1.username"));
dataSource.setPassword(environment.getProperty("jdbc.test1.password"));
return dataSource;
}
@Bean
public HibernateTransactionManager getTransactionManagerTest1() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(test1SessionFactory().getObject());
return transactionManager;
}

@Bean(name = "test2")
public LocalSessionFactoryBean test2SessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(test2DataSource());
sessionFactory.setPackagesToScan("com.example");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource test2DataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("jdbc.test2.driverClassName"));
dataSource.setUrl(environment.getProperty("jdbc.test2.url"));
dataSource.setUsername(environment.getProperty("jdbc.test2.username"));
dataSource.setPassword(environment.getProperty("jdbc.test2.password"));
return dataSource;
}
@Bean
public HibernateTransactionManager getTransactionManagerTest2() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(test2SessionFactory().getObject());
return transactionManager;
}

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"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
}
}

ConnectionDemo.java

import java.util.Date;import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConnectionDemo {
@Autowired
@Qualifier("test1")
SessionFactory db1;

@Autowired
@Qualifier("test2")
SessionFactory db2;

@GetMapping("/hello")
@ResponseBody
public String hello() {
System.out.println("db1 : " + db1);
System.out.println("db2 : " + db2);
System.out.println(new Date());
return "hello";
}
}

Output in console: