Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Multiple DbCntexts select query can't work concurrently. #29

Description

@IvanZheng

Multiple DbContexts select query can't work concurrently. Please see the code below.
BTW, if I use async query, "System.ObjectDisposedException : Cannot access a disposed object." will occur.
`

    public async Task ConcurrentTest()
    {
        var tasks = new List<Task>();
        for (int i = 0; i < 20; i++)
        {
            var j = i;
            tasks.Add(Task.Run(() => InternalGetUsersTest(j)));
        }
        await Task.WhenAll(tasks);
    }

    private void InternalGetUsersTest(int i)
    {
        var builder = new DbContextOptionsBuilder().UseMongoDb("mongodb://10.100.7.46:9007/DemoDb");
        using (var dbContext = new DemoDbContext(builder.Options))
        {
            var database = dbContext.GetMongoDbDatabase();
            // !!! 20 concurrent tasks cost 3 seconds.
            var user1 = database.GetCollection<User>("users")
                                .Find(new ExpressionFilterDefinition<User>(u => u.Id == "xxx"));

            // !!! 20 concurrent tasks cost almost 20 seconds. It seems each task run one by one.
            var user2 = dbContext.Users
                                 .FirstOrDefault(u => u.Id == "xxx");
        }
    }`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions