public void WhenCommittingItemsAfterSessionDisposalWillAddThemTo2ndLevelCache()
        {
            int          id;
            const string notNullData = "test";

            using (var tx = new TransactionScope())
            {
                using (var s = OpenSession())
                {
                    var person = new CacheablePerson {
                        NotNullData = notNullData
                    };
                    s.Save(person);
                    id = person.Id;

                    ForceEscalationToDistributedTx.Escalate();

                    s.Flush();
                }
                tx.Complete();
            }

            DodgeTransactionCompletionDelayIfRequired();

            using (var tx = new TransactionScope())
            {
                using (var s = OpenSession())
                {
                    ForceEscalationToDistributedTx.Escalate();

                    var person = s.Load <CacheablePerson>(id);
                    Assert.That(person.NotNullData, Is.EqualTo(notNullData));
                }
                tx.Complete();
            }

            // Closing the connection to ensure we can't actually 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 = WithOptions().Connection(connection).OpenSession())
            {
                CacheablePerson person = null;
                Assert.DoesNotThrow(() => person = s.Load <CacheablePerson>(id), "Failed loading entity from second level cache.");
                Assert.That(person.NotNullData, Is.EqualTo(notNullData));
            }
        }
Example #2
0
        public async Task WhenCommittingItemsAfterSessionDisposalWillAddThemTo2ndLevelCacheAsync()
        {
            int          id;
            const string notNullData = "test";

            using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                using (var s = OpenSession())
                {
                    var person = new CacheablePerson {
                        NotNullData = notNullData
                    };
                    await(s.SaveAsync(person));
                    id = person.Id;

                    await(s.FlushAsync());
                }
                tx.Complete();
            }

            using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                using (var s = OpenSession())
                {
                    var person = await(s.LoadAsync <CacheablePerson>(id));
                    Assert.That(person.NotNullData, Is.EqualTo(notNullData));
                }
                tx.Complete();
            }

            // Closing the connection to ensure we can't actually use it.
            var connection = await(Sfi.ConnectionProvider.GetConnectionAsync(CancellationToken.None));

            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 = WithOptions().Connection(connection).OpenSession())
            {
                CacheablePerson person = null;
                Assert.DoesNotThrowAsync(async() => person = await(s.LoadAsync <CacheablePerson>(id)), "Failed loading entity from second level cache.");
                Assert.That(person.NotNullData, Is.EqualTo(notNullData));
            }
        }
        public void WhenCommittingItemsWillAddThemTo2ndLevelCache()
        {
            int          id;
            const string notNullData = "test";

            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    var person = new CacheablePerson {
                        NotNullData = notNullData
                    };
                    s.Save(person);
                    id = person.Id;

                    t.Commit();
                }

            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    var person = s.Load <CacheablePerson>(id);
                    Assert.That(person.NotNullData, Is.EqualTo(notNullData));
                    t.Commit();
                }

            // Closing the connection to ensure we can't actually 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())
            {
                CacheablePerson person = null;
                Assert.DoesNotThrow(
                    () => person = s.Load <CacheablePerson>(id),
                    "Failed loading entity from second level cache.");
                Assert.That(person.NotNullData, Is.EqualTo(notNullData));
            }
        }