2016-06-28 50 views
2

我正在一個多租戶環境中工作,在該環境中可以通過web應用程序(休息)前端從大約10個不同的數據源(和entitymanagers)訪問數據。使用Spring和Hibernate訪問多租戶環境中的數據的策略

要使用的實體管理器取決於其餘api中的URL參數,例如。 api/orders//1000003。

我需要使用entitymanager「1」來獲取數據。目前,我正在使用存儲庫層中的方法調用setDistrict(1),在創建hibernate會話並通過hibernate Criteria創建查詢之前。

所有工作都很好,但我擔心這個方法需要同步以避免從錯誤的entitymanager獲取數據。 當我同步存儲庫方法時,我擔心性能會很糟糕..

實施此多租戶訪問的好策略是什麼,因此性能很好,並且在重負載下也會返回正確的數據?

感謝您的建議。

回答

1

休眠的會話工廠允許使用tenancy behavior

  • SCHEMA相關於單獨的模式的方法。嘗試使用 此策略打開沒有租戶標識符的會話是錯誤的。此外,必須指定一個 org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider 。

  • 數據庫與單獨的數據庫方法相關。嘗試打開沒有使用此策略的租戶標識 的會話是錯誤的。此外,必須指定一個 org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider 。

  • DISCRIMINATOR與分區(鑑別器)方法相關。嘗試使用此策略打開沒有租戶 標識符的會話是錯誤的。該策略在4.0和4.1版本的Hibernate中尚未實現 。它的支持計劃爲5.0。

在你的情況下,我認爲你需要SCHEMADATABASE和必須實現MultiTenantConnectionProvidersource)。

/** 
* Simplisitc implementation for illustration purposes supporting 2 hard coded providers (pools) and leveraging 
* the support class {@link org.hibernate.service.jdbc.connections.spi.AbstractMultiTenantConnectionProvider} 
*/ 
public class MultiTenantConnectionProviderImpl extends AbstractMultiTenantConnectionProvider { 
    private final ConnectionProvider acmeProvider = ConnectionProviderUtils.buildConnectionProvider("acme"); 
    private final ConnectionProvider jbossProvider = ConnectionProviderUtils.buildConnectionProvider("jboss"); 

    @Override 
    protected ConnectionProvider getAnyConnectionProvider() { 
     return acmeProvider; 
    } 

    @Override 
    protected ConnectionProvider selectConnectionProvider(String tenantIdentifier) { 
     if ("acme".equals(tenantIdentifier)) { 
      return acmeProvider; 
     } 
     else if ("jboss".equals(tenantIdentifier)) { 
      return jbossProvider; 
     } 
     throw new HibernateException("Unknown tenant identifier"); 
    } 
} 

欲瞭解更多詳情,請參閱鏈接的文檔。

相關問題