user4856296
user4856296

Reputation:

Configure Multiple Database on Spring-Boot with JPA and Hibernate Web Application

I have ONE spring boot (1.5.4.RELEASE) project using java 8 deployed on AWS HPC. This project architect scope works for Spring Web Application(Website), Rest API Services(Mobile Developer) & Account Administration for Company. So there is 3 different respective Database like (2-SQL Server & 1-MySQL).

Here on stack-overflow, I'm posting my question for find a best way to implementation this Spring-Boot Project by help of talented stack-overflow users.

Here is my configure properties files.

application.properties

#For Public Website
spring.datasource.clone.web.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.clone.web.url = jdbc:sqlserver://127.0.0.01\\dbo:1433;databaseName=PROD_WEB;
# Username and password
spring.datasource.web.username = web
spring.datasource.web.password = ED5FLW64ZU976Q36

#For Rest API
spring.datasource.clone.url = jdbc:mysql://localhost:3306/PROD_REST;
# Username and password
spring.datasource.clone.username = rest
spring.datasource.clone.password = Firewall77#


#For Account Administration for Company Users
spring.datasource.admin.url = jdbc:sqlserver://127.0.0.01\\dbo:1433;databaseName=PROD_ADMIN;
# Username and password
spring.datasource.admin.username = admin
spring.datasource.admin.password = Firewall77#

# Backup & Cron Policy
...

I would greatly appreciate for some very good suggestion to implement it. your knowledge on this subject would help me, Thanks.

Upvotes: 4

Views: 5421

Answers (2)

cнŝdk
cнŝdk

Reputation: 32145

You need to implement two different beans, one for each datasource and make them take the corresponding configuration properties respectively:

  1. First bean will be responsible for the first datasource configuration, and should be daclared as primary datasource with @Primary, so it can be setup as the main datasource for the project.
  2. The second bean will configure the second datasource.

This is how they should be implemented in Spring:

@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource.web")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.datasource.rest")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

Here's how should be your application.properties configured, to take these two beans configuration into account:

#For Public Website
spring.datasource.web.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.web.url = jdbc:sqlserver://127.0.0.01\\dbo:1433;databaseName= PROD_WEB;
# Username and password
spring.datasource.web.username = web
spring.datasource.web.password = ED5FLW64ZU976Q36

#For Rest API
spring.datasource.rest.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.rest.url = jdbc:mysql://localhost:3306/PROD_REST;
# Username and password
spring.datasource.rest.username = rest
spring.datasource.rest.password = Firewall77#

Note:

I changed the configuration properties here so they can be differentiated between web and rest datasources:

  1. Properties starting with spring.datasource.web will be dedicated to configure the first datasource.
  2. Properties starting with spring.datasource.rest will be dedicated to configure the second datasource.

Upvotes: 3

baba
baba

Reputation: 271

try to use DataSource configuration specified at Configuration for each data sources for more help check this Using multiple datasources with Spring Boot and Spring Data

Upvotes: 1

Related Questions