具有Hibernate的Mysql AbstractMultiTenantConnectionProvider与C3P0ConnectionProvider抛出GenericJDBCException:无法打开连接

时间:2017-02-21 14:13:15

标签: mysql hibernate multi-tenant c3p0

我正致力于实现multitenancy =“DATABASE”,即每个租户数据库/模式的单连接池(mysql数据库和模式是同义词)。

我有MultiTenantConnectionProviderImpl.java

public class MultiTenantConnectionProviderImpl extends AbstractMultiTenantConnectionProvider implements
    ServiceRegistryAwareService {

private static final long serialVersionUID = 1234567890L;

private final HashMap<String, C3P0ConnectionProvider> connProviderMap = new HashMap<>();
private Map<String, String> originalSettings;
private ServiceRegistryImplementor serviceRegistry;

public MultiTenantConnectionProviderImpl() {
}

@Override
protected C3P0ConnectionProvider getAnyConnectionProvider() {
    return selectConnectionProvider(TenantContext.getTenantId());
}

@Override
protected C3P0ConnectionProvider selectConnectionProvider(String tenantId) {
    if (tenantId == null || !tenantId.equals(TenantContext.getTenantId())) {
        throw new TenantStateException(
                "Cannot get connection. Cause: tenant_id is not defined.");
    }

    C3P0ConnectionProvider connectionProvider = connProviderMap.get(tenantId);
    if (connectionProvider == null) {
        // create the new connection and register it
        Map<String, String> settings = new HashMap<>(originalSettings);
        // alter connection by changing user / password of the connection
        Properties properties = new PropertiesBuilder().withScope("database").build();
        settings.put("hibernate.connection.user", DatabaseConnectionProperties.getUser());
        settings.put("hibernate.connection.password", DatabaseConnectionProperties.getPassword());
        settings.put("hibernate.connection.url", DatabaseConnectionProperties.getConnectionUrl());
        settings.put("hibernate.connection.driver_class", properties.getProperty("hibernate.connection.driver_class"));

        connectionProvider = new C3P0ConnectionProvider();
        connectionProvider.injectServices(serviceRegistry);
        connectionProvider.configure(settings);
        connProviderMap.put(tenantId, connectionProvider);
    }

    return connectionProvider;
}

@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
    this.serviceRegistry = serviceRegistry;
    originalSettings = serviceRegistry.getService(ConfigurationService.class).getSettings();
    C3P0ConnectionProvider connectionProvider = new C3P0ConnectionProvider();
    connectionProvider.injectServices(serviceRegistry);
    connectionProvider.configure(originalSettings);
    connProviderMap.put(TenantContext.getTenantId(), connectionProvider);
}
}

连接用户如下

String.format("jdbc:mysql://%s/%s", databaseHost, databaseName)

失败了
rg.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
...

  at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297) ~[hibernate-core-4.1.12.Final.jar:4.1.12.Final]
    ... 43 common frames omitted
   Caused by:     com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool  could not acquire a resource from its primary factory or source.

我试图覆盖getConnection(String tenantId)来执行“use tenantId”来获取正确的数据库。但它没有帮助,我恢复到使用连接URL指定数据库的原始方式。

其他人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:1)

我最终实现了MultiTenantConnectionProvider,初始连接到默认模式,然后对租户模式进行模式更改

Settings > Build, Execution, Deployments > Application Servers