简体   繁体   English

每个租户多租户连接提供程序的Hibernate数据库正在为单个租户创建多个数据库连接

[英]Hibernate database per tenant multi-tenant connection provider is creating multiple database connections for single tenant

I want to know how hibernate's multi-tenant connection provider handles the data base connections for multiple tenants in separate database multi-tenancy approach. 我想知道hibernate的多租户连接提供程序如何在单独的数据库多租户方法中处理多个租户的数据库连接。 If I hit the API with same tenant, say 1234, then at first hit it should CONNECT to that tenant's specific database and after several hits for same tenant, will it use the same connection of database or it will open the new connection again for the same tenant? 如果我使用相同的租户点击API,比如1234,那么首先点击它应该连接到该租户的特定数据库,并且在对同一租户进行几次点击后,它是否会使用相同的数据库连接,否则它将再次打开新连接同一个房客?

I have used AbstractDataSourceBasedMultiTenantConnectionProviderImpl and CurrentTenantIdentifierResolver implementations in database per tenant approach 我在每个租户方法的数据库中使用了AbstractDataSourceBasedMultiTenantConnectionProviderImplCurrentTenantIdentifierResolver实现

class CurrentTenantIdentifierResolver {
    // Other methods + fields
    public String resolveCurrentTenantIdentifier() {        
      String tenantId = TenantContext.getCurrentTenant();       
      return tenantId;  
    }
}

class AbstractDataSourceBasedMultiTenantConnectionProviderImpl {
    // Other methods + fields

    @Override protected DataSource selectDataSource(String tenantIdentifier) {       
      return MultiTenantInitService.getDataSourceMap().get(tenantIdentifier);   
    }
}

class MultiTenantInitService {
    // Other methods + fields

    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(prop.getProperty("spring.jpa.properties.hibernate.driver-class-name"));
    dataSource.setUrl(prop.getProperty("spring.datasource.url"));
    dataSource.setUsername(prop.getProperty("spring.datasource.username"));
    dataSource.setPassword(prop.getProperty("spring.datasource.password"));
    dataSourceMap.put(ApplicationConstants.DEFAULT_TENANT_ID, dataSource);
}

I expected multi tenant connection provider to connect to the database on first hit of API for single tenant for once only. 我希望多租户连接提供商只需一次就可以在单个租户的API首次访问时连接到数据库。 It should not open the connections again and again for the same tenant. 它不应该为同一个租户一次又一次地打开连接。 The new connection with data base should only be formed for new tenant. 只应为新租户建立与数据库的新连接。 But, if it does open the connections then it should also manage the connection closing on its own. 但是,如果它确实打开了连接,那么它也应该自己管理连接关闭。

I think what you want is, 我想你想要的是,

spring.datasource.max-active=1

This property limits the maximum active connections to your database. 此属性限制与数据库的最大活动连接。 So you can set this property to your DataSource and use it. 因此,您可以将此属性设置为DataSource并使用它。 which means you only have one connection in the connection pool. 这意味着您在连接池中只有一个连接。 But there are pros and cons of this approach as if in any means the connection is corrupted, you will have to work on creating a different connection to work on the particular tenant again. 但是这种方法的优点和缺点就好像在任何情况下连接都已损坏,您将不得不再次创建一个不同的连接来处理特定的租户。 So there are pretty good reasons to have a connection pool instead of having just one connection. 所以有很好的理由建立连接池而不是只有一个连接。 So the better solution would have a small connection pool as per your requirement. 因此,根据您的要求,更好的解决方案将拥有一个小型连接池。 If your application is constantly accessing the database its obvious that you need to have a connection pool. 如果您的应用程序不断访问数据库,则显然您需要拥有连接池。 So I would suggest you have small connection pools per tenant. 所以我建议你每个租户有一个小连接池。

spring.datasource.max-active=5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM