Java – Implementation of AbstractMultiTenantConnectionProvider

Implementation of AbstractMultiTenantConnectionProvider… here is a solution to the problem.

Implementation of AbstractMultiTenantConnectionProvider

I’m trying to use hibernate Multi-Tenancy (from chapter 16 )。

Now my ExtendedStorageManager uses this method to start a Multi-Tenancy 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;

}

Apparently I got the first error while testing because I completely missed the implementation of MultiTenantConnectionProvider, as in as explained here

Using grepcode I discovered AbstractMultiTenantConnectionProvider looks like I want to use. So I created a class that extends 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 now somewhat don’t know what to use to get the ConnectionProvider in both cases. Can you help me?

PS: I found ConnectionProviderBuilder.

Solution

I think you can implement your own implementation with an abstract class like this:

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 this could be a good and understandable example….

Related Problems and Solutions