多用户数据源 - Spring + Hibernate [英] Multi-user Datasources - Spring + Hibernate

查看:123
本文介绍了多用户数据源 - Spring + Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个支持多个用户的网络应用程序。每个用户都有自己的数据库 - 使用H2。所有的数据库模式都是一样的。



我希望在这个应用程序中使用Spring + Hibernate。如何将用户数据库与该用户相关联 - 可能将其与HTTPSession相关联,并扩展Spring的AbstractRoutingDataSource?但不会影响Hibernate的缓存吗?另一种方法是为每个数据源分配一个SessionFactory,即使每个数据源的模式都是相同的......所以我认为这是浪费。



无论如何选择数据源需求是动态的 - 它们不能在上下文文件中预先配置,因为每个新用户都将创建自己的数据库。是否有任何现有的框架/解决方案?



我不太了解Hibernate Shards,也许这有用吗?

SessionFactory ,否则我的可能是错误的,正如一些资源所建议的那样:



我会带一些有时间重新阅读明天的所有内容(我没有把所有的细节都说实话),并且完全理解这种设置的含义(尽管它很明显会打破第二级缓存)。我将在稍后回来。







我正在编写一个web应用程序支持多个用户。每个用户都有自己的数据库 - 使用H2。所有数据库模式都是相同的。


我想知道这将如何扩展......您拥有多少用户?你如何运行H2,是什么模式?
$ b


所以我被困在如何将用户数据库与该用户相关联 - 可能在HTTPSession中关联它,并扩展Spring的AbstractRoutingDataSource?

您必须为每个用户构建一个 SessionFactory 并将其关联到记录的用户(在 Map 中,使用login作为键),然后从给定的<$获得 Session C $ C>的SessionFactory 。将 SessionFactory 的生命周期绑定到HTTP会话似乎是一个好主意(以节省一些内存),但我不确定Spring在这里会非常有帮助。我可能错了,但是 HibernateUtil 类的一个变体,并且完全程序化的方法看起来更容易。顺便说一下,我不确定你需要每个用户有多个连接。


但是这不会影响Hibernate的缓存吗?


什么缓存?
$ b


另一种方法是为每个数据源分配一个SessionFactory,尽管每个数据源的模式都是相同的......所以我将它看作是浪费。

哦,这太浪费了,但这就是你想要做的事情(每个用户一个数据库)。你没有选择(每个数据库你需要一个 SessionFactory )。为什么实际上每个用户需要一个数据库?你确定这是一个明智的决定吗?正如已经暗示的那样,这意味着很多麻烦,不能很好地扩展,增加复杂性等。为什么不使用单个数据库并将数据关联到用户?


无论如何,选择数据源需要是动态的 - 它们不能在上下文文件中预先配置,因为每个新用户都将创建自己的数据库。是否有任何现有的框架/解决方案?


不是我所知。这也是为什么我认为你必须以编程方式做所有事情。


我对Hibernate Shards不太了解,

考虑到您的应用程序的动态需求,我不明白它有何帮助。


I'm writing a web app that supports multiple users. Each user has their own database - using H2. all database schemas are the same.

I wish to use Spring + Hibernate for this application.

So I'm stuck at how to associate a user's database with that user - maybe associated it in the HTTPSession, and extend spring's AbstractRoutingDataSource? but wouldn't this effect Hibernate's cache? Another way is to have a SessionFactory with each datasource, even though every datasource's schema is the same... so I see that as a waste.

Anyways selecting the datasource needs to be dynamic - they can't be pre-configured in context files, as each new user will have its own database created. Is there any existing frameworks/solutions?

I don't know too much about Hibernate Shards, maybe that works?

解决方案

I might be wrong about the (strict) need to have one SessionFactory per database, as suggested by some resources:

I'll take some time to re-read everything tomorrow (I didn't get all the details to be honest) and to fully understand the implications of such a setup (although it seems clear that it will break the second-level cache). I'll come back on this later.


I'm writing a web app that supports multiple users. Each user has their own database - using H2. all database schemas are the same.

I wonder how this will scale... How many users do you have? How do you run H2, what mode?

So I'm stuck at how to associate a user's database with that user - maybe associated it in the HTTPSession, and extend spring's AbstractRoutingDataSource?

You'll have to build a SessionFactory per user and associate it to the logged user (in a Map, using the login as key) and then obtain a Session from a given SessionFactory. Binding the lifecycle of the SessionFactory to the HTTP session seems to be a good idea (to save some memory) but I am not sure Spring will be very helpful here. I might be wrong but a variation of the HibernateUtil class and a fully programmatic approach looks easier. I'm not sure you'll need multiple connections per user by the way.

but wouldn't this effect Hibernate's cache?

What cache?

Another way is to have a SessionFactory with each datasource, even though every datasource's schema is the same... so I see that as a waste.

Oh, it's a waste, but that's what you want to do (one database per user). And you don't have the choice (you need one SessionFactory per datadabase). Why do you need one database per user actually? Are you sure this is a wise decision? As already hinted, this means much troubles, won't scale well, adds complexity, etc. Why not using a single database and associating data to the user?

Anyways selecting the datasource needs to be dynamic - they can't be pre-configured in context files, as each new user will have its own database created. Is there any existing frameworks/solutions?

Not to my knowledge. Which is also why I think you'll have to do everything programatically.

I don't know too much about Hibernate Shards, maybe that works?

Given the dynamic needs of your application, I don't see how it could help.

这篇关于多用户数据源 - Spring + Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆