How to use C3P0 in a spring boot hibernate multi tenant application to manage connection pool?

Antara Datta :

I am trying yo implement a multi tenant application using hibernate's MultiTenantConnectionProvider and CurrentTenantIdentifierResolver. I am not sure how the pooling is managed by hibernate and whether it is a good practice to leave it to Hibernate. Can I use C3P0 in this multi tenant application.

Here is my initial code:

@Bean(name = "dataSources" )
public Map<String, DataSource> dataSources() {
    Map<String, DataSource> result = new HashMap<>();
    for (DataSourceProperties dsProperties : this.multiTenantProperties.getDataSources()) {
        DataSourceBuilder factory = DataSourceBuilder
            .create()
            .url(dsProperties.getUrl())
            .username(dsProperties.getUsername())
            .password(dsProperties.getPassword())
            .driverClassName(dsProperties.getDriverClassName());
        result.put(dsProperties.getTenantId(), factory.build());
    }
    return result;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(MultiTenantConnectionProvider DataSourceMultiTenantConnectionProviderImpl ,
    CurrentTenantIdentifierResolver currentTenantIdentifierResolver) {

    Map<String, Object> hibernateProps = new LinkedHashMap<>();
    hibernateProps.putAll(this.jpaProperties.getProperties());
    hibernateProps.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
    hibernateProps.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
    hibernateProps.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, TenantIdentifierResolverImpl );


    // No dataSource is set to resulting entityManagerFactoryBean
    LocalContainerEntityManagerFactoryBean result = new LocalContainerEntityManagerFactoryBean();
    result.setPackagesToScan(new String[] { Test.class.getPackage().getName() });
    result.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    result.setJpaPropertyMap(hibernateProps);

    return result;
}

This is the Connection provider configured in hibernate

public class DataSourceMultiTenantConnectionProviderImpl extends 
AbstractDataSourceBasedMultiTenantConnectionProviderImpl {

private static final long serialVersionUID = 1L;

@Autowired
private Map<String, DataSource> dataSources;

@Override
protected DataSource selectAnyDataSource() {
    return this.dataSources.values().iterator().next();
}

@Override
protected DataSource selectDataSource(String tenantIdentifier) {
    return this.dataSources.get(tenantIdentifier);
}
}

This is the tenant resolver provided to hibernate

public class TenantIdentifierResolverImpl implements 
CurrentTenantIdentifierResolver {

private static String DEFAULT_TENANT_ID = "tenant_1";

@Override
public String resolveCurrentTenantIdentifier() {
    String currentTenantId = TenantContext.getTenantId();
    return (currentTenantId != null) ? currentTenantId : DEFAULT_TENANT_ID;
}

@Override
public boolean validateExistingCurrentSessions() {
    return true;
}
}

How to handle connection pooling here?

Preety Singh :

You might need to change your datasources bean to this:

    @Bean(name = "dataSources" )
public Map<String, ComboPooledDataSource> dataSources() throws PropertyVetoException {
    Map<String, ComboPooledDataSource> result = new HashMap<>();
    for (DataSourceProperties dsProperties : this.multiTenantProperties.getDataSources()) {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass(dsProperties.getDriverClassName());
        ds.setJdbcUrl(dsProperties.getUrl());
        ds.setUser(dsProperties.getUsername());
        ds.setPassword(dsProperties.getPassword());
        ds.setInitialPoolSize(5);
        ds.setMinPoolSize(1);
        ds.setAcquireIncrement(1);
        ds.setMaxPoolSize(5);
        result.put(dsProperties.getTenantId(), ds);
    }   

    return result;
}

And add this method to your DataSourceMultiTenantConnectionProviderImpl

@Override
public Connection getConnection(String tenantIdentifier) throws SQLException {
    return this.dataSources.get(tenantIdentifier).getConnection();
}

This should be enough to start connection pooling using C3P0.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=133421&siteId=1