I am interested at implementing a multitenancy solution using hibernate as provider and SQLServer as database. I am using the schema based approach meaning one database and different schemas.
Specifically, my question is how to alter the execution schema with SQLServer. I have used this approach with MySQL use $database
,
PostgreSQL SET search_path TO $schema
and Oracle ALTER SESSION SET CURRENT_SCHEMA = $schema
and i was able to successfully switch from one schema to an other, noting that the schema concept differs from one database to another.
However, with SQLServer, I understand that there's no such thing as altering execution schema. I am aware of the possibility of switching the default schema of a given user in case the user is not granted the sysadmin role. I am also aware of the impersonation concept using the folllwing query : EXECUTE AS USER = $user
. Impersonation allows one user to impersonate an other user, which gives the possibility to change the current user.
Switching the current user allows by definition to switch the execution schema since once the user is changed the execution schema is the new user's default schema. However, the impersonation has a limitation since we can't perform impersonation over 32 times.Although it's possible to execute revert
after each impersonation in order to avoid reaching the limit impersonation number, this solution is not suitable in my case and I'm looking for an alternative.
Does anyone have any suggestions about multitenancy implementation using SQLServer and a schema based approach?
Also is there any solutions other than the ones I mentioned to switch execution schema.
Your help is very much appreciated.Thank you so much.