NHibernate:读/写分离
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在底层,nhibernate 使用 ado.net 连接到数据源。所以你需要看看 ado.net 如何处理这种情况。
但除此之外,我认为如果你这样做的话,你不会得到任何好处。
一些背景:
nhibernate 中的对象与会话绑定,会话与会话工厂绑定,而会话工厂与 1 个连接绑定。
假设您有一个 ip 1 的负载均衡器用于读取。它平衡 ip 2 和 3 上的数据库。您可以直接访问 ip 2 进行写入。
因此,
如果您通过与连接 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
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).