Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2e52955

Browse files
update read me
1 parent 509e6ae commit 2e52955

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

‎readme.md‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
# Dynamic-Datasource-Routing!
2-
Lest first start with discussing one use-case. We might encounter the requirement of changing DATASOURCE dynamically. For example you are writing one kickass RESTful api and the your clients are your local banks banks. Banks doesn't want to use the database use provide each of the banks want to have their own database integrated with your service. So you have one RESTful api but multiple database.
32

4-
You can distinguish requests from banks from data from the request header or from request path information.
5-
Spring 2.0.1 introduced an `AbstractRoutingDataSource` which comes in handy in this regard. The general idea is that a routing DataSource acts as an intermediary - while the ‘real’ DataSource can be determined dynamically at runtime
3+
## Use case
4+
Let's first start by discussing one use case. We might encounter the requirement of changing DATASOURCE dynamically. For example, you are writing one kickass RESTful API and your clients are local bank banks. Banks don't want to use the database; they want to have their own database integrated with your service. So you have one RESTful API but multiple databases.
5+
6+
You can distinguish requests coming from the banks, from the request header or path information.
7+
Spring 2.0.1 introduced an `AbstractRoutingDataSource` , which is handy in this regard. The general idea is that a routing DataSource acts as an intermediary - while the 'real' DataSource can be determined dynamically at runtime.
68

79
## Technology Used
810
- Spring Boot 2.2.0
911
- MySQL
1012
- Spring Data JPA
1113

1214
## Objectives
13-
- We will create two database ( **database1** & **database2**). Each of the database will have one table **TBL_EMPLOYEE**
14-
- We will expose one **GET** api with path having **/{bank}/employees**. Path variable **{bank}** will be replaced in run time. Example:
15-
-- http://localhost:8080/bank1/employees
16-
-- http://localhost:8080/bank2/employees
17-
- If context path starts with **bank1** application will serve data from the **database1**, if it starts with **bank2** then from **database2** and so on.
15+
- We will create two database ( **database1** & **database2**). Each of the database will have one table **TBL_EMPLOYEE**
16+
- We will expose a **GET** API with a path variable **/{bank}/employees**. Path variable **{bank}** will be replaced in run time. Example:
17+
-- http://localhost:8080/bank1/employees -- http://localhost:8080/bank2/employees
18+
- If the path variable is **bank1** application will serve data from the **database1**, if it is **bank2** then from **database2** and so on.
1819

19-
## Target Overview
20-
- We will introduce one interceptor, Which will read the request context decides which database to use
21-
- We will not let spring boot auto-configuration to come in our way regarding data source configuration. We will disable data-source and transac-manager auto-configuration.
22-
- We will create the two data source **datasurce1**, **datasoruce2** and one **transactionManager**, one **entityManagerFactory** manually
23-
- application properties will have database related properties for both **datasource**. For example:
20+
## How we are going to do it?
21+
- We will write one interceptor, which will read the path variable, and based on the path variable we will decide which database to use
22+
- We will not let spring boot auto-configuration come our way to configure the data source. So we will disable the data-source and transaction-manager auto-configuration.
23+
- We will configure two data source **datasurce1**, **datasoruce2** and one **transactionManager**, one **entityManagerFactory** manually
24+
- We will provide the corresponding property in the **application.properties** file to configure both data sources. Properties will look like the following:
2425
<pre>
2526
spring.datasource.driver-class-name.1=com.mysql.cj.jdbc.Driver
2627
spring.datasource.url.1=jdbc:mysql://localhost:3306/database1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /