Esempio n. 1
0
        public void When_committing_transaction_scope_will_commit_transaction()
        {
            object id;

            using (var tx = new TransactionScope())
            {
                using (ISession s = Sfi.OpenSession())
                {
                    id = s.Save(new Nums {
                        NumA = 1, NumB = 2, ID = 5
                    });
                }
                tx.Complete();
            }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    Nums nums = s.Get <Nums>(id);
                    Assert.IsNotNull(nums);
                    s.Delete(nums);

                    tx.Commit();
                }
        }
Esempio n. 2
0
        public async Task When_committing_transaction_scope_will_commit_transactionAsync()
        {
            object id;

            using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                using (ISession s = Sfi.OpenSession())
                {
                    id = await(s.SaveAsync(new Nums {
                        NumA = 1, NumB = 2, ID = 5
                    }));
                }
                tx.Complete();
            }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    Nums nums = await(s.GetAsync <Nums>(id));
                    Assert.IsNotNull(nums);
                    await(s.DeleteAsync(nums));

                    await(tx.CommitAsync());
                }
        }
Esempio n. 3
0
        public void When_using_two_sessions()
        {
            object id1, id2;

            using (var tx = new TransactionScope())
            {
                using (ISession s1 = sessions.OpenSession())
                    using (ISession s2 = sessions.OpenSession())
                    {
                        id1 = s1.Save(new Nums {
                            NumA = 1, NumB = 2, ID = 5
                        });

                        id2 = s2.Save(new Nums {
                            NumA = 1, NumB = 2, ID = 6
                        });

                        tx.Complete();
                    }
            }

            using (ISession s = sessions.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    Nums nums = s.Get <Nums>(id1);
                    Assert.IsNotNull(nums);
                    s.Delete(nums);

                    nums = s.Get <Nums>(id2);
                    Assert.IsNotNull(nums);
                    s.Delete(nums);

                    tx.Commit();
                }
        }
Esempio n. 4
0
        public void When_commiting_items_in_DTC_transaction_will_add_items_to_2nd_level_cache()
        {
            using (var tx = new TransactionScope())
            {
                using (var s = Sfi.OpenSession())
                {
                    s.Save(new Nums {
                        ID = 29, NumA = 1, NumB = 3
                    });
                }
                tx.Complete();
            }
            try
            {
                using (var tx = new TransactionScope())
                {
                    using (var s = OpenSession())
                    {
                        var nums = s.Load <Nums>(29);
                        Assert.AreEqual(1, nums.NumA);
                        Assert.AreEqual(3, nums.NumB);
                    }
                    tx.Complete();
                }

                //closing the connection to ensure we can't really use it.
                var connection = Sfi.ConnectionProvider.GetConnection();
                Sfi.ConnectionProvider.CloseConnection(connection);

                // The session is supposed to succeed because the second level cache should have the
                // entity to load, allowing the session to not use the connection at all.
                // Will fail if a transaction manager tries to enlist user supplied connection. Do
                // not add a transaction scope below.
                using (var s = Sfi.WithOptions().Connection(connection).OpenSession())
                {
                    Nums nums = null;
                    Assert.DoesNotThrow(() => nums = s.Load <Nums>(29), "Failed loading entity from second level cache.");
                    Assert.AreEqual(1, nums.NumA);
                    Assert.AreEqual(3, nums.NumB);
                }
            }
            finally
            {
                using (var s = OpenSession())
                    using (var tx = s.BeginTransaction())
                    {
                        var nums = s.Load <Nums>(29);
                        s.Delete(nums);
                        tx.Commit();
                    }
            }
        }
Esempio n. 5
0
        public void When_using_two_sessions_with_explicit_flush()
        {
            if (!Dialect.SupportsConcurrentWritingConnectionsInSameTransaction)
            {
                Assert.Ignore(Dialect.GetType().Name + " does not support concurrent connections in same transaction.");
            }
            if (!Dialect.SupportsDistributedTransactions)
            {
                Assert.Ignore(Dialect.GetType().Name + " does not support distributed transactions.");
            }

            object id1, id2;

            using (var tx = new TransactionScope())
            {
                using (ISession s1 = Sfi.OpenSession())
                    using (ISession s2 = Sfi.OpenSession())
                    {
                        id1 = s1.Save(new Nums {
                            NumA = 1, NumB = 2, ID = 5
                        });
                        s1.Flush();

                        id2 = s2.Save(new Nums {
                            NumA = 1, NumB = 2, ID = 6
                        });
                        s2.Flush();

                        tx.Complete();
                    }
            }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    Nums nums = s.Get <Nums>(id1);
                    Assert.IsNotNull(nums);
                    s.Delete(nums);

                    nums = s.Get <Nums>(id2);
                    Assert.IsNotNull(nums);
                    s.Delete(nums);

                    tx.Commit();
                }
        }
Esempio n. 6
0
        public async Task When_using_two_sessions_with_explicit_flushAsync()
        {
            if (!TestDialect.SupportsConcurrentTransactions)
            {
                Assert.Ignore(Dialect.GetType().Name + " does not support concurrent transactions.");
            }
            if (!TestDialect.SupportsDistributedTransactions)
            {
                Assert.Ignore(Dialect.GetType().Name + " does not support distributed transactions.");
            }

            object id1, id2;

            using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                using (ISession s1 = Sfi.OpenSession())
                    using (ISession s2 = Sfi.OpenSession())
                    {
                        id1 = await(s1.SaveAsync(new Nums {
                            NumA = 1, NumB = 2, ID = 5
                        }));
                        await(s1.FlushAsync());

                        id2 = await(s2.SaveAsync(new Nums {
                            NumA = 1, NumB = 2, ID = 6
                        }));
                        await(s2.FlushAsync());

                        tx.Complete();
                    }
            }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    Nums nums = await(s.GetAsync <Nums>(id1));
                    Assert.IsNotNull(nums);
                    await(s.DeleteAsync(nums));

                    nums = await(s.GetAsync <Nums>(id2));
                    Assert.IsNotNull(nums);
                    await(s.DeleteAsync(nums));

                    await(tx.CommitAsync());
                }
        }
Esempio n. 7
0
        public void When_using_two_sessions()
        {
            if (!TestDialect.SupportsConcurrentTransactions)
            {
                Assert.Ignore(Dialect.GetType().Name + " does not support concurrent transactions.");
            }

            object id1, id2;

            using (var tx = new TransactionScope())
            {
                using (ISession s1 = sessions.OpenSession())
                    using (ISession s2 = sessions.OpenSession())
                    {
                        id1 = s1.Save(new Nums {
                            NumA = 1, NumB = 2, ID = 5
                        });

                        id2 = s2.Save(new Nums {
                            NumA = 1, NumB = 2, ID = 6
                        });

                        tx.Complete();
                    }
            }

            using (ISession s = sessions.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    Nums nums = s.Get <Nums>(id1);
                    Assert.IsNotNull(nums);
                    s.Delete(nums);

                    nums = s.Get <Nums>(id2);
                    Assert.IsNotNull(nums);
                    s.Delete(nums);

                    tx.Commit();
                }
        }
Esempio n. 8
0
        public void Will_not_save_when_flush_mode_is_never()
        {
            object id;

            using (var tx = new TransactionScope())
            {
                using (ISession s = Sfi.OpenSession())
                {
                    s.FlushMode = FlushMode.Manual;
                    id          = s.Save(new Nums {
                        NumA = 1, NumB = 2, ID = 5
                    });
                }
                tx.Complete();
            }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    Nums nums = s.Get <Nums>(id);
                    Assert.IsNull(nums);
                    tx.Commit();
                }
        }
Esempio n. 9
0
        public async Task Will_not_save_when_flush_mode_is_neverAsync()
        {
            object id;

            using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                using (ISession s = Sfi.OpenSession())
                {
                    s.FlushMode = FlushMode.Manual;
                    id          = await(s.SaveAsync(new Nums {
                        NumA = 1, NumB = 2, ID = 5
                    }));
                }
                tx.Complete();
            }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    Nums nums = await(s.GetAsync <Nums>(id));
                    Assert.IsNull(nums);
                    await(tx.CommitAsync());
                }
        }