0

I’m using Jhipster (Gradle + React) for one of my project, and recently, I want to add Multi-tenancy support to the whole application (tenant will be the company name, each user need to set during login a account ID which is the tenant)

The RequestInterceptor and all of these things are working great, but I got a strange behavior for some login, on different schema

Default Schema is set to a schema that doesn’t exist (master) When I try to connect to public schema with good credentials, it’s working good When I try to connect to a schema that doesn’t exist, error message is fired ; The problem is when I try to connect to schema “Test” with credential only present in schema “public”, the login works (but the requests after are in error because data not found).

It seems the for first request the schema is not changed immediately, but changed when new request is incoming or something similar.

Here is my MultiTenantConnectionProvider

 @Override
public Connection getConnection(String tenantIdentifier) throws SQLException {
    System.out.println("Get connection for tenant " + tenantIdentifier);
    final Connection connection = getAnyConnection();
    connection.createStatement().execute("SET SCHEMA '" + tenantIdentifier + "';");
    return connection;
}

@Override
public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException 
{
    System.out.println("Release connection for tenant " + tenantIdentifier);
    String DEFAULT_TENANT = "master";
   `enter code here` connection.createStatement().execute("SET SCHEMA '" + DEFAULT_TENANT + "';");
    releaseAnyConnection(connection);
}

And here a part of my HIbernateConfig

    @Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(
    DataSource dataSource,
    MultiTenantConnectionProvider multiTenantConnectionProviderImpl,
    CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl
) {

    Map<String, Object> jpaPropertiesMap = new HashMap<>(jpaProperties.getProperties());

    jpaPropertiesMap.put(Environment.PHYSICAL_NAMING_STRATEGY, "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
    jpaPropertiesMap.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
    jpaPropertiesMap.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl);
    jpaPropertiesMap.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl);

    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    em.setPackagesToScan("com.project.webapp.domain");
    em.setJpaVendorAdapter(this.jpaVendorAdapter());
    em.setJpaPropertyMap(jpaPropertiesMap);
    return em;
}

When I check a debug message on the tenant handler, it seems the tenant change correctly when a login request is send to the back-end, but maybe it’s not the case for the schema.

Any idea ? Error with my configuration ?

Kindly, Florian

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
FlorianR
  • 31
  • 3
  • Any ideas after a month without answer ? – FlorianR Apr 05 '22 at 21:48
  • I have a hard time to understand how exactly your application is currently behaving, but it does sound like it might be a duplicate of one of these: https://stackoverflow.com/questions/69312537/spring-boot-requestscope-and-hibernate-schema-based-multi-tenancy https://stackoverflow.com/questions/55973220/spring-boot-hibernate-multi-tenancy-transactional-not-working – Jens Schauder Jul 28 '22 at 12:10

0 Answers0