Building Multi-Tenant Applications - Dynamic Connection Strings


A multi-tenant application is an application that is shared by many different customers. is a good example of a multi-tenant application. There are typically two different approaches to building a multi-tenant application. These are:

  • All tenants share the same database, but each table in the database has a 'tenantId' field which identifies the tenant. Every query that is executed includes the tenantId in the WHERE clause. The tentantId is set when the customer logs into the application.

  • Each tenant has its own database.

The second approach is much easier to code because you don't have worry about adding the 'tenantId' field into every query and insert statement. However, it is less scalable because you need to provision a new database for every tenant. To support the second approach it is necessary to have 'dynamic' named connections. Here is how dynamic connections work: You must name your connection string 'DynamicConnection_<some name of your choice>'. For example:


Then, after the user has logged in, you must have an A5W page that sets a protected session variable called '__protected__<some name of your choice>'. For example:

session.__protected__MyAppConnection = "Your connection string"
Dynamic connections only apply to SQL Grid and UX components. They do NOT apply to old style (V10 and prior) Dialog components, or to any Xbasic code that you add to .A5W pages yourself. However, in any Xbasic code you write, if instead of opening the connection yourself (using the connection object's .open()) method you use the a5SQLConnectionOpen function, your dynamic connection strings will be resolved correctly.

When the user runs a Grid or UX component, if the named connection string starts with 'DynamicConnection_', then Alpha Anywhere will look for the appropriate protected session variable. If the variable exists, the named connection will be resolved to the value of the protected session variable. If the variable does not exist, then the named connection string will be resolved in the standard way, by looking up its value from data defined in the a5_application.a5i file.

See Also