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 badges1 Answer
Reset to default 0It 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
版权声明:本文标题:Spring Boot Hibernate multitenancy with database per tenant default database - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047752a2417889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论