DapperLib / DapperLib/Dapper.Contrib

Dapper.Contrib InsertAsync w/ mysql has problems with inserted ID (auto increment key column) and high concurrency

Open
#63 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
293
Forks
109
PR merge metrics
No merged PRs in 30d

Description

Using the latest MySQL driver 7.07-m61 without transactions I was running into issues where multiple inserts at the same time (on different MySQL connections but from the same pool) were getting the wrong (or no/0) values back for primary key auto increment integer columns. This was only an issue in high frequently inserts. By wrong I mean the insert ID for a different recently inserted item was assigned to another item.
This was with single inserts (not multiple in one call).
In theory last_insert_id should apple to the current connection however something is clearly amiss. I fixed this temporarily with a hack to use DBCommand .LastInsertedId (as on the exact command it can't miss) but not a great solution. Will be filing a feature request that may allow for lastinsertedid solution using the oncompleted feature.

Also this is not extremely high concurrency maybe only a 6-12 items in a second or two, but over a somewhat slow link which might exacerbate the problem. Could look at dual statement queries to do it in the insert query itself (like done in some other sql drivers) otherwise.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Dapper.Contrib's InsertAsync path for single inserts and reproduce the reported high-concurrency behavior across pooled MySQL connections. Compare the returned IDs with DBCommand.LastInsertedId and the connection-scoped last_insert_id behavior described in the issue. Done means each inserted item reliably receives its own auto-increment ID, including under concurrent inserts.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, mysql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.