lundi 22 février 2016

Nhibernate HiLo generator is generating duplicate ids

I am facing weird behavior of nHibernate. It does generate duplicate Ids with hi-lo generator in some circumstances. If the transaction is rolled back, and the number of id's to generate is more than hi-lo max, eventually it starts to generate the duplicates - Debug.Assert fails.

Maybe someone had faced something like that, or just knows how to prevent this ? Database is sqlite if it matters.

private void Test(ISessionFactory factory)
    {
        for (int i = 0; i < 3; i++)
        {
            using (var session = factory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                CheckIdGenerator(session.GetSessionImplementation(), 11);
                transaction.Rollback();
            }
        }
    }

private void CheckIdGenerator(ISessionImplementor sessionImplementor, int max)
{
    var factory = sessionImplementor.Factory;
    var mapping= (SingleTableEntityPersister)factory.GetClassMetadata(typeof(Dummy));
    var generator= mapping.IdentifierGenerator;

    var ids = new List<long>();
    for (int i = 0; i < max; i++)
    {
        var id = (long)generator.Generate(sessionImplementor, null);
        Debug.Assert(!ids.Contains(id));
        ids.Add(id);
    }
}

mapping:

class DummyMap : ClassMap<Dummy>
      {
            public DummyMap()
            {
                Id(x => x.Id)
                    .UniqueKey(nameof(Dummy.Id))
                    .GeneratedBy.HiLo("NHHiLoIdentity", "NextHiValue", "10",
                        $"[EntityName] = '[{nameof(Dummy)}]'")
                    .Not.Nullable();
            }
        }

Aucun commentaire:

Enregistrer un commentaire