2

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

The table is empty.

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

Is there a way to not get these gaps?

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Daniel
  • 8,133
  • 5
  • 36
  • 51
  • Almost all "sequential" assignment methods will generate gaps in some circumstances (or will produce a performance bottleneck). It would be better to re-work whatever it is that has issues with these gaps existing. – Damien_The_Unbeliever May 04 '11 at 09:21
  • Are you working on a dev machine (with the app stop-starting all the time)? This will probably be less of an issue in production if you keep the application spooled up. You can also reduce the number you are taking each time in your config to e.g. 10 which will mitigate things somewhat. – UpTheCreek May 04 '11 at 09:49

2 Answers2

4

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.

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.

Edit:

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.

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.

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

Phill
  • 18,398
  • 7
  • 62
  • 102
  • As of now we have one SF for webapp, one SF for services and X SF for clients, and MaxLo = 100. The webapp and services is no big deal, but every client connection could cause a lot of gaps as I see it. Ideas, thoughts? Turn it of for clients perhaps? – Daniel May 04 '11 at 10:02
  • @Daniel - Updated my answer with suggestions based on you're comment. – Phill May 04 '11 at 14:29
  • Path was allready chosen with identities. Of course a change can be made, with "a little of work". I would, as you suggest, also go that route with a common service layer. But if that's not an option...? – Daniel May 04 '11 at 19:04
  • @Daniel - It sounds like you don't have many options at all. Don't forget tho, there are 2,147,483,647 numbers to an Int32. I doubt you would ever run out of values even tho you have gaps, unless you had 100's of client app's running which were restarted multiple times a day, then it may be possible to use all those valid numbers. – Phill May 05 '11 at 00:28
2

As Phill quite rightly pointed out the gaps appear when you build a sessionFactory e.g. the application restarts etc or you are running on the cloud and each node has its own set of Hi and lo.

You could reduce the lo to 10 rather than 100 to reduce the gaps and/or I recommend using int64 rather than int32 for ease of mind.

However does the gaps actually matter? Would you ever see yourself running out?

I have never read anything negative about performance (database issues) with a large number of gaps when using Hilo. The only thing I have seen is that some people complain that they run out when using int32 or when the lo has been set to high.

Rippo
  • 22,117
  • 14
  • 78
  • 117