Example #1
0
        public SequenceGenerator()
        {
            int count = MaxGenerationAttemptsCount;

            while (true)
            {
                try {
                    _explicitUnitOfWork = new ExplicitUnitOfWork(_defaultDataLayer);
                    var sequences = new XPCollection(_explicitUnitOfWork, _sequenceObjectType);
                    foreach (XPBaseObject seq in sequences)
                    {
                        seq.Save();
                    }
                    _explicitUnitOfWork.FlushChanges();
                    break;
                }
                catch (LockingException) {
                    Close();
                    count--;
                    if (count <= 0)
                    {
                        throw;
                    }
                    Thread.Sleep(MinGenerationAttemptsDelay * count);
                }
            }
        }
Example #2
0
        public void Locks_Only_Modified_Tables_When_Different_datalyer()
        {
            using (var application = SequenceGeneratorModule().Application){
                var defaultDataLayer1  = NewSimpleDataLayer(application);
                var explicitUnitOfWork = new ExplicitUnitOfWork(defaultDataLayer1);
                var testObject         = new TestObject(explicitUnitOfWork);
                explicitUnitOfWork.FlushChanges();

                var defaultDataLayer2   = NewSimpleDataLayer(application);
                var explicitUnitOfWork1 = new ExplicitUnitOfWork(defaultDataLayer2);
                var testObject2         = new TestObject2(explicitUnitOfWork1);
                explicitUnitOfWork1.FlushChanges();

                Should.Throw <ShouldCompleteInException>(() => Should.CompleteIn(
                                                             () => explicitUnitOfWork.GetObjectByKey <TestObject2>(testObject2.Oid).ShouldNotBeNull(),
                                                             TimeSpan.FromSeconds(1)));
                Should.Throw <ShouldCompleteInException>(() => Should.CompleteIn(
                                                             () => explicitUnitOfWork1.GetObjectByKey <TestObject>(testObject.Oid).ShouldNotBeNull(),
                                                             TimeSpan.FromSeconds(1)));
                explicitUnitOfWork.Close();
                defaultDataLayer1.Dispose();
                explicitUnitOfWork1.Close();
                defaultDataLayer2.Dispose();
            }
        }
Example #3
0
        public SequenceGenerator(Dictionary <string, bool> lockedSequenceTypes)
        {
            int count = MaxGenerationAttemptsCount;

            while (true)
            {
                try {
                    euow = new ExplicitUnitOfWork(DefaultDataLayer);
                    //Dennis: It is necessary to update all sequences because objects graphs may be complex enough, and so their sequences should be locked to avoid a deadlock.
                    XPCollection <Sequence> sequences = new XPCollection <Sequence>(euow, new InOperator("TypeName", lockedSequenceTypes.Keys), new SortProperty("TypeName", SortingDirection.Ascending));
                    foreach (Sequence seq in sequences)
                    {
                        seq.Save();
                    }
                    euow.FlushChanges();
                    break;
                }
                catch (LockingException) {
                    Close();
                    count--;
                    if (count <= 0)
                    {
                        throw;
                    }
                    Thread.Sleep(MinGenerationAttemptsDelay * count);
                }
            }
        }
        public SequenceGenerator(Dictionary <string, bool> lockedSequenceTypes)
        {
            int count = MaxGenerationAttemptsCount;

            while (true)
            {
                try
                {
                    euow = new ExplicitUnitOfWork(DefaultDataLayer);
                    seq  = euow.FindObject <Sequence>(new InOperator("TypeName", lockedSequenceTypes.Keys));
                    //XPCollection<Sequence> sequences = new XPCollection<Sequence>(euow, new InOperator("TypeName", lockedSequenceTypes.Keys), new SortProperty("TypeName", DevExpress.Xpo.DB.SortingDirection.Ascending));
                    //foreach (Sequence seq in sequences)
                    if (seq == null)
                    {
                        throw new LockingException();
                    }
                    seq.Save();
                    euow.FlushChanges();
                    break;
                }
                catch (LockingException)
                {
                    Close();
                    count--;
                    if (count <= 0)
                    {
                        throw;
                    }
                    Thread.Sleep(MinGenerationAttemptsDelay * count);
                }
            }
        }
Example #5
0
        public async Task Generate_Sequences_When_Locks_From_ExplicitUow()
        {
            using var application = NewApplication();
            var simpleDataLayer = NewSimpleDataLayer(application);

            SequenceGeneratorModule(application);
            using (var objectSpace = application.CreateObjectSpace()){
                objectSpace.SetSequence <TestObject>(o => o.SequentialNumber, 10);
            }
            var testObjectObserver = SequenceGeneratorService.Sequence.OfType <TestObject>().SubscribeReplay();
            var explicitUnitOfWork = new ExplicitUnitOfWork(simpleDataLayer);

            new TestObject(explicitUnitOfWork);
            explicitUnitOfWork.FlushChanges();
            await TestObjects(application, false, 1)
            .Merge(Unit.Default.ReturnObservable().Delay(TimeSpan.FromMilliseconds(300))
                   .Do(_ => explicitUnitOfWork.CommitChanges())).FirstAsync();

            explicitUnitOfWork.Close();
            simpleDataLayer.Dispose();

            var firstAsync = await testObjectObserver.FirstAsync();

            firstAsync.SequentialNumber.ShouldBe(10);
        }
Example #6
0
 public void Close()
 {
     if (euow != null)
     {
         if (euow.InTransaction)
         {
             euow.RollbackTransaction();
         }
         euow.Dispose();
         euow = null;
     }
 }
Example #7
0
 public SequenceGenerator() {
     int count = MaxGenerationAttemptsCount;
     while (true) {
         try {
             _explicitUnitOfWork = new ExplicitUnitOfWork(_defaultDataLayer);
             var sequences = new XPCollection(_explicitUnitOfWork, _sequenceObjectType);
             foreach (XPBaseObject seq in sequences)
                 seq.Save();
             _explicitUnitOfWork.FlushChanges();
             break;
         } catch (LockingException) {
             Close();
             count--;
             if (count <= 0)
                 throw;
             Thread.Sleep(MinGenerationAttemptsDelay * count);
         }
     }
 }
        public void Throws_if_another_connection_exists_with_same_DataLayer()
        {
            using var application      = SequenceGeneratorModule().Application;
            using var defaultDataLayer = NewSimpleDataLayer(application);
            var explicitUnitOfWork = new ExplicitUnitOfWork(defaultDataLayer);
            var testObject         = new TestObject(explicitUnitOfWork);

            explicitUnitOfWork.FlushChanges();
            explicitUnitOfWork.GetObjectByKey <TestObject>(testObject.Oid).ShouldNotBeNull();

            var explicitUnitOfWork1 = new ExplicitUnitOfWork(defaultDataLayer);


            new TestObject2(explicitUnitOfWork1);

            Should.Throw <InvalidOperationException>(() => explicitUnitOfWork1.FlushChanges(), SequenceGeneratorService.ParallelTransactionExceptionMessage);

            explicitUnitOfWork.Close();
            explicitUnitOfWork1.Close();
        }
        public async Task UnLocks_current_Record_When_Commit_Changes()
        {
            using var application = SequenceGeneratorModule().Application;
            var defaultDataLayer1 = await application.ObjectSpaceProvider.SequenceGeneratorDatalayer();

            var explicitUnitOfWork = new ExplicitUnitOfWork(defaultDataLayer1);

            explicitUnitOfWork.SetSequence <TestObject>(o => o.SequentialNumber);
            explicitUnitOfWork.FlushChanges();

            var defaultDataLayer2   = NewSimpleDataLayer(application);
            var explicitUnitOfWork1 = new ExplicitUnitOfWork(defaultDataLayer2);

            explicitUnitOfWork.CommitChanges();
            explicitUnitOfWork1.GetSequenceStorage(typeof(TestObject));

            explicitUnitOfWork.Close();
            defaultDataLayer1.Dispose();
            explicitUnitOfWork1.Close();
            defaultDataLayer2.Dispose();
        }
        private static long RecreateAddress(ViewRecord selectedRow)
        {
            long personOid;

            using (ExplicitUnitOfWork explicitUnitOfWork = new ExplicitUnitOfWork(DatabaseHelper.DataLayer)) {
                //Get selected person object.
                Person person = explicitUnitOfWork.GetObjectByKey <Person>(selectedRow["Oid"]);
                if (person == null || person.Address == null)
                {
                    throw new ArgumentNullException("person");
                }
                //Remember person key value.
                personOid = person.Oid;
                Address       address       = person.Address;
                long          addressOid    = address.Oid;
                List <Person> referenceList = new List <Person>(address.Persons);
                //Reset references to the Address object.
                for (int i = 0; i < referenceList.Count; i++)
                {
                    referenceList[i].Address = null;
                }
                //Delete the Address object.
                explicitUnitOfWork.Delete(address);
                //Save changes to the database in case of an explicit transaction.
                explicitUnitOfWork.FlushChanges();

                //Create a new instance of the Address object.
                address = DatabaseHelper.CreateNewAddress(explicitUnitOfWork, addressOid);
                //Recover references to the Address object.
                for (int i = 0; i < referenceList.Count; i++)
                {
                    referenceList[i].Address = address;
                }
                explicitUnitOfWork.CommitChanges();
            }
            return(personOid);
        }
Example #11
0
 public SequenceGenerator(Dictionary<string, bool> lockedSequenceTypes)
 {
     int count = MaxGenerationAttemptsCount;
     while (true) {
         try {
             euow = new ExplicitUnitOfWork(DefaultDataLayer);
             //Dennis: It is necessary to update all sequences because objects graphs may be complex enough, and so their sequences should be locked to avoid a deadlock.
             var sequences = new XPCollection<Sequence>(euow,
                                                        new InOperator("TypeName", lockedSequenceTypes.Keys),
                                                        new SortProperty("TypeName", SortingDirection.Ascending));
             foreach (Sequence seq in sequences)
                 seq.Save();
             euow.FlushChanges();
             break;
         } catch (LockingException) {
             Close();
             count--;
             if (count <= 0)
                 throw;
             Thread.Sleep(MinGenerationAttemptsDelay * count);
         }
     }
 }
Example #12
0
 public void Close()
 {
     if (euow != null) {
         if (euow.InTransaction)
             euow.RollbackTransaction();
         euow.Dispose();
         euow = null;
     }
 }
Example #13
0
 public SequenceGenerator(IDataLayer dataLayer)
 {
     euow      = new ExplicitUnitOfWork(dataLayer);
     classInfo = euow.GetClassInfo <T>();
 }
Example #14
0
 private SequenceGenerator()
 {
     _explicitUnitOfWork = new ExplicitUnitOfWork(_defaultDataLayer);
 }
Example #15
0
        public SequenceGenerator(Dictionary<string, bool> lockedSequenceTypes)
        {
            var count = MaxGenerationAttemptsCount;
            while (true)
            {
                try
                {
                    euow = new ExplicitUnitOfWork(DefaultDataLayer);
                    var sequences = new XPCollection<Sequence>(euow, new InOperator("TypeName", lockedSequenceTypes.Keys), new SortProperty("TypeName", DevExpress.Xpo.DB.SortingDirection.Ascending));
                    foreach (Sequence seq in sequences)
                    {
                        seq.Save();
                    }
                    euow.FlushChanges();

                    break;
                }
                catch (LockingException)
                {
                    Close();
                    count--;
                    if (count <= 0)
                    {
                        throw;
                    }

                    Thread.Sleep(MinGenerationAttemptsDelay * count);
                }
            }
        }