NHibernate: Increment function not getting next sequence value

0

I get the following error when I try to save an Entity with NHibernate:

System.Data.OracleClient.OracleException (0x80131938): ORA-00001: unique constraint (FEMADEV.TRN_INFLOW_DETAILS_HIS_PK) violated

My Controller Code:

TrnInflowDetailsHis trnInflowDetailsHis = Mapper.Map<TrnInflowDetailsHis>(trnInflowDetailsAud);
trnInflowDetailsHis.InflowSeq = trnInflowDetailsAud.Id;
trnInflowDetailsHisService.Add(trnInflowDetailsHis);

Mapping Code:

 public class TrnInflowDetailsHisMap : ClassMap<TrnInflowDetailsHis>
 {
    public TrnInflowDetailsHisMap()
    {
        Table("TRN_INFLOW_DETAILS_HIS");
        LazyLoad();
        Id(x => x.Id).Column("INFLOW_ID").GeneratedBy.Increment();
        Map(x => x.InflowSeq).Column("INFLOW_SEQ");
        Map(x => x.TranRef).Column("TRAN_REF");
        Map(x => x.Currency).Column("CURRENCY");
     }
  }

The problem here is, GeneratedBy.Increment() is working fine, but sometimes it is not incrementing next sequence value. I do not know what is the problem. So please any one help me to find the solution.

c#
asp.net-mvc
oracle
nhibernate
increment
asked on Stack Overflow Oct 18, 2016 by Md Aslam • edited Oct 18, 2016 by croxy

1 Answer

2

I would suspect that issue is more about multi concurrent instances, rather than a bug in the increment generator

The exception ORA-00001 means:

An UPDATE or INSERT statement attempted to insert a duplicate key. For Trusted Oracle configured in DBMS MAC mode, you may see this message if a duplicate entry exists at a different level.

Other words, someone already INSERTed ID value, which current thread tries to use. And that could easily happen in multi app instance accessing the same DB with an ID generator Increment.

The doc 5.1.5.1. generator :

increment

  • generates identifiers of any integral type that are unique only when no other process is inserting data into the same table. Do not use in a cluster.

So, just change this generator to anything else. The increment is maybe for development, or for almost immutable types. Or if you can be sure, that there is only one instance (one application) running at time.

answered on Stack Overflow Oct 18, 2016 by Radim Köhler

User contributions licensed under CC BY-SA 3.0