sunitk / multitenancy-dynamic-tenant

Spring Boot 2 JPA Hibernate with database per tenant with dynamic configuration of tenants, secured by Spring Security
Apache License 2.0
99 stars 76 forks source link

fix: missing bean datasourceBasedMultitenantConnectionProvider when runs abnormally after packaging #16

Open zhuoNy opened 3 years ago

zhuoNy commented 3 years ago

BUG

***************************
APPLICATION FAILED TO START
***************************

Description:

Field productRepository in com.s.s.product.tenant.service.impl.ProductServiceImpl required a bean named 'tenantEntityManagerFactory' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'entityManagerFactory' in 'TenantDatabaseConfig' not loaded because @ConditionalOnBean (names: datasourceBasedMultitenantConnectionProvider; SearchStrategy: all) did not find any beans named datasourceBasedMultitenantConnectionProvider

Action:

Consider revisiting the entries above or defining a bean named 'tenantEntityManagerFactory' in your configuration.

MacBook-Pro:~ s$ java -jar /Users/s/git/s-microservice/product-microservice/target/product-service-1.0.0-SNAPSHOT.jar 

FIX

  1. Comment this code in TenantDatabaseConfig.java

    //    @Bean(name = "datasourceBasedMultitenantConnectionProvider")
    //    @ConditionalOnBean(name = "masterEntityManagerFactory")
    //    public MultiTenantConnectionProvider multiTenantConnectionProvider() {
    //        // Autowires the multi connection provider
    //        return new DataSourceBasedMultiTenantConnectionProviderImpl();
    //    }
  2. Add Annotation on TenantDatabaseConfig.java

    @AutoConfigureAfter(name = "datasourceBasedMultitenantConnectionProvider")
    public class TenantDatabaseConfig {
    ...
    }
  3. Change Annotaion and spectify bean name in DataSourceBasedMultiTenantConnectionProviderImpl.java

    @Component("datasourceBasedMultitenantConnectionProvider")
    public class DataSourceBasedMultiTenantConnectionProviderImpl
        extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl {
    ...
    }
sunitk commented 3 years ago

@zhuoNy Thanks for providing the fix. When I built this project I never faced these issues with the versions of Spring Boot, Spring data, etc. The project is still running without any issues in production for a intranet application and works fine. But once again, thanks for fixing the issue.

Kuldeep9844 commented 8 months ago

can you tell me exact order of configuration of all multitenancy classes, because i was having the issue that my tenants were not getting configured and now they are getting configured by your above solution but still application is not able to route dynamically between tenants , its only connecting to masterdatasource instead of tenant.