使用Spring JPA的多租户 [英] Multitenancy with Spring JPA
问题描述
我正在寻找针对我的Web应用程序的多租户解决方案。
我想用Separate Schema Model实现一个应用程序。我想每个会话都有一个数据源。为了做到这一点,我把datasource和entitymanger放在会话范围内,但那不起作用。我想在用户输入用户名和密码以及tenantId时加载data-access-context.xml(包括数据源和其他存储库bean)文件。我想知道它是否是一个很好的解决方案?
I am looking around for a multitenancy solution for my web application. I would like to implement a application with Separate Schema Model. I am thinking to have a datasource per session. In order to do that i put datasource and entitymanger in session scope , but thats not working. I am thinking to load data-access-context.xml(which include datasource and other repository beans) file when the user entered username and password and tenantId. I would like to know if it is a good solution?
推荐答案
多租户是一个有点棘手的主题,它必须在JPA提供者方面,从客户端代码的角度来看,没有任何或几乎没有任何变化。 eclipselink 支持多租户(请参阅: EclipseLink / Development / Indigo / Multi-Tenancy ), hibernate 最近添加了它。
Multitenancy is a bit tricky subject and it has to be handled on the JPA provider side so that from the client code perspective nothing or almost nothing changes. eclipselink has support for multitenancy (see: EclipseLink/Development/Indigo/Multi-Tenancy), hibernate just added it recently.
另一种方法是使用 AbstractRoutingDataSource
,参见: Hibernate中的多租户。
Another approach is to use AbstractRoutingDataSource
, see: Multi tenancy in Hibernate.
使用会话范围风险太大(同样,最终会有数千个数据库连接,每个会话/用户都很少。最后 EntityManager
和底层数据库连接不可序列化,因此您无法迁移会话并正确扩展应用程序。
Using session-scope is way too risky (also you will end up with thousands of database connections, few for every session/user. Finally EntityManager
and underlying database connections are not serializable so you cannot migrate your session and scale your app properly.
这篇关于使用Spring JPA的多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!