Exemplo n.º 1
0
        [Fact] // -- too slow
        public async Task tx_session_locks()
        {
            var settings = new PostgresqlSettings();

            using (var conn1 = new NpgsqlConnection(Servers.PostgresConnectionString))
                using (var conn2 = new NpgsqlConnection(Servers.PostgresConnectionString))
                    using (var conn3 = new NpgsqlConnection(Servers.PostgresConnectionString))
                    {
                        await conn1.OpenAsync();

                        await conn2.OpenAsync();

                        await conn3.OpenAsync();

                        var tx1 = conn1.BeginTransaction();
                        await settings.GetGlobalTxLock(conn1, tx1, 4);


                        // Cannot get the lock here
                        var tx2 = conn2.BeginTransaction();
                        (await settings.TryGetGlobalTxLock(conn2, tx2, 4)).ShouldBeFalse();

                        // Can get the new lock
                        var tx3 = conn3.BeginTransaction();
                        (await settings.TryGetGlobalTxLock(conn3, tx3, 5)).ShouldBeTrue();

                        // Cannot get the lock here
                        (await settings.TryGetGlobalTxLock(conn2, tx2, 5)).ShouldBeFalse();

                        tx1.Rollback();
                        tx2.Rollback();
                        tx3.Rollback();
                    }
        }
Exemplo n.º 2
0
        public async Task explicitly_release_global_tx_session_locks()
        {
            var settings = new PostgresqlSettings();

            using (var conn1 = new NpgsqlConnection(Servers.PostgresConnectionString))
                using (var conn2 = new NpgsqlConnection(Servers.PostgresConnectionString))
                    using (var conn3 = new NpgsqlConnection(Servers.PostgresConnectionString))
                    {
                        await conn1.OpenAsync();

                        await conn2.OpenAsync();

                        await conn3.OpenAsync();

                        var tx1 = conn1.BeginTransaction();
                        await settings.GetGlobalTxLock(conn1, tx1, 2);


                        // Cannot get the lock here
                        var tx2 = conn2.BeginTransaction();
                        (await settings.TryGetGlobalTxLock(conn2, tx2, 2)).ShouldBeFalse();


                        tx1.Rollback();


                        for (var j = 0; j < 5; j++)
                        {
                            if (await settings.TryGetGlobalTxLock(conn2, tx2, 2))
                            {
                                tx2.Rollback();
                                return;
                            }

                            await Task.Delay(250);
                        }

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