I wrote next code that run generation and insert SQL in DB db by incoming JSON.
Future<dbEnums> matchAndInsert(String section, Map jsonMap) async {
tableToInsert = "notifications";
try {
db.saveOriginalJson(jsonMap);
await db.KVToTableInsert(tableToInsert, jsonMap);
KVToTableInsert
inside store some temp data (like names of nested tables that I am extracting from JSON).
The request handler is done in next way:
app.post('/foo', (req, res) async {
dbEnums insertResult = await matchQuery.foo("tablename", req.body);
This code work fine if I am sending data to handler from one thread (desktop C# app that can work in n threads). But it crush if I am trying to do it with few threads of parser (different root and nested tables). In error I am seeing that it's try to insert data to table2 while some fields are from table1.
Do I an using async wrong? Because it's look like data in method is overlapping?
How to fix code?
Am I right understand that I am overwriting field in saveOriginalJson
before KVToTableInsert
is complete? How to fix it?
db.saveOriginalJson(jsonMap);
await db.KVToTableInsert(tableToInsert, jsonMap);
P.S. more code if it needed https://gist.github.com/bubnenkoff/517e485757955a52494280977a85fb66
question from:
https://stackoverflow.com/questions/65641682/is-there-any-potential-bug-in-async-code-that-work-with-db 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…