3

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?

1 Answer 1

3

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 ...

2
  • Two questions: 1) ConnectionBuilderProvider is a class from the testing package of hibernate. Should I use it in production? 2) Since I have 1000 schemas would it be correct to use like a map where I save all those connection providers and I pass one of them at the creation of the session? Could two different session use the same connection provider?
    – dierre
    Sep 10, 2012 at 13:13
  • 1) You can use the connectionBuilderProvider class or you can construct instances directly from the constructor of the ConnectionProvider class. 2) Yes, using a map would be of course the best bet in that context .
    – aleroot
    Sep 10, 2012 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.