Git Product home page Git Product logo

multi-tenant-app-demo's Introduction

README

Sample Application to demonstrate Multi-tenancy based on discriminator field using Spring Boot & Hibernate.

Since hibernate (5.x) has some issues (refer the link below) in supporting the 'Discriminator' based multi-tenancy strategy. This following workaround will help to achieve the same.

Approach:

  • Uses hibernate filter to limit the query results based on tenant.
  • Uses hibernate interceptors to enforce tenant details during creating/updating entities.
  • Uses Spring AOP (AspectJ) to set the filter parameters.

Explanation:

  • Each request goes thru a custom servlet filter which checks for X-TenantID http header and set's it in the ThreadLocal variable using TenantContext class. If http header is not present in request, it'll be rejected.
  • Controller routes the request to Service class and the Spring AOP (UserServiceAspect class) intercepts the service call and set's the hibernate tenant filter.
  • All the service method has to be annotated with @Transactional for UserServiceAspect to work.
  • Above method works only for read queries, for write queries, we have to use hibernate interceptors.
  • Custom Entity interceptor (using EmptyInterceptor) class which sets the tenantId value during the save/delete/flush-dirty entity events.
  • Entity class should implement TenantSupport interface for the Entity interceptor to work.

Refer:

multi-tenant-app-demo's People

Contributors

ramsrib avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

multi-tenant-app-demo's Issues

Potential bug in the EmptyInterceptor

I know this is a demo, but in case someone follows it too literally, there's a likely issue that would occur with that kind of an Interceptor. Given that the tenantId is not the PK for most entities, this approach would (risk) assigning other tenant's entities to the ownership of the calling tenant.

findById

findById is not working, when i tring to getbyid (in header tenant1) but userId is id of user tanant2 in responce i get information about user2

doubt

Is this solution with filters and interceptor being used in any project in production?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.