my-spring-boot-experiments
  • README
  • Spring Batch implementation
  • API Example for Archunit
  • spring-boot-chaos-monkey
  • Grafana LGTM Implementation
  • MongoDb for insertion and search using elastic search
  • OpenSearch Implementation
  • spring-boot-rabbitmq-thymeleaf
  • boot-rest-docs-sample
  • boot-strategy-plugin
  • spring-boot-togglz-sample
  • spring-boot-ultimate-redis
  • GraphQL
    • spring-boot-graphql-querydsl
    • GraphQl implementation using webflux
    • GraphQl implementation using webmvc
  • httpClients
    • http-proxy
    • rest-template
    • rest-client
    • web-client-mvc
    • web-client-webflux
  • jmh-benchmark
  • jpa
    • Custom Sequence
    • Hibernate Envers Implementation
    • multiple datasources using Spring Boot
    • spring-boot-hibernate2ndlevelcache-sample
    • JNDI in embedded Tomcat
    • JPA Jooq Marriage
    • JPA locks implementation
    • read-replica-with-spring-boot
    • KeySet Pagination Using Blaze
    • KeySet Pagination Using Data-JPA
    • MultiTenancy using Hibernate in Spring Data JPA
      • multitenancy-db
      • multidatasource-multitenancy
      • Partitioned (Discriminator) Data – the data for each tenant is partitioned by a discriminator value
      • schema
  • open-api-spring-boot
  • r2dbc
    • r2dbc-jooq
    • PostgreSQL JSON and enum column support
    • PostgreSQL Notify and Listen support using reactive spring boot
    • r2dbc-boot
    • reactive-cache
  • scheduler
    • Scheduling using JobRunr
    • Scheduling using Quartz
    • Scheduling using Shedlock distribution
  • Code Of Conduct
Powered by GitBook
On this page
  • Application Flow
  • How It Works
  • Run tests
  • Run locally
  • Useful Links

Was this helpful?

Edit on GitHub
  1. jpa
  2. MultiTenancy using Hibernate in Spring Data JPA

multidatasource-multitenancy

Demonstrates a scenario where a single Spring Boot application connects to multiple databases and also implements multi-tenant logic (schema-based or DB-based).


Application Flow

---
title: Application Data fetching strategy
---
flowchart TD
    A([Appication]) -->|Get Data| B{Where should I get data}
    B --> |primary| C[(Oracle)]
    B --> |secondary| D[(Postgres)]
    C --> |TenantId| E[Discriminator based MultiTenancy]
    D --> |Schema| F[Schema based MultiTenancy]
    E --> |Choose Tenant |G{tenant1 or tenenat2}
    F --> |Choose Schema |H{schema1 or schema2}
    G --> I(((END)))
    H --> I

How It Works

  1. Multiple DataSources: Oracle for certain domain objects, Postgres for others.

  2. MultiTenancy: Switches which schema or DB to use based on an incoming tenant identifier.

  3. TenantInterceptor: Captures request-scope tenant info and sets it in the TenantIdentifierResolver.


Run tests

./mvnw clean verify

Run locally

docker-compose -f docker/docker-compose.yml up -d
./mvnw spring-boot:run -Dspring-boot.run.profiles=local

Useful Links

  • Swagger UI: http://localhost:8080/swagger-ui.html

  • Actuator Endpoint: http://localhost:8080/actuator

  • PgAdmin : http://localhost:5050

Previousmultitenancy-dbNextPartitioned (Discriminator) Data – the data for each tenant is partitioned by a discriminator value

Last updated 4 months ago

Was this helpful?