NHibernate:读/写分离

发布于 2024-09-07 22:23:10 字数 105 浏览 4 评论 0原文

我想将 NHibernate 连接到 MySQL 主从复制配置, 所以我想向主机发送写入,并向主机发送读取 主人和奴隶。这可能吗?我还计划拥有一个负载均衡器 以平衡读取。 (ldirectord)

I would like to connect NHibernate to a MySQL master-slave replication configuration,
so I would like to send writes to the master, and reads to the
master and slaves. Is this possible? I am also planning on having a load balancer
to balance the reads. (ldirectord)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2024-09-14 22:23:10

在底层,nhibernate 使用 ado.net 连接到数据源。所以你需要看看 ado.net 如何处理这种情况。

但除此之外,我认为如果你这样做的话,你不会得到任何好处。

一些背景:
nhibernate 中的对象与会话绑定,会话与会话工厂绑定,而会话工厂与 1 个连接绑定。

假设您有一个 ip 1 的负载均衡器用于读取。它平衡 ip 2 和 3 上的数据库。您可以直接访问 ip 2 进行写入。

因此,

Ip     Use
1      Balancer
2      Read / Write
3      Read  

如果您通过与连接 1 绑定的会话读取一个对象,则必须加载该对象,然后保存并刷新以在与连接 2 绑定的会话上写入。此时,您已完成 2 次读取和 1 次写入。而如果您使用一个会话工厂,则您可以进行读取和写入(假设同一会话通过读取和写入处于活动状态,或者设置了二级缓存)。

under the hood, nhibernate uses ado.net to connect to data sources. So you would need to look at how ado.net handles this situation.

But additionally, I don't think you would gain anything if you could do this.

Some background:
Objects in nhibernate are tied to sessions, which are tied to session factories, which are tied to 1 connection.

Let's say you had a load balancer with ip 1 for reads. And it balances databases on ip's 2 and 3. And you hit ip 2 directly for writes.

So you have

Ip     Use
1      Balancer
2      Read / Write
3      Read  

If you read an object with a session tied to connection 1, you have to load that object and then save and flush to write on a session tied to connection 2. At that point, you have done 2 read's and 1 write. Whereas if you use one session factory then you have a read and a write (assuming the same session is alive through the read and the write, or that 2nd level caching is setup).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文