Exemplo n.º 1
0
        public static int GetNextUniqueValue(IDataLayer dataLayer, string sequencePrefix)
        {
            if (dataLayer == null)
            {
                throw new ArgumentNullException("dataLayer");
            }
            string realSeqPrefix = sequencePrefix + '@' + GetSequencePrefix(dataLayer);

            return(XpoSequencer.GetNextValue(dataLayer, realSeqPrefix));
        }
        public static int GetNextValue(IDataLayer dataLayer, string sequencePrefix)
        {
            if (dataLayer == null)
            {
                throw new ArgumentNullException("dataLayer");
            }
            if (sequencePrefix == null)
            {
                sequencePrefix = string.Empty;
            }

            int attempt = 1;

            while (true)
            {
                try {
                    using (Session generatorSession = new Session(dataLayer)) {
                        XpoSequencer generator = generatorSession.FindObject <XpoSequencer>(new OperandProperty("SequencePrefix") == sequencePrefix);
                        if (generator == null)
                        {
                            generator = new XpoSequencer(generatorSession);
                            generator.SequencePrefix = sequencePrefix;
                        }
                        generator.Counter++;
                        generator.Save();
                        return(generator.Counter);
                    }
                }
                catch (LockingException) {
                    if (attempt >= MaxIdGenerationAttempts)
                    {
                        throw;
                    }
                }
                if (attempt > MaxIdGenerationAttempts / 2)
                {
                    Thread.Sleep(new Random().Next(MinConflictDelay, MaxConflictDelay));
                }

                attempt++;
            }
        }