NHibernate HiLo 解释 [英] Explanation of NHibernate HiLo

查看:14
本文介绍了NHibernate HiLo 解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力弄清楚 HiLo 生成器在 NHibernate 中是如何工作的.我已经阅读了此处的说明,这让事情变得更清楚了.

I'm struggling to get my head round how the HiLo generator works in NHibernate. I've read the explanation here which made things a little clearer.

我的理解是每个 SessionFactory 从数据库中检索高值.这提高了性能,因为我们可以在不访问数据库的情况下访问 ID.

My understanding is that each SessionFactory retrieves the high value from the database. This improves performance because we have access to IDs without hitting the database.

来自上述链接的解释还指出:

The explanation from the above link also states:

例如,假设您有一个当前值为 35 的高"序列,而低"数字在 0-1023 的范围内.然后客户端可以将序列增加到 36(其他客户端在使用 35 时能够生成密钥)并且知道密钥 35/0、35/1、35/2、35/3...35/1023 是全部可用.

For instance, supposing you have a "high" sequence with a current value of 35, and the "low" number is in the range 0-1023. Then the client can increment the sequence to 36 (for other clients to be able to generate keys while it's using 35) and know that keys 35/0, 35/1, 35/2, 35/3... 35/1023 are all available.

这在 Web 应用程序中如何工作,因为我不是只有一个 SessionFactory,因此有一个 hi 值.这是否意味着在断开连接的应用程序中,您的实体表中可能会出现重复的(低)ID?

How does this work in a web application as don't I only have one SessionFactory and therefore one hi value. Does this mean that in a disconnected application you can end up with duplicate (low) ids in your entity table?

在我的测试中,我使用了以下设置:

In my tests I used these settings:

<id name="Id" unsaved-value="0">
  <generator class="hilo"/>
</id>

我进行了一项测试以保存 100 个对象.我的表中的 ID 为 32768 - 32868.下一个 hi 值增加到 2.然后我再次运行我的测试,ID 的范围是 65536 - 65636.

I ran a test to save 100 objects. The IDs in my table went from 32768 - 32868. The next hi value was incremented to 2. Then I ran my test again and the Ids were in the range 65536 - 65636.

首先,为什么从 32768 开始而不是 1,其次为什么从 32868 跳到 65536?

First off, why start at 32768 and not 1, and secondly why the jump from 32868 to 65536?

现在我知道我的代理键应该没有任何意义,但我们确实在我们的应用程序中使用它们.为什么我不能让它们像 SQL Server 标识字段那样很好地递增.

Now I know that my surrogate keys shouldn't have any meaning but we do use them in our application. Why can't I just have them increment nicely like a SQL Server identity field would.

终于有人能解释一下 max_lo 参数的工作原理了吗?这是可以针对高值创建的低值(我脑海中的实体 ID)的最大数量吗?

Finally can someone give me an explanation of how the max_lo parameter works? Is this the maximum number of low values (entity ids in my head) that can be created against the high value?

这是 NHibernate 中的一个主题,我一直在努力寻找相关文档.我阅读了整个 NHibernate in action book,但它仍然没有详细介绍它是如何工作的.

This is one topic in NHibernate that I have struggled to find documentation for. I read the entire NHibernate in action book and it still doesn't go into how this works in any detail.

谢谢本

推荐答案

我相信你的理解或多或少是正确的.max_lo 参数仅用于确定可用于任何给定 Hi 值的 Id 数量.

I believe your understanding is more or less correct. The max_lo parameter is simply used to determine the number of Ids available for any given Hi value.

我最好的猜测是 NHibernate 的默认 max_lo 值是 32768.因此,Hi 值为 1 将使您的 Id 从 32768 开始并运行到 65535.Hi 值为 2 将从 65536 开始并运行另一个 max_lo Id.

My best guess is that NHibernate's default max_lo value is 32768. Thus a Hi value of 1 would start your Ids at 32768 and run you right up to 65535. A Hi value of 2 would start at 65536 and run up another max_lo Ids.

基本上,您使用 max_lo 值来控制 Id 碎片.32768 可能不是每种情况下的最佳值.

Basically you use the max_lo value to control Id fragmentation. 32768 is likely not the optimal value for every situation.

但是需要注意的是,这仅在 SessionFactory 的范围内有效.如果您正在停止/启动您的应用程序并大量重新初始化 SessionFactory,无论如何它都会在启动时增加 Hi 值,并且您会看到您的 Id 快速跳跃.

It is important to note however that this only works within the scope of a SessionFactory. If you are stopping/starting your application and reinitializing the SessionFactory a whole bunch, it's going to increment the Hi value upon startup anyway and you're going to see your Ids jump pretty quickly.

这篇关于NHibernate HiLo 解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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