Class TenantDataSources
java.lang.Object
com.codename1.backend.tenancy.TenantDataSources
One DataSource per tenant -- the backend's counterpart of Spring's
AbstractRoutingDataSource -- for a database, or a schema, per tenant.
Each pool opens on first use. ${tenant} stands for the identifier in the
URL template and in the connection-init template:
- A database per tenant:
postgres://app:secret@db/app_${tenant}ordata/t_${tenant}.db, no init statement. - A schema per tenant: one URL and an init statement that pins every
connection of the tenant's pool to its schema,
SET search_path TO tenant_${tenant}on PostgreSQL orUSE tenant_${tenant}on MySQL.
Pools can also be registered with register(String, DataSource). At most maxPools opened
pools stay open; the least recently used one is closed when another is
needed (its borrowed connections close as they come back). Registered pools
are never evicted.
TenantDataSources tenants = TenantDataSources.fromConfig(config);
tenants.migrateAll(Arrays.asList("acme", "globex"), config);
EntityManager entities = EntityManager.open(tenants.current());
-
Constructor Summary
ConstructorsConstructorDescriptionTenantDataSources(String template, int poolSize, int maxPools) A database per tenant.TenantDataSources(String template, String initTemplate, int poolSize, int maxPools) A database or a schema per tenant. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes every pool this object opened.current()The pool of the current tenant.The pool oftenant, opening it when needed.static TenantDataSourcesfromConfig(Config config) Readscn1.tenancy.datasource.url(falling back tocn1.datasource.url),cn1.tenancy.datasource.init,cn1.tenancy.datasource.pool.sizeandcn1.tenancy.datasource.maxPools.intmigrateAll(Iterable<String> tenants, Config config) Runs the application's migrations against every tenant intenants, one after another; the first failure stops the run and names the tenant.voidregister(String tenant, DataSource pool) Registers a pool someone else opened fortenant.
-
Constructor Details
-
TenantDataSources
A database per tenant.- Parameters:
template- the URL with${tenant}in it, or null to accept only registered poolspoolSize- connections per tenant poolmaxPools- how many template pools stay open at once
-
TenantDataSources
A database or a schema per tenant.- Parameters:
template- the URL, or null to accept only registered poolsinitTemplate- the statement each connection runs as it opens, or nullpoolSize- connections per tenant poolmaxPools- how many template pools stay open at once- Throws:
IllegalArgumentException- when neither template names${tenant}, which would hand every tenant the same database
-
-
Method Details
-
fromConfig
Readscn1.tenancy.datasource.url(falling back tocn1.datasource.url),cn1.tenancy.datasource.init,cn1.tenancy.datasource.pool.sizeandcn1.tenancy.datasource.maxPools.- Parameters:
config- the configuration- Returns:
- the pools
- Throws:
IOException- when no URL is configured
-
register
Registers a pool someone else opened fortenant.- Parameters:
tenant- the tenant identifierpool- its pool
-
current
The pool of the current tenant.- Returns:
- the pool
- Throws:
IOException- when it cannot be openedMissingTenantException- when no tenant is current
-
forTenant
The pool oftenant, opening it when needed.- Parameters:
tenant- the tenant identifier- Returns:
- the pool
- Throws:
IOException- when it cannot be opened
-
migrateAll
Runs the application's migrations against every tenant intenants, one after another; the first failure stops the run and names the tenant.- Parameters:
tenants- the tenantsconfig- the configuration the migrations read- Returns:
- the total number of migrations that ran
- Throws:
IOException- when a database fails
-
close
public void close()Closes every pool this object opened. Registered pools stay open.
-