Class TenantScope

java.lang.Object
org.chenile.core.tenant.TenantScope

public class TenantScope extends Object
Runs work as one tenant, so that every database connection opened inside it goes to that tenant's database.

Background threads (the outbox poller, Quartz, file watch, work-queue runners) have no request to take the tenant from, and code they call may change the ContextContainer: a nested Chenile call, for example. call(String, Supplier) therefore does two things for the duration of the work: it sets the tenant in the ContextContainer, and it pins the tenant on the thread. A tenant-routing DataSource (multi-datasource-utils' TenantRoutingDataSource) routes by the pinned tenant first, so a REQUIRES_NEW transaction opened deep inside the work still reaches the right database. Both are restored afterwards, also when the work fails.

Without multi-datasource-utils there is one database; pinning is then harmless and only the ContextContainer tenant matters. A blank tenant runs the work unchanged.

  • Field Details

  • Constructor Details

    • TenantScope

      public TenantScope()
  • Method Details

    • pinnedTenant

      public String pinnedTenant()
      The tenant pinned on this thread, or null.
    • run

      public void run(String tenant, Runnable work)
    • call

      public <T> T call(String tenant, Supplier<T> work)