How to Implement Dynamic Multi-Tenant Database Switching Per Session in Mendix?

0
Hello everyone, I am working on a scenario where I need to build a Single Mendix application with multiple database connectivity (multi-tenant architecture), and I’d appreciate any guidance, suggestions, or examples from the community. Scenario Overview- The application has an Admin who can:  - Create Tenants (each having an ID, Name, and Database Type).  - Maintain a Credential entity (JDBC URL, username, and password) for each tenant’s database.  - Create accounts for tenant users. - Tenant users log in to the same application URL.  - After login, they are shown their Dashboard, where a button navigates to a page displaying data isolated to that tenant’s own database (in this case, just a list of names).  - Each tenant manages their own names independently of other tenants. Technical Requirement Here is the key challenge I am facing:  When the tenant user clicks the “View Data” button on their dashboard, Mendix should dynamically switch the default database connection to the one specified in their credentials (`JDBC URL`, `username`, `password`). For the duration of their session, all CRUD operations should occur against the tenant’s assigned database. When the tenant logs out, the connection should switch back to the Mendix default database (where admin entities and other common data are stored).  Throughout the process, session management and data isolation must remain intact, ensuring users don’t see each other’s data. What I’ve Tried So Far- Custom Java actions to set up a JDBC connection dynamically. - Storing the tenant configuration in the session context to persist the tenant-specific connection. - However, I couldn’t find examples of how to replace the connection dynamically at runtime per session, nor how to ensure all entity queries go to the correct DB. My Questions  Is there an established pattern or recommended approach for multi-tenant dynamic database switching in Mendix? Can this be handled cleanly via Java actions, or is there a better way to hook into Mendix persistence at runtime? How can I ensure proper session management so after logout the system reverts to the default database safely? Are there examples or modules available that demonstrate this level of dynamic connection handling? Any examples, documentation links, or advice on pitfalls would be greatly appreciated. Thank you so much in advance for your help.
asked
1 answers
1

Hello Siddharth,

 

Mendix do not support multiple database connections by default; but you can achieve this by using database-connector module.

Database | Mendix Documentation

 

Try something like, open a separate database connections based the user type and execute queries.

 

 

answered