RDBMS : Multitenancy

On occasion you need to share a data model with other user-groups or other applications and where the model is persisted to the same structure of datastore. There are three ways of handling this with DataNucleus.

  • Separate Database per Tenant - have a different database per user-group/application.
  • Separate Schema per Tenant - as the first option, except use different schemas.
  • Same Database/Schema but with a Discriminator - this is described below.
Multitenancy via Discriminator

If you specify the persistence property datanucleus.tenantId as an identifier for your user-group/application then DataNucleus will know that it needs to provide a tenancy discriminator to all primary tables of persisted classes. This discriminator is then used to separate the data of the different user-groups.

By default this will add a column TENANT_ID to each primary table, of type VARCHAR(255) . You can control this by specifying extension metadata for each persistable class

<class name="MyClass">
     <extension vendor-name="datanucleus" key="multitenancy-column-name" value="TENANT"/>
     <extension vendor-name="datanucleus" key="multitenancy-column-length" value="24"/>

In all subsequent use of DataNucleus, any INSERT to the primary table(s) will also include the TENANT column value. Additionally any query will apply a WHERE clause restricting to a particular value of TENANT column.

If you want to disable multitenancy on a class, just specify the following metadata

<class name="MyClass">
     <extension vendor-name="datanucleus" key="multitenancy-disable" value="true"/>

This is new in DataNucleus AccessPlatform 3.0.2