1

Using a AbstractMultiTenantConnectionProvider give me some problems. In the case i had 1000 tenants and i want to add more without restarting the webserver, how can i use selectConnectionProvider easily?

 @Override
protected ConnectionProvider selectConnectionProvider(String tenantIdentifier) {

if( "xml1".equals(tenantIdentifier) ) 
    return xml1;

if( "xml2".equals(tenantIdentifier) ) 
    return xml2;

return null;
}

As you can see in this example tenants are linked statically. How could i solve this issue. Thanks for any hint or solution! Cheers, t.

Its a follow-up question from here Implement an AbstractMultiTenantConnectionProvider

Community
  • 1
  • 1
tonimaroni
  • 1,062
  • 10
  • 19

1 Answers1

2

Make it a registry (as in the pattern) with which you register/deregister tenants as needed ("as needed" is defined by your app/environment). Structurally then the MultiTenantConnectionProvider internally is a Map. You can access the MultiTenantConnectionProvider from the Hibernate SessionFactory using:

MultiTenantConnectionProvider multiTenantConnectionProvider = 
( (SessionFactoryImplementor) sessionFactory )
        .getServiceRegistry()
        .getService( MultiTenantConnectionProvider.class );
YourMultiTenantConnectionProviderImpl yourMultiTenantConnectionProvider = (YourMultiTenantConnectionProviderImpl) multiTenantConnectionProvider;
yourMultiTenantConnectionProvider.registerTenant( ... );
...
yourMultiTenantConnectionProvider.deregisterTenant( ... );

Then you would just need to decide how you want to persist the tenants between starts/stops (write to file, etc). Personally I'd persist immediately upon registerTenant/deregisterTenant calls. Alternately you could wait until shutdown (have YourMultiTenantConnectionProviderImpl implement Stoppable), but you would potentially miss writing some out in cases of JVM crashes.

Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46