Class TenantContext

java.lang.Object
com.codename1.backend.tenancy.TenantContext

public final class TenantContext extends Object

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.

  • Method Details

    • get

      public static String get()
      The current tenant, or null when none is set.
      Returns:
      the tenant identifier
    • set

      public static void set(String tenant)
      Sets the current tenant. Prefer runAs(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

      public static Object runAs(String tenant, TenantContext.Work work) throws Exception
      Runs work with tenant current, then restores what was current before.
      Parameters:
      tenant - the tenant identifier
      work - the work
      Returns:
      what the work answered
      Throws:
      Exception - whatever the work throws
    • runUnscoped

      public static Object runUnscoped(TenantContext.Work work) throws Exception
      Runs work with 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()
      Returns:
      whether filtering is off on this thread
    • allowNativeSql

      public static Object allowNativeSql(TenantContext.Work work) throws Exception

      Runs work with native SQL allowed on sessions that map tenant-partitioned entities. Native statements are never rewritten, so outside this (or runUnscoped(TenantContext.Work)) creating one on such a session throws TenantNativeSqlException. 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()
      Returns:
      whether native SQL is allowed on this thread
    • decorator

      public static TaskDecorator decorator()
      A decorator that carries the submitting thread's tenant into the task, for Tasks.executor(name).setTaskDecorator(...). An unscoped caller's task is NOT unscoped: that is a decision each piece of work makes.
      Returns:
      the decorator