Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

dart - Is there any potential bug in async code that work with DB?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...