Exemple #1
0
        public void EarlyCommit()
        {
            var id1 = Guid.NewGuid();

            try
            {
                using (var container = RhetosProcessHelper.CreateTransactionScopeContainer())
                {
                    container.CommitChanges(); // CommitChanges is incorrectly places at this position.
                    var context = container.Resolve <Common.ExecutionContext>();
                    context.Repository.TestEntity.BaseEntity.Insert(new TestEntity.BaseEntity {
                        ID = id1
                    });
                    throw new FrameworkException(nameof(EarlyCommit)); // The exception is not handled within transaction scope to discard the transaction.
                }
            }
            catch (FrameworkException ex)
            {
                Console.WriteLine($"{ex.GetType().Name}: {ex.Message}");
            }

            using (var container = RhetosProcessHelper.CreateTransactionScopeContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();
                Assert.IsTrue(context.Repository.TestEntity.BaseEntity.Query(new[] { id1 }).Any()); // The transaction is committed because of incorrect implementation pattern above.
            }
        }
Exemple #2
0
        public void RollbackByDefault()
        {
            var id1 = Guid.NewGuid();

            try
            {
                using (var container = RhetosProcessHelper.CreateTransactionScopeContainer())
                {
                    var context = container.Resolve <Common.ExecutionContext>();
                    context.Repository.TestEntity.BaseEntity.Insert(new TestEntity.BaseEntity {
                        ID = id1
                    });
                    throw new FrameworkException(nameof(RollbackByDefault)); // The exception that is not handled within transaction scope.
#pragma warning disable CS0162                                               // Unreachable code detected
                    container.CommitChanges();
#pragma warning restore CS0162                                               // Unreachable code detected
                }
            }
            catch (FrameworkException ex)
            {
                Console.WriteLine($"{ex.GetType().Name}: {ex.Message}");
            }

            using (var container = RhetosProcessHelper.CreateTransactionScopeContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();
                Assert.IsFalse(context.Repository.TestEntity.BaseEntity.Query(new[] { id1 }).Any());
            }
        }
Exemple #3
0
        public void IndependentTransactions()
        {
            const int threadCount = 2;

            int initialCount;

            using (var container = RhetosProcessHelper.CreateTransactionScopeContainer())
            {
                RhetosProcessHelper.CheckForParallelism(container.Resolve <ISqlExecuter>(), threadCount);

                var context = container.Resolve <Common.ExecutionContext>();
                initialCount = context.Repository.TestEntity.BaseEntity.Query().Count();
            }

            var id1 = Guid.NewGuid();

            Parallel.For(0, threadCount, thread =>
            {
                using (var container = RhetosProcessHelper.CreateTransactionScopeContainer())
                {
                    var context = container.Resolve <Common.ExecutionContext>();

                    Assert.AreEqual(initialCount, context.Repository.TestEntity.BaseEntity.Query().Count());
                    Thread.Sleep(100);
                    context.Repository.TestEntity.BaseEntity.Insert(new TestEntity.BaseEntity {
                        ID = id1
                    });                                                                                      // Each thread uses the same ID to make sure only one thread can run this code at same time.
                    Assert.AreEqual(initialCount + 1, context.Repository.TestEntity.BaseEntity.Query().Count());
                }
            });
        }
Exemple #4
0
        public void ExplicitCommit()
        {
            var id1 = Guid.NewGuid();

            using (var container = RhetosProcessHelper.CreateTransactionScopeContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();
                context.Repository.TestEntity.BaseEntity.Insert(new TestEntity.BaseEntity {
                    ID = id1
                });
                container.CommitChanges();
            }

            using (var container = RhetosProcessHelper.CreateTransactionScopeContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();
                Assert.IsTrue(context.Repository.TestEntity.BaseEntity.Query(new[] { id1 }).Any());
            }
        }