Spring Boot — Multiple DB Connect using Hibernate | Spring Boot

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>multiple_db_connect</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>multiple_db_connect</name>
<description>Demo project for Spring Boot</description>
<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>
hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update
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
package com.example;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");
}
}
package com.example.configure;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;
}
}
package com.example.controller;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";
}
}
db1 : org.hibernate.internal.SessionFactoryImpl@768ad1d6
db2 : org.hibernate.internal.SessionFactoryImpl@9740b6c

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Monitors of the Trendyol Order Management System

From top-down to bottom-up: a three-decade journey from complexity to simplicity

Star Wars Sand Crawler with a bunch of bricks and characters. Boba Fett fell over, and it’s meant to show a lot from a little

Java/ Apply Character Formatting to Text in Word Document

Latest DevSecOps Best Practices Review for Docker & Kubernetes Security

Understanding the Power of Streams in Java

Golang and Ruby on the Arduino

Nginx Reverse Proxy for S3 — Docker

AWS Autoscaling and Elastic Load Balancing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Communication Between Microservices | Spring WebClient

Spring Cloud Gateway Authentication with Auth0

Basic Spring Security tips before going into production