arch / UnitOfWork

A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, multiple database with distributed transaction supported, and MySQL multiple databases/tables sharding supported.
MIT License
1.34k stars 343 forks source link

Concurrent issue with MySQL table sharding #113

Open chenchunan opened 5 years ago

chenchunan commented 5 years ago

We are trying to do table sharding with following code

` [HttpGet("test/{flag}")] public string test(string flag) { var _repository = _unitOfWork.GetRepository();

        Comment _data;

        if (flag.Equals("0"))
        {
            _repository.ChangeTable("Comment_0");
            _data = new Comment();
            _data.Title = "0";
            _data.Content = "0";
        }
        else
        {
            _repository.ChangeTable("Comment_1");
            _data = new Comment();
            _data.Title = "1";
            _data.Content = "1";
        }

        _repository.Insert(_data);

        _unitOfWork.SaveChanges();

        return $"is:{flag}";
    }`

Http client would send some requests with parameter 1 and 0 at the sam time. And the result shows that some rows of 1s go to table "Comment_0" and some rows of 0s go to table "Comment_1". It looks like a concurrency problem to me. Anyone can help?

Thanks!

rigofunc commented 4 years ago

Using AddScoped<T, T>?