Writing to db in Asynchronous Loop

I have an application that continually reads from a serial port using an asynchronous loop. When the program receives the data it will need to write it to a database.

Sometimes, however the database write takes a very long time. Would it be frowned upon for me to begin another asynchronous function that writes to the database and call it from my asynchronous loop? Is there an alternative?



The thing you’re going to have to worry about is the async write-to-db calls ‘stacking’ up. If you’re reading data from the serial port faster/more often than you can write it to the db your application will eventually fall apart.

Do you know how many writes can stack on top of each other?

Every async call runs in a new thread. The order in which the run is essentially non-deterministic and the number really just depends on when you run out of memory.

If you’re writing to a DB in this async function and the writes can’t complete as fast as you’re reading first you’ll run up against the DB connection limit, then additional threads would block waiting to get a DB connection, and that’s where the ‘stacking’ would begin.

Thanks! That was exactly the answer I was looking for.

Also, what is the timeout on system.db.runPrepQuery? What happens if it does time out?

If you’re running these queries from the client there’s a 60s timeout between client and gateway for anything.

Additionally, there’s a timeout on getting a connection from the pool, which defaults to 5s and is settable. The actual querying does not have timeout…

system.db.runPrepQuery should throw an exception if any of these occur.