Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
feitiana03120 opened this issue Sep 30, 2024 · 0 comments
Open
Labels

Comments

@feitiana03120
Copy link

feitiana03120 commented Sep 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant