Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement an AbstractMultiTenantConnectionProvider

I'm trying to use hibernate multi-tenancy (from chapter 16).

Right now my ExtendedStorageManager uses this method to start a multi-tenant session:

public Session getClabSession(int serverId, String customerSchema) {
    if (!clabFactories.containsKey(serverId)) {
        DbServers d = databaseConfigurations.get(serverId);
        clabCustomerConfig.setProperty(
            "hibernate.connection.url",
            ResourceBundleService.getInstance().decorateBundle(
                "database.connection.pattern", d.getHost(),
                d.getPort()));
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
            .applySettings(clabCustomerConfig.getProperties())
            .buildServiceRegistry();
        SessionFactory factory = clabCustomerConfig
            .buildSessionFactory(serviceRegistry);
        clabFactories.put(d.getId(), factory);
        serviceRegistry = null;
        factory = null;
    }
    Session session = clabFactories.get(serverId).withOptions()
        .tenantIdentifier(customerSchema).openSession();
    session.setDefaultReadOnly(false);
    return session;

}

Obviously I had a first error when testing because I totally missed the implementation of a MultiTenantConnectionProvider as explained here.

Using grepcode I found out about AbstractMultiTenantConnectionProvider that looks like what I want to use. So I created my class extending it:

public class XMLDMultiTenantConnectionProvider extends
    AbstractMultiTenantConnectionProvider {

    /**
     * 
     */
    private static final long serialVersionUID = -6679645015449636823L;

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

    @Override
    protected ConnectionProvider selectConnectionProvider(
        String tenantIdentifier) {
    // TODO Auto-generated method stub
    return null;
    }

}

But I'm a bit clueless now on what to use to get a ConnectionProvider in these two cases. Can you help me?

PS: I found ConnectionProviderBuilder. Do you think I can use it as an example to follow?

like image 601
dierre Avatar asked Jan 26 '26 15:01

dierre


1 Answers

I think that you can have your own implementation of the abstract class with something like the following :

public class XMLDMultiTenantConnectionProvider extends AbstractMultiTenantConnectionProvider {


      private final ConnectionProvider xml1 = ConnectionProviderBuilder.buildConnectionProvider("xml1DataSource");
      private final ConnectionProvider xml2 = ConnectionProviderBuilder.buildConnectionProvider("xml2DataSource");

    /**
     * 
     */
    private static final long serialVersionUID = -6679645015449636823L;

    @Override
    protected ConnectionProvider getAnyConnectionProvider() {
         //Default Datasource (in this case i chose the first one as default)
         return xml1;
    }

    @Override
    protected ConnectionProvider selectConnectionProvider(String tenantIdentifier) {

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

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

    return null;
    }

}

I think that could be a good and understandable example ...

like image 119
aleroot Avatar answered Jan 28 '26 05:01

aleroot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!