admin管理员组

文章数量:1318774

I'm evaluating the approach to have a database per tenant solution. Following along I ended up with a prototype using AbstractRoutingDataSource that requires a default database when no tenant is yet selected. It looks like this is required just to allow Spring Boot (Data Jpa) to startup and initializing a entity manager.

@Component
public class TenantRoutingDatasource extends AbstractRoutingDataSource {

    @Autowired private TenantIdentifierResolver tenantIdentifierResolver;

    TenantRoutingDatasource() {

        setDefaultTargetDataSource(createEmbeddedDatabase("default"));

        HashMap<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("VMWARE", createEmbeddedDatabase("VMWARE"));
        targetDataSources.put("PIVOTAL", createEmbeddedDatabase("PIVOTAL"));
        setTargetDataSources(targetDataSources);
    }

    @Override
    protected String determineCurrentLookupKey() {
        return tenantIdentifierResolver.resolveCurrentTenantIdentifier();
    }

    private EmbeddedDatabase createEmbeddedDatabase(String name) {

        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).setName(name).addScript("manual-schema.sql")
                .build();
    }
}

Does anyone have a solution for avoiding that default data source? Is there a way to have Spring Boot defer the creation of any entity manager until it gets used by a repository or the like?

I'm evaluating the approach to have a database per tenant solution. Following along https://github/spring-projects/spring-data-examples/tree/main/jpa/multitenant/db I ended up with a prototype using AbstractRoutingDataSource that requires a default database when no tenant is yet selected. It looks like this is required just to allow Spring Boot (Data Jpa) to startup and initializing a entity manager.

@Component
public class TenantRoutingDatasource extends AbstractRoutingDataSource {

    @Autowired private TenantIdentifierResolver tenantIdentifierResolver;

    TenantRoutingDatasource() {

        setDefaultTargetDataSource(createEmbeddedDatabase("default"));

        HashMap<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("VMWARE", createEmbeddedDatabase("VMWARE"));
        targetDataSources.put("PIVOTAL", createEmbeddedDatabase("PIVOTAL"));
        setTargetDataSources(targetDataSources);
    }

    @Override
    protected String determineCurrentLookupKey() {
        return tenantIdentifierResolver.resolveCurrentTenantIdentifier();
    }

    private EmbeddedDatabase createEmbeddedDatabase(String name) {

        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).setName(name).addScript("manual-schema.sql")
                .build();
    }
}

Does anyone have a solution for avoiding that default data source? Is there a way to have Spring Boot defer the creation of any entity manager until it gets used by a repository or the like?

Share Improve this question asked Jan 21 at 11:00 Martin AhrerMartin Ahrer 1,0375 gold badges16 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It looks like the default database is optional, but I haven't tested.

You could defer the datasource connection on the first statement with LazyConnectionDataSourceProxy. It is a proxy for your datasource that will lazy initialize underlying JDBC connection (and thus defer everything that depends on it).

This article have a different use case, but they are combining AbstractRoutingDataSource with LazyConnectionDataSourceProxy, I believe this is what you are looking for.

The other option is to play with JPA bootstrap mode, but in this case, you will have to wait for each JDBC connection to be resolved in the pool and the EntityManagerFactory to be initialized.

本文标签: Spring Boot Hibernate multitenancy with database per tenant default databaseStack Overflow