c# - NHibernate Hilo Sequence without NHibernate -
I have an application that uses NHibernate to save organizations in a database, ID for NIBNET I have another application that I need to save data in a single table, although in this application, I am not using NHibernate, is there an easy way to use HiLo without reference to NHibernate and mapping files?
& lt; Id name = "EntityId" not saved-value = "0" & gt; & Lt; Column name = "entity-id" sql-type = "int" not-null = "true" /> & Lt; Generator class = "solution" & gt; & Lt; Param name = "table" & gt; Hibernate_unique_key & lt; / Param & gt; & Lt; Param name = "column" & gt; Next_e & lt; / Param & gt; & Lt; Param name = "max_lo" & gt; 1000 & lt; / Param & gt; & Lt; / Generator & gt; & Lt; / Id & gt; Update: I created a new method to get the next ID, which I believe is the same behavior as the NHibernate HiLo sequence. I'm not putting any lock on the database, so if This was a high frequency table that I was putting on, it could be a problem, but for a very low frequency, there is a very small chance that there might be a concurrency problem.
/// & lt; Summary & gt; /// becomes the next unit id /// & lt; / Summary & gt; /// & lt; Returns & gt; & Lt; / Returns & gt; Public Static GetNextAvailableEntityId () {int max_lo = 1000; Int nextHi = DefaultContext.Database.SqlQuery & lt; Int & gt; (Select next_hi from "hibernate_unique_key"). Solo (); Int nextRangeStart = max_lo * nextHi; Int currentMax = DefaultContext.Database.SqlQuery & lt; Int & gt; ("Choose from units MAX (unit number)"). Solo (); If (Current Max & Lt; Next Range Start) {Current Mike + 1; } Else {DefaultContext.Database.ExecuteSqlCommand ("update hibernate_unique_key next_en = next_en + 1"); The return is next; }}
If there is a doubt, take a look at the NH code, make sure that you Apply it properly.
Something like this:
- Open a separate transaction, read the pay scale and the next high price, Commit
- When you run away from ID , Start over again
You can make an example of an ID generator for all transactions in the same example. process. Make sure your id generator thread is secure.
Comments
Post a Comment