Пример #1
0
        [Fact] // - too slow
        public async Task global_session_locks()
        {
            var settings = new SqlServerSettings();

            using (var conn1 = new SqlConnection(Servers.SqlServerConnectionString))
                using (var conn2 = new SqlConnection(Servers.SqlServerConnectionString))
                    using (var conn3 = new SqlConnection(Servers.SqlServerConnectionString))
                    {
                        await conn1.OpenAsync();

                        await conn2.OpenAsync();

                        await conn3.OpenAsync();

                        await settings.GetGlobalLock(conn1, 24);


                        try
                        {
                            // Cannot get the lock here
                            (await settings.TryGetGlobalLock(conn2, 24)).ShouldBeFalse();

                            // Can get the new lock
                            (await settings.TryGetGlobalLock(conn3, 25)).ShouldBeTrue();

                            // Cannot get the lock here
                            (await settings.TryGetGlobalLock(conn2, 25)).ShouldBeFalse();
                        }
                        finally
                        {
                            await settings.ReleaseGlobalLock(conn1, 24);

                            await settings.ReleaseGlobalLock(conn3, 25);
                        }
                    }
        }
Пример #2
0
        public async Task explicitly_release_global_session_locks()
        {
            var settings = new SqlServerSettings();

            using (var conn1 = new SqlConnection(Servers.SqlServerConnectionString))
                using (var conn2 = new SqlConnection(Servers.SqlServerConnectionString))
                    using (var conn3 = new SqlConnection(Servers.SqlServerConnectionString))
                    {
                        await conn1.OpenAsync();

                        await conn2.OpenAsync();

                        await conn3.OpenAsync();


                        await settings.GetGlobalLock(conn1, 1);


                        // Cannot get the lock here
                        (await settings.TryGetGlobalLock(conn2, 1)).ShouldBeFalse();


                        await settings.ReleaseGlobalLock(conn1, 1);


                        for (var j = 0; j < 5; j++)
                        {
                            if (await settings.TryGetGlobalLock(conn2, 1))
                            {
                                return;
                            }

                            await Task.Delay(250);
                        }

                        throw new Exception("Advisory lock was not released");
                    }
        }