Class TenantContext
java.lang.Object
com.codename1.backend.tenancy.TenantContext
The tenant of the work running on the current thread, the backend's
counterpart of Hibernate's CurrentTenantIdentifierResolver backed by a
thread local.
A request gets its tenant from TenantFilter; a task submitted to an
executor carries it across through decorator(). Identifiers are 1 to 64
characters of [A-Za-z0-9_.-], because they are written into SQL and into
database and schema names.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectRunsworkwith native SQL allowed on sessions that map tenant-partitioned entities.static voidclear()Clears the current tenant.static TaskDecoratorA decorator that carries the submitting thread's tenant into the task, forTasks.executor(name).setTaskDecorator(...).static Stringget()The current tenant, or null when none is set.static booleanTrue insideallowNativeSql(TenantContext.Work).static booleanTrue insiderunUnscoped(TenantContext.Work).static ObjectrunAs(String tenant, TenantContext.Work work) Runsworkwithtenantcurrent, then restores what was current before.static ObjectRunsworkwith tenant filtering switched off, for a migration, a report across tenants or a job that provisions one.static voidSets the current tenant.
-
Method Details
-
get
The current tenant, or null when none is set.- Returns:
- the tenant identifier
-
set
Sets the current tenant. PreferrunAs(String, TenantContext.Work), which cannot leak the tenant into the next request served by this thread.- Parameters:
tenant- the tenant identifier- Throws:
IllegalArgumentException- when the identifier is not valid
-
clear
public static void clear()Clears the current tenant. -
runAs
Runsworkwithtenantcurrent, then restores what was current before.- Parameters:
tenant- the tenant identifierwork- the work- Returns:
- what the work answered
- Throws:
Exception- whatever the work throws
-
runUnscoped
Runsworkwith tenant filtering switched off, for a migration, a report across tenants or a job that provisions one. Entities saved inside keep whatever tenant they already name. There is deliberately no switch that turns filtering off for longer than one call.- Parameters:
work- the work- Returns:
- what the work answered
- Throws:
Exception- whatever the work throws
-
isUnscoped
public static boolean isUnscoped()True insiderunUnscoped(TenantContext.Work).- Returns:
- whether filtering is off on this thread
-
allowNativeSql
Runs
workwith native SQL allowed on sessions that map tenant-partitioned entities. Native statements are never rewritten, so outside this (orrunUnscoped(TenantContext.Work)) creating one on such a session throwsTenantNativeSqlException. Inside it the statement must carry the tenant condition itself:TenantContext.allowNativeSql(() -> session.createNativeQuery( "SELECT COUNT(*) FROM invoice WHERE tenant_id = :t", Long.class) .setParameter("t", TenantContext.get()).first());- Parameters:
work- the work- Returns:
- what the work answered
- Throws:
Exception- whatever the work throws
-
isNativeSqlAllowed
public static boolean isNativeSqlAllowed()True insideallowNativeSql(TenantContext.Work).- Returns:
- whether native SQL is allowed on this thread
-
decorator
A decorator that carries the submitting thread's tenant into the task, forTasks.executor(name).setTaskDecorator(...). An unscoped caller's task is NOT unscoped: that is a decision each piece of work makes.- Returns:
- the decorator
-