In this post, We will learn How to Configure Multiple Data Sources in a Spring Boot using a demo project?
There are many situations where we need to read/write data from multiple databases and while using Spring data JPA sometimes it becomes challenging to configure to get connections with multiple databases in a single application. Hence, in this post, we are going to discuss creating multiple database connections with Spring data JPA using spring boot through a single application. Actually, spring boot usually provides a very easy way to use multiple data sources in a single application using properties file configurations.
Here We are going to learn How to use MySQL and Oracle databases to get connections in a single Spring Boot application.
Below is the Project Structure created in STS(Spring Tool Suite):
pom.xml
Maven pom with Spring data JPA,mysql-connector-java, and ojdbc8(for Oracle) dependencies.
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 |
<?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 https://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.5.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kkhindigyan.app</groupId> <artifactId>SpringBootDataJPAWithMultipleDatabaseExample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootDataJPAWithMultipleDatabaseExample</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-data-jpa</artifactId> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
We have two different config files (MySQLDBConfig & OracleDBConfig) for two different data sources.
Following is the configuration to connect to MySQL database. We have configured the entitymanager required to query the MySQL database as per JPA.
@ConfigurationProperties(prefix = “spring.mysql.datasource”). This will ensure that spring picks properties starting with spring.mysql.datasource
creating the data source and utilizes it while executing methods of UserRepository.java.
basePackages = “com.kkhindigyan.app.user.dao” will ensure that spring uses mysql datasource while executing methods of UserRepository.java
@Primary This annotation tells spring to use this bean to use as a primary bean as we have multiple beans for the same return type. To use other beans of the same return type we require to use @Qualifier
annotation.
MySQLDBConfig.java
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 |
package com.kkhindigyan.app.config; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "mysqlEntityManager", transactionManagerRef = "mysqlTransactionManager", basePackages = "com.kkhindigyan.app.user.dao") public class MySQLDBConfig { @Primary @Bean @ConfigurationProperties(prefix = "spring.mysql.datasource") public DataSource mysqlDataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = "mysqlEntityManager") public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder builder) { return builder.dataSource(mysqlDataSource()).properties(setHibernateProperties()) .packages("com.kkhindigyan.app.user.entities").build(); } @Primary @Bean(name = "mysqlTransactionManager") public PlatformTransactionManager mysqlTransactionManager( @Qualifier("mysqlEntityManager") EntityManagerFactory entityManagerFactory) { return new JpaTransactionManager(entityManagerFactory); } private Map<String, Object> setHibernateProperties() { Resource resource = new ClassPathResource("hibernate.properties"); try { Properties properties = PropertiesLoaderUtils.loadProperties(resource); Map<String, Object> propsMap = properties.entrySet().stream() .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue())); return propsMap; } catch (IOException e) { return new HashMap<String, Object>(); } } } |
OracleDBConfig.java
Following is the configuration for Oracle Database The configurations are similar to MySQLDBConfig.java.
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 |
package com.kkhindigyan.app.config; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "oracleEntityManager", transactionManagerRef = "oracleTransactionManager", basePackages = "com.kkhindigyan.app.emp.dao") public class OracleDBConfig { @Bean @ConfigurationProperties(prefix = "spring.oracle.datasource") public DataSource oracleDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "oracleEntityManager") public LocalContainerEntityManagerFactoryBean oracleEntityManagerFactory(EntityManagerFactoryBuilder builder) { return builder.dataSource(oracleDataSource()).properties(setHibernateProperties()) .packages("com.kkhindigyan.app.employee.entities") .build(); } @Bean(name = "oracleTransactionManager") public PlatformTransactionManager oracleTransactionManager( @Qualifier("oracleEntityManager") EntityManagerFactory entityManagerFactory) { return new JpaTransactionManager(entityManagerFactory); } private Map<String, Object> setHibernateProperties() { Resource resource = new ClassPathResource("hibernate.properties"); try { Properties properties = PropertiesLoaderUtils.loadProperties(resource); Map<String, Object> propsMap = properties.entrySet().stream() .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue())); return propsMap; } catch (IOException e) { return new HashMap<String, Object>(); } } } |
Employee.java
Employee JPA Entity class
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 |
package com.kkhindigyan.app.employee.entities; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name="employee_table") @DynamicUpdate public class Employee { @Id @Column(name="employee_id") @GeneratedValue(strategy=GenerationType.AUTO) private Integer employeeId; @Column(name="employee_name",length=100,nullable=false) private String employeeName; @Column(name="email",unique=true) private String email; @Column(name="date_of_joing") private Date doj; @Column(name="salary") private Double salary; public Integer getEmployeeId() { return employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getDoj() { return doj; } public void setDoj(Date doj) { this.doj = doj; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", email=" + email + ", doj=" + doj + ", salary=" + salary + "]"; } } |
User.java
User JPA Entity class
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 |
package com.kkhindigyan.app.user.entities; import java.time.LocalDate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.DynamicUpdate; @Entity @Table(name = "user_table") @DynamicUpdate public class User { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; @Column(name = "date_of_birth") private LocalDate dob; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public LocalDate getDob() { return dob; } public void setDob(LocalDate dob) { this.dob = dob; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", dob=" + dob + "]"; } } |
Following is the DAO interfaces that is responsible to query against MySQL and Oracle databases. Both interfaces(EmployeeRepository & UserRepository) extends CrudRepository
which has different CRUD methods such as create, findOne, delete etc.. and hence our DAO’s interfaces automatically inherit them which is available for our service classes to use. It’s spring data that will generate the implementations at run time for these CRUD methods. Hence, we don’t have to provide the implementations.
Notice the generic parameters in CrudRepository. Based on these parameters, Spring data will perform different CRUD operations at run time on our behalf.
EmployeeRepository.java
1 2 3 4 5 6 7 8 9 |
package com.kkhindigyan.app.emp.dao; import org.springframework.data.repository.CrudRepository; import com.kkhindigyan.app.employee.entities.Employee; public interface EmployeeRepository extends CrudRepository<Employee, Integer> { } |
UserRepository.java
1 2 3 4 5 6 7 8 9 |
package com.kkhindigyan.app.user.dao; import org.springframework.data.repository.CrudRepository; import com.kkhindigyan.app.user.entities.User; public interface UserRepository extends CrudRepository<User, Integer> { } |
Following are the service classes where we have injected both the DAOs. Here both the service classes communicate with the different DAOs and doing read/write operations from multiple databases.
UserService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.kkhindigyan.app.user.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.kkhindigyan.app.user.dao.UserRepository; import com.kkhindigyan.app.user.entities.User; @Service public class UserService { @Autowired private UserRepository userRepository; public User createUser(User user) { return userRepository.save(user); } public Iterable<User> fetchAllUsers(){ return userRepository.findAll(); } } |
EmployeeService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.kkhindigyan.app.emp.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.kkhindigyan.app.emp.dao.EmployeeRepository; import com.kkhindigyan.app.employee.entities.Employee; @Service public class EmployeeService { @Autowired private EmployeeRepository employeeRepository; public Employee createEmpoyee(Employee employee) { return employeeRepository.save(employee); } public Iterable<Employee> fetchAllEmployees(){ return employeeRepository.findAll(); } } |
Following is the application.properties the file that contains configurations for both MySQL and Oracle databases. You can notice that properties starting from spring.mysql.datasource has MySQL database configuration and properties starting from spring.oracle.data source has Oracle data source configurations. These configurations are used in the above MySQLDBConfig.java & OracleDBConfig.java while configuring entitymanager and transactionmanager for respective database connections.
application.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#MySQL Database Configration spring.mysql.datasource.jdbcUrl=jdbc:mysql://localhost:3306/test spring.mysql.datasource.username=root spring.mysql.datasource.password=root spring.mysql.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #Orcale Database Configration spring.oracle.datasource.jdbcUrl=jdbc:oracle:thin:@localhost:1521:ORCL spring.oracle.datasource.username=SH spring.oracle.datasource.password=pass123 spring.oracle.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.jpa.hibernate.ddl-auto=create-drop |
hibernate.properties
1 2 3 |
hibernate.show_sql=true hibernate.format_sql=true hibernate.hbm2ddl.auto=create |
SpringBootDataJpaWithMultipleDatabaseExampleApplication.java
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 |
package com.kkhindigyan.app; import java.time.LocalDate; import java.time.Month; import java.util.Date; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.kkhindigyan.app.emp.dao.EmployeeRepository; import com.kkhindigyan.app.emp.service.EmployeeService; import com.kkhindigyan.app.employee.entities.Employee; import com.kkhindigyan.app.user.dao.UserRepository; import com.kkhindigyan.app.user.entities.User; import com.kkhindigyan.app.user.service.UserService; @SpringBootApplication public class SpringBootDataJpaWithMultipleDatabaseExampleApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringBootDataJpaWithMultipleDatabaseExampleApplication.class, args); System.out.println("Creating & Reading User information using MySQL Database.."); UserService userService = applicationContext.getBean(UserService.class); userService.createUser(getUser()); userService.fetchAllUsers().forEach(System.out::println); System.out.println("--------------------------------------------------"); System.out.println("Creating & Reading Employee information using Oracle Database.."); EmployeeService employeeService = applicationContext.getBean(EmployeeService.class); employeeService.createEmpoyee(getEmployee()); employeeService.fetchAllEmployees().forEach(System.out::println); } private static Employee getEmployee() { Employee employee = new Employee(); employee.setEmployeeName("Sean"); employee.setDoj(new Date()); employee.setSalary(99000.00); return employee; } private static User getUser() { User user = new User(); user.setName("KK"); user.setAge(32); user.setDob(LocalDate.of(1990, Month.DECEMBER, 10)); return user; } } |
If you run ClientTest.java as Spring Boot App then it will give the below output:
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ [32m :: Spring Boot :: [39m [2m (v2.5.2)[0;39m [2m2021-07-17 11:32:31.664[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mpaWithMultipleDatabaseExampleApplication[0;39m [2m:[0;39m Starting SpringBootDataJpaWithMultipleDatabaseExampleApplication using Java 1.8.0_65 on DESKTOP-SJ5V0GM with PID 4816 (D:\JavaWorkSpaces\SpringBootWorkspace_1\SpringBootDataJPAWithMultipleDatabaseExample\target\classes started by Kishan Kumar in D:\JavaWorkSpaces\SpringBootWorkspace_1\SpringBootDataJPAWithMultipleDatabaseExample) [2m2021-07-17 11:32:31.668[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mpaWithMultipleDatabaseExampleApplication[0;39m [2m:[0;39m No active profile set, falling back to default profiles: default [2m2021-07-17 11:32:32.008[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode. [2m2021-07-17 11:32:32.046[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 30 ms. Found 1 JPA repository interfaces. [2m2021-07-17 11:32:32.047[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode. [2m2021-07-17 11:32:32.051[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 3 ms. Found 1 JPA repository interfaces. [2m2021-07-17 11:32:32.454[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default] [2m2021-07-17 11:32:32.496[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.Version [0;39m [2m:[0;39m HHH000412: Hibernate ORM core version 5.4.32.Final [2m2021-07-17 11:32:32.498[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.cfg.Environment [0;39m [2m:[0;39m HHH000205: Loaded properties from resource hibernate.properties: {hibernate.show_sql=true, hibernate.bytecode.use_reflection_optimizer=false, hibernate.hbm2ddl.auto=update, hibernate.format_sql=true} [2m2021-07-17 11:32:32.622[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.hibernate.annotations.common.Version [0;39m [2m:[0;39m HCANN000001: Hibernate Commons Annotations {5.1.2.Final} [2m2021-07-17 11:32:32.739[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-1 - Starting... [2m2021-07-17 11:32:32.875[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-1 - Start completed. [2m2021-07-17 11:32:32.901[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.dialect.Dialect [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect Hibernate: create table hibernate_sequence ( next_val bigint ) engine=InnoDB Hibernate: insert into hibernate_sequence values ( 1 ) Hibernate: create table user_table ( id integer not null, age integer, date_of_birth date, name varchar(255), primary key (id) ) engine=InnoDB [2m2021-07-17 11:32:33.429[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.h.e.t.j.p.i.JtaPlatformInitiator [0;39m [2m:[0;39m HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] [2m2021-07-17 11:32:33.437[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default' [2m2021-07-17 11:32:33.451[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default] [2m2021-07-17 11:32:33.457[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-2 - Starting... [2m2021-07-17 11:32:33.457[0;39m [33m WARN[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.zaxxer.hikari.util.DriverDataSource [0;39m [2m:[0;39m Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation. [2m2021-07-17 11:32:33.829[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-2 - Start completed. [2m2021-07-17 11:32:33.831[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.dialect.Dialect [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect Hibernate: create table employee_table ( employee_id number(10,0) not null, date_of_joing timestamp, email varchar2(255 char), employee_name varchar2(100 char) not null, salary double precision, primary key (employee_id) ) Hibernate: alter table employee_table drop constraint UK_2casspobvavvi9s23312f9mhm Hibernate: alter table employee_table add constraint UK_2casspobvavvi9s23312f9mhm unique (email) [2m2021-07-17 11:32:34.247[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.h.e.t.j.p.i.JtaPlatformInitiator [0;39m [2m:[0;39m HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] [2m2021-07-17 11:32:34.247[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default' [2m2021-07-17 11:32:34.518[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ main][0;39m [36mpaWithMultipleDatabaseExampleApplication[0;39m [2m:[0;39m Started SpringBootDataJpaWithMultipleDatabaseExampleApplication in 3.214 seconds (JVM running for 3.856) <span style="color: #0000ff;">Creating & Reading User information using MySQL Database..</span> Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: insert into user_table (age, date_of_birth, name, id) values (?, ?, ?, ?) Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.date_of_birth as date_of_3_0_, user0_.name as name4_0_ from user_table user0_ User [id=1, name=KK, age=32, dob=1990-12-10] -------------------------------------------------- <span style="color: #0000ff;">Creating & Reading Employee information using Oracle Database..</span> Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into employee_table (date_of_joing, email, employee_name, salary, employee_id) values (?, ?, ?, ?, ?) Hibernate: select employee0_.employee_id as employee_id1_0_, employee0_.date_of_joing as date_of_joing2_0_, employee0_.email as email3_0_, employee0_.employee_name as employee_name4_0_, employee0_.salary as salary5_0_ from employee_table employee0_ Employee [employeeId=22, employeeName=Sean, email[email protected], doj=2021-07-17 11:32:34.683, salary=99000.0] [2m2021-07-17 11:32:34.909[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Closing JPA EntityManagerFactory for persistence unit 'default' [2m2021-07-17 11:32:34.909[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-2 - Shutdown initiated... [2m2021-07-17 11:32:34.997[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-2 - Shutdown completed. [2m2021-07-17 11:32:34.997[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Closing JPA EntityManagerFactory for persistence unit 'default' [2m2021-07-17 11:32:34.998[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-1 - Shutdown initiated... [2m2021-07-17 11:32:35.002[0;39m [32m INFO[0;39m [35m4816[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-1 - Shutdown completed. |
That’s all about How to Configure Multiple Data Sources in a Spring Boot?
Github Link to Download Source Code
If you have any feedback or suggestion please feel free to drop in below comment box.