What is the right approach for Multi-tenancy using eclipselink

纵然是瞬间 提交于 2019-12-04 19:25:40

One can use ThreadLocal as shown below:

public class TenantContext {
  private static final ThreadLocal<String> TENANT_TL = new ThreadLocal<>();

  public static String getTenantId() {
    return TENANT_TL.get();
  }

  public static void setTenantId(String tenantId) {
    TENANT_TL.set(tenantId);
  }
}

The tenantId can be set on authentication, usually in an Interceptor. While creating EntityManager, above tenant can be used as shown:

em.setProperty(EntityManagerProperties.MULTITENANT_PROPERTY_DEFAULT, TenantContext.getTenantId());

I have created a java example on Multi-Tenancy (Table per Tenant) using Eclipse Link and Spring Data. An existing application can be converted to Multi-Tenant with a minimal code change. This is done using bytecode instrumentation. This idea is chosen to utilize the complete power of Spring Data.

One can execute MultiTenantTest to see it working.

The idea is open-sourced and is available at Maven Central

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!