I'm building a multi-tenant WebApp. For ease of maintenance and operation, I would like to have a single WebApp (WAR) in a single Tomcat instance (JVM process) handling the requests of all tenants.
Tenants (my customers) can be added and removed dynamically in the system (using an Administration WebApp). Data for each tenant are stored in dedicated database SCHEMA (different user/password and even database instances). My question is: how can I add/removed datasource programmatically in a running Tomcat and WebApp? I know out to declare one or multiple static datasource, which typically means adding the following XML nodes in the specified files [<WAR_HOME>/WEB-INF/web.xml] <web-app> <resource-ref> <description>My DataSource</description> <res-ref-name>jdbc/myds</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> </web-app> [<WAR_HOME>/META-INF/context.xml] <Context path="/dictao-d3s-authority-web"> <ResourceLink name="jdbc/msDS" global="jdbc/myDS" type="javax.sql.DataSource" /> </Context> [<TOMCAT_HOME>/server.xml] <Server> <GlobalNamingResources> <Resource name="jdbc/myDS" auth="Container" url="…" user="…" password="…" type="javax.sql.DataSource" driverClassName="…" maxActive="10" maxIdle="5"/> </GlobalNamingResources> </Server> I'm trying to do that programmatically, for each new tenant added to my system, I would like to add the corresponding resource-ref, ResourceLink and Resource dynamically using some code running inside my Administration WebApp. Any thought ?