Giter Club home page Giter Club logo

blog-multitenancy's Introduction

Multi Tenancy with Spring Boot, Hibernate & Liquibase

Overview

Multi Tenancy usually plays an important role in the business case for SAAS solutions. Spring Data and Hibernate provide out-of-the-box support for different Multi-tenancy strategies. Configuration however becomes more complicated, and the available examples are few.

This project complements my blog series on Multi Tenancy (see https://callistaenterprise.se/blogg/teknik/2020/09/19/multi-tenancy-with-spring-boot-part1/), and contains working examples of different Multi Tenant strategies implemented with Spring Boot, Hibemate and Liquibase, complete with support for database migrations as well as dynamically set up new tenants on the fly.

How to use the examples

The master branch contains a common, minimal example project skeleton. The different Multi-tenancy strategy examples are in separate branches.

Database per tenant

The database branch implements the Database per tenant strategy.

Schema per tenant

The schema branch implements the Schema per tenant strategy.

Shared Database with Discriminator, using Hibernate Filters

The shared_database_hibernate branch implements the Shared Database with Discriminator strategy, using Hibernate's experimental support for discriminator-based multi-tenancy (see e.g. https://hibernate.atlassian.net/browse/HHH-6054)

Shared Database with Discriminator, using PostgreSQL's Row Level Security

The shared_database_postgres_rls branch implements the Shared Database with Discriminator strategy, using PostgreSQL's Row Level Security.

How to start a Dockerized postgres database

All the examples require a postgres database running at localhost:5432. Run the following command to use the provided docker-compose.yml configuration to start a dockerized postgres container:

docker-compose up -d

Close it down with the following command when done, or if you need to recreate the database:

docker-compose down

blog-multitenancy's People

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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blog-multitenancy's Issues

Test don't work

Hi
the tests on the Service layer don't appear to work.

If I run: mvn '-Dtest=se.callista.blog.tenant_management.service.*.Test' test
it produces an error relating to no tests found...

Not able to execute the cURL command for testing

When i execute the cURL command, i get the below error. Not sure what is the issue, i tried removing the Lombak dependencies from the ProductValue class but still no success.
curl -H "X-TENANT-ID: tenant1" -H "Content-Type: application/se.callista.blog.service.api.product.v1_0+json" -X POST -d '{"name":"Product 1"}' localhost:8080/products

2020-12-08 20:46:30.335 WARN 13848 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/se.callista.blog.service.api.product.v1_0+json;charset=UTF-8' not supported]
2020-12-08 20:46:30.365 WARN 13848 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap]

tenant relation does not exist error when running the services with mvn

Hi,
First of all, thank you for publishing this project.
I am trying to use your project as a reference to introduce Multitenancy to Postgres in my project.
I have tried to test your implementation but I am running into an error in both services when I run the maven run command out of the box:

  • in multi-tenant-service with the following error:
    2022-11-14 08:11:03.864 INFO 9419 --- [ restartedMain] micSchemaBasedMultiTenantSpringLiquibase : Schema based multitenancy enabled 2022-11-14 08:11:03.900 WARN 9419 --- [ restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01 2022-11-14 08:11:03.900 ERROR 9419 --- [ restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: relation "tenant" does not exist Position: 79 2022-11-14 08:11:03.907 WARN 9419 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tenantLiquibase' defined in class path resource [se/callista/blog/service/multitenancy/config/tenant/TenantLiquibaseConfig.class]: Invocation of init method failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
  • in multi-tenant-reactive-service with the following error:
    2022-11-14 08:06:20.572 WARN 8710 --- [ restartedMain] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tenantLiquibase' defined in class path resource [se/callista/blog/service/multitenancy/config/tenant/TenantLiquibaseConfig.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SELECT * FROM Tenant]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "tenant" does not exist Position: 15

it seems that in both services, the application cannot find the tenant table (tenant table and related schema were not created) which is supposed to be in the master repository where tenant-related information is saved. I am expecting that at least a schema is created automatically at startup which contains the tenant table, but this is not the case.
did I miss doing some configuration here or am I misunderstanding / forgetting something?

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.