LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[BUG][v 4.1.4] Timeout.DataBase is locked for more than 00:01:00.

Open feitiana03120 opened this issue 4 months ago • 0 comments

Version version :4.1.4 OS:WINDOWS .NET FRAMEWORK:.NET 4.7.2

Describe the bug When multiple threads concurrently read and write to the LiteDB database, occasional LockTimeout exceptions may occur.

Code to Reproduce using LiteDB; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks;

namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { try { var db = new LiteDatabase(@"Filename=C:\Temp\MyData.db;mode=shared;");

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            Console.WriteLine("the start");
            Parallel.For(0, 5000, i =>
            {

                // Get a collection (or create, if doesn't exist)
                var col = db.GetCollection<Customer>("customers");

                if (i % 2 == 0)
                {
                    // Create your new customer instance
                    var customer = new Customer
                    {
                        Name = "John Doe",
                        Phones = new string[] { "8000-0000", "9000-0000" },
                        IsActive = true
                    };

                    // Insert new customer document (Id will be auto-incremented)
                    col.Insert(customer);

                    // Update a document inside a collection
                    customer.Name = "Jane Doe";

                    col.Update(customer);
                }
                else
                {
                    // and now we can query phones
                    var r = col.FindOne(x => x.Name == "John Doe");
                }

            });
            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;
            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds,
                ts.Milliseconds / 10);
            Console.WriteLine("Runtime " + elapsedTime);
            Console.WriteLine("the end");
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
            Console.WriteLine(ex);
            Console.ReadLine();
        }

    }

    // Create your POCO class entity
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string[] Phones { get; set; }
        public bool IsActive { get; set; }
    }
}

}

Expected behavior Synchronous lock is expected in the LockService,not ReaderWriterLockSlim

Screenshots/Stacktrace

feitiana03120 avatar Sep 30 '24 17:09 feitiana03120