接続プールを管理するために、マルチテナントアプリケーションを休止状態?春ブーツにC3P0を使用する方法

ダッタの中で:

私はヨーヨーが休止状態のMultiTenantConnectionProviderとCurrentTenantIdentifierResolverを用いたマルチテナントアプリケーションを実装しようとしています。私はプールが休止状態で、休止状態に任せることをお勧めであるかどうかをどのように管理されるかわかりません。私は、このマルチテナントアプリケーションでC3P0を使用することができます。

ここに私の最初のコードは次のとおりです。

@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;
}

これは、休止状態に構成された接続プロバイダです

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);
}
}

これは、休止状態にするために提供テナントリゾルバです

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;
}
}

ここでは、接続プーリングを処理する方法は?

Preetyシン:

あなたはこれにあなたのデータソースBeanを変更する必要があります:

    @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;
}

そして、あなたのDataSourceMultiTenantConnectionProviderImplに、このメソッドを追加

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

これは、C3P0を使用して接続プーリングを開始するのに十分でなければなりません。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=237007&siteId=1