NHibernate HiLo的说明 [英] Explanation of NHibernate HiLo

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

问题描述

我正在努力弄清HiLo生成器在NHibernate中的工作方式.我已经在此处阅读了说明,

我的理解是,每个SessionFactory都会从数据库中检索高价值.因为我们无需访问数据库即可访问ID,因此可以提高性能.

上面链接的说明也指出:

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

这在Web应用程序中如何工作,因为我不仅只有一个SessionFactory,因此还有一个高价值.这是否意味着在断开连接的应用程序中,您可以在实体表中得到重复的(低)ID?

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

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

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

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

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

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

这是NHibernate中的一个主题,我一直在努力寻找相关文档.我阅读了整个NHibernate行动手册,但仍然没有深入了解它的工作原理.

谢谢 本

解决方案

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

我的最佳猜测是NHibernate的默认max_lo值是32768.因此,Hi值1将在32768处启动您的ID,然后将您运行到65535.Hi值2将在65536处开始并运行另一个max_lo ID.

基本上,您可以使用max_lo值来控制ID分段. 32768可能并不是每种情况的最佳值.

重要的是要注意,这仅在SessionFactory的范围内有效.如果要停止/启动应用程序并重新初始化SessionFactory,那么无论如何,它将在启动时增加Hi值,并且您会看到ID迅速跳跃.

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.

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:

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.

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>

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.

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

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.

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?

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.

Thanks Ben

解决方案

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.

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.

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

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天全站免登陆