Making SystemPropertyTenantResolver work


I use SystemPropertyTenantResolver for testing purposes. One use-case is building a simple app to see how some aspects of multitenancy works. The other use-case is testing with Spock.

Minimum necessary code to setup multitenancy and select one tenant.

First in application.yml enable multitenancy.

grails:
    gorm:
        multiTenancy:
            tenantResolverClass: org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver
            mode: DATABASE

Then in application.yml define datasources for each tenant. In this example the tenant name is “abc”:

dataSources:
    abc:
        dbCreate: create-drop
        driverClassName: org.h2.Driver
        username: sa
        password: ''
        url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

If you have only one tenant, you can set it in BootStrap.groovy within the def init closure:

def init = { servletContext ->
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'abc')
}

Testing with SystemPropertyTenantResolver

Integration test and unit testing are configured by selecting the from within the setupSpec method.

    def setupSpec() {
        System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'abc')
    }

In an application that uses another TenantResolver for production you can define it within the development environment in application.yml:

environments:
    development:
        grails:
            gorm:
                multiTenancy:
                    tenantResolverClass: org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver

,