Multi-tenancy in Mendix - Part I

Published in
4 min readMay 27, 2021

--

Hello folks. I hope everyone is doing well in this trying time. In this blog, I will talk about an important topic - cloud computing and SaaS (Software as a service), also known as Multi-tenancy.

Multi-tenancy is about more than just a SaaS implementation: a lot of enterprises wanted to isolate their data based on their business units and their domain verticals.

First I would like to take a moment to explain what is multi-tenancy, for those of you who are not familiar with it.

The term “multi-tenancy” refers to a software architecture in which a single instance of software runs on a server and serves multiple tenants. For more clarity, I would recommend everyone to go through the following wikipedia entry Multitenancy — Wikipedia

In general, we have two options in achieving multi-tenancy in any platform.

  1. Shared database for all the tenants.
  2. Separate database or Schema for each tenant.

We are going to discuss option 2 - Separate database or Schema’s for each tenant.

The term “schema” refers to the organization of data as a blueprint of how the database is constructed — wikipedia

Manipulating the Database

We will be using PostgreSQL as a backend in order to do the database manipulation.

There has to be an interface for an admin to create the tenants dynamically. Through this interface, the admin can name the tenants and initiate the database creation at run-time.

We will achieve this in four steps, a microflow will invoke the Java action, which in turn invokes the batch file, which in turn invokes the sql script file.

  • Step 1 - Mendix
  • Step 2 - Java Actions
  • Step 3 - Batch file (Windows)
  • Step 4 - SQL Script file.
https://bit.ly/MXW21
https://bit.ly/MXW21

We will need the help of a Marketplace module to support this implementation, the Database connector. This will help retrieve the data from all the external data sources.

Create a microflow and call a java action

Assume that the NewSchema object will be created and passed as an input parameter to this microflow, following with the piece of java code in the java action by specifying the file path and batch file name.

This piece of code under executeAction() method invokes the batch file and the batch file script should look like as follows.

The sql script file would have the script as follows.

SQL Script

So, with these mechanisms you would be able to create the separate database or schema for each tenant.

We will see how the user will post and retrieve data from the created database or schema in the next part.

Until then. Stay safe, stay happy, stay foolish.

Read more

From the Publisher -

If you enjoyed this article you can find more like it at our Medium page or at our own Community blog site.

For the makers looking to get started, you can sign up for a free account, and get instant access to learning with our Academy.

Interested in getting more involved with our community? You can join us in our Slack community channel or for those who want to be more involved, look into joining one of our Meet ups.

--

--