Class TenantDataSources

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

public final class TenantDataSources extends Object

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} or data/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 or USE 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 Details

    • TenantDataSources

      public TenantDataSources(String template, int poolSize, int maxPools)
      A database per tenant.
      Parameters:
      template - the URL with ${tenant} in it, or null to accept only registered pools
      poolSize - connections per tenant pool
      maxPools - how many template pools stay open at once
    • TenantDataSources

      public TenantDataSources(String template, String initTemplate, int poolSize, int maxPools)
      A database or a schema per tenant.
      Parameters:
      template - the URL, or null to accept only registered pools
      initTemplate - the statement each connection runs as it opens, or null
      poolSize - connections per tenant pool
      maxPools - 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

      public static TenantDataSources fromConfig(Config config) throws IOException
      Reads cn1.tenancy.datasource.url (falling back to cn1.datasource.url), cn1.tenancy.datasource.init, cn1.tenancy.datasource.pool.size and cn1.tenancy.datasource.maxPools.
      Parameters:
      config - the configuration
      Returns:
      the pools
      Throws:
      IOException - when no URL is configured
    • register

      public void register(String tenant, DataSource pool)
      Registers a pool someone else opened for tenant.
      Parameters:
      tenant - the tenant identifier
      pool - its pool
    • current

      public DataSource current() throws IOException
      The pool of the current tenant.
      Returns:
      the pool
      Throws:
      IOException - when it cannot be opened
      MissingTenantException - when no tenant is current
    • forTenant

      public DataSource forTenant(String tenant) throws IOException
      The pool of tenant, opening it when needed.
      Parameters:
      tenant - the tenant identifier
      Returns:
      the pool
      Throws:
      IOException - when it cannot be opened
    • migrateAll

      public int migrateAll(Iterable<String> tenants, Config config) throws IOException
      Runs the application's migrations against every tenant in tenants, one after another; the first failure stops the run and names the tenant.
      Parameters:
      tenants - the tenants
      config - 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.