在单个应用程序中使用NHibernate处理多个数据库 [英] Handling Multiple databases with NHibernate in a single application
问题描述
此刻,我在配置文件中定义连接属性,并且仅连接到一个数据库.我希望能够在某个时候进行用户登录,弄清楚(也许是通过单独的中央数据库)他们应该连接哪个数据库,并且从那时开始,所有创建的会话都将与该数据库进行对话.
At the moment I define the connection properties in a configuration file and only ever connect to one database. I'd like to be able to at some point have a user login, figure out (via a seperate central database maybe) what database they should be connected and from that point on all sessions created will talk to that database.
实现此目标的最佳方法是什么?为每个可能的数据库创建一个配置文件?还是我可以有一个会话管理器并相应地即时更改连接URL?我有哪些选择?
Whats the best way to achieve this. Create a configuration file for every possible database? Or could I have a single session manager and change the connection url on the fly accordingly? What sort of options do i have?
更新:抱歉,我应该提到的是NHibernate.我不认为这很重要,但是像Hibernate Shards这样的东西并不适用,因为我相信NHibernate Shards正在等待.
Update: Apologies I should have mentioned this was NHibernate. I didn't think it would matter but some things like Hibernate Shards will not be applicable to be as I believe NHibernate Shards is waiting.
推荐答案
您只需要创建两个数据源,然后为特定查询调用一个数据源即可.
You just need to make two datasources then call the one you need for the specific query.
请查看以下内容:
https://www.hibernate.org/450.html
一些官方解决方案.
在这里:
http://www.java-forums.org /database/159-hibernate-multiple-database.html
有关此问题的在线主题.
an online thread about this issue.
这篇关于在单个应用程序中使用NHibernate处理多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!