Esempio n. 1
0
        private static void PessimisticLockingExample2(IBucket bucket)
        {
            // tag::PessimisticLockingExample2[]
            bucket.Upsert("mymagic", new { magicLevel = 1, name = "Fire Magic" });

            // alternatively, if A never gets around to releasing the lock
            // then the lock will automatically be released after a certain time
            var magicAResult = bucket.GetAndLock <dynamic>("mymagic", TimeSpan.FromMilliseconds(5000));

            if (magicAResult.Success)
            {
                Console.WriteLine("Got a lock on 'mymagic'");
            }
            // try to get a new lock every second
            for (var i = 0; i < 10; i++)
            {
                var magicBResult = bucket.GetAndLock <dynamic>("mymagic", TimeSpan.FromMilliseconds(5000));
                if (magicBResult.Success)
                {
                    Console.WriteLine("Got a new lock on 'mymagic'!");
                    bucket.Unlock("mymagic", magicBResult.Cas); // unlock it right away
                    break;
                }
                else
                {
                    Console.WriteLine("'mymagic' document is still locked.");
                }

                Thread.Sleep(1000);
            }
            // end::PessimisticLockingExample2[]
        }
Esempio n. 2
0
        private static void PessimisticLockingExample1(IBucket bucket)
        {
            // tag::PessimisticLockingExample1[]
            // create initial document
            bucket.Upsert("myshield", new { defenseLevel = 1, name = "Mirror Shield" });

            // document is retrieved and locked by A
            var shieldAResult = bucket.GetAndLock <dynamic>("myshield", TimeSpan.FromMilliseconds(30000));
            var shieldA       = shieldAResult.Value;
            // B attempts to get and lock it as well
            var shieldBResult = bucket.GetAndLock <dynamic>("myshield", TimeSpan.FromMilliseconds(30000));

            if (!shieldBResult.Success)
            {
                Console.WriteLine("B couldn't establish a lock, trying a plain Get");
                shieldBResult = bucket.Get <dynamic>("myshield");
            }

            // B tries to make a change, despite not having a lock
            Console.WriteLine("'B' is updating the document");
            var shieldB = shieldBResult.Value;

            shieldB.defenseLevel = 3;
            IOperationResult bResult = bucket.Replace("myshield", shieldB);

            if (!bResult.Success)
            {
                Console.WriteLine($"B was unable to make a change: {bResult.Message}");
                Console.WriteLine();
            }

            // A can make the change, but MUST use the CAS value
            shieldA.defenseLevel = 2;
            IOperationResult aResult = bucket.Replace("myshield", shieldA);

            if (!aResult.Success)
            {
                Console.WriteLine($"A tried to make a change, but forgot to use a CAS value: {aResult.Message}");
                Console.WriteLine();
                Console.WriteLine("Trying again with CAS this time");
                aResult = bucket.Replace("myshield", shieldA, shieldAResult.Cas);
                if (aResult.Success)
                {
                    Console.WriteLine("Success!");
                }
            }

            // now, the document is unlocked
            // so B can try again
            bResult = bucket.Replace("myshield", shieldB);
            if (bResult.Success)
            {
                Console.WriteLine($"B was able to make a change.");
            }
            // end::PessimisticLockingExample1[]
        }
Esempio n. 3
0
        public void GetWithLock_WhenKeyIsLocked_ResponseStatusIsTemporaryFailure_And_MessageIsLOCK_ERROR()
        {
            _bucket.Upsert("roikatz", "bbbb");
            var res1 = _bucket.GetAndLock <string>("roikatz", 30);

            Assert.IsTrue(res1.Success);

            var res2 = _bucket.GetAndLock <string>("roikatz", 30);

            Assert.IsFalse(res2.Success);
            Assert.AreEqual(ResponseStatus.TemporaryFailure, res2.Status);
        }