java - Strategy for accessing data in multi-tenant environment with Spring and Hibernate -
i working in multi-tenant environment data can accessed 10 different datasources (and entitymanagers) webapplication (rest) frontend.
the entitymanager used depending on url parameter in rest api, ex. api/orders/1/1000003.
i need use entitymanager "1" fetch data. @ moment using method in repository layer call setdistrict(1), before creating hibernate session , creating query via hibernate criteria.
all working fine, worried fact method need synchronized avoid getting data wrong entitymanager. when synchronize repository method worried performance horrible..
what strategy implementing multi-tenant access performance , correct data returned under heavy load well?
thanks advice.
the sessionfactory of hibernate allows use tenancy behavior:
schema correlates separate schema approach. error attempt open session without tenant identifier using strategy. additionally, org.hibernate.service.jdbc.connections.spi.multitenantconnectionprovider must specified.
database correlates separate database approach. error attempt open session without tenant identifier using strategy. additionally, org.hibernate.service.jdbc.connections.spi.multitenantconnectionprovider must specified.
discriminator correlates partitioned (discriminator) approach. error attempt open session without tenant identifier using strategy. strategy not yet implemented in hibernate of 4.0 , 4.1. support planned 5.0.
in case think need schema or database , have implement multitenantconnectionprovider (source).
/** * simplisitc implementation illustration purposes supporting 2 hard coded providers (pools) , leveraging * 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" ); } }
for more details see linked documentation.
Comments
Post a Comment