本文介绍了NHibernate高/低-ID的差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:使用Lo 100为MyEntity初始化了Hi/Lo

Scenario: Hi/Lo is initialized for MyEntity with Lo 100

表为空.

两个具有不同连接的会话都插入了三个项目.

Two sessions with different connections have both inserted three items.

TableIds

1
2
3
100
101
102

如果稍后有第三个出现,并插入了三个项目:

If a third one comes in at a later time and inserts three items:

TableIds

...
200
201
202

有没有办法消除这些差距?

推荐答案

Lo值是针对SessionFactory而不是Session存储的.重新启动应用程序并创建SessionFactory的新实例时,您只会碰到缝隙.

The Lo value is stored against the SessionFactory, not the Session. You only ever get gaps when you restart your application and create a new instance of the SessionFactory.

从数据库中提取一个新的Hi值并将其存储在SessionFactory中,因此,如果您有一个Web场,则每个站点都将拥有自己的SessionFactory实例,而每个SessionFactory将从数据库中拥有自己的下一个" Hi值.当Lo值用尽时,它将把Hi更新为数据库中下一个可用的Hi.

A new Hi value is pulled from the database and stored in the SessionFactory, so if you have a web farm, each site will have it's own instance of the SessionFactory which will each have it's own 'next' Hi value from the database. When it runs out of Lo values it will update the Hi to the next available Hi from the database.

如果您有客户端应用程序,那么我建议您完全不使用HiLo,而要使用GuidComb,它是一个顺序的Guid,并且您不会有任何差距.

If you have client applications then I would recommend not using HiLo at all, instead use a GuidComb, it's a sequential Guid and you wont have an issue with gaps.

但是,由于它是一个现有应用程序,并且您无法真正更改标识符,因此我建议您要求客户端应用程序通过具有自己的SF的Web服务进行插入,这样您就可以维护单个Hi而不是多个Hi您好,每个客户应用都可以.

Since it's an existing app however and you can't really go changing the identifier, I would recommend requiring the client applications to insert via a Web Service which has it's own SF, that way you can maintain a single Hi instead of multiple Hi's per client app.

如果您无法做到这一点,那么您将不得不降低Lo.

If you can't do that then you're going to have to lower your Lo.

这篇关于NHibernate高/低-ID的差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:20