sqlite-net icon indicating copy to clipboard operation
sqlite-net copied to clipboard

Question about Async Api

Open sezonis opened this issue 1 year ago • 1 comments

So with SQLiteAsyncConnection, it does not implement IDisposable. Therefore, do we instantiate this object once only?

Then call .CloseAsync when done? I'm trying to go for a multi threaded approach but I'm a little bit confused on how it works. If I created a new instance every time and then close the connection on query/insert done, would it wait for all other jobs to finish before closing or will it kick them out?

Also does async need to worry about the multithreaded inserts or is it all handled in the background? The official documentation on github doesn't really explain much but I'd assume it does if it uses the thread pool queue.

sezonis avatar Mar 06 '23 03:03 sezonis

SQLiteAsyncConnection is not an actual connection. it is just a "coordinator" that maintains a Dictionary<connectionstring,sqliteconnection> global dictionary.

you can have ten threads/tasks that try to open the same database file via SQLiteAsyncConnection, but actually SQLiteAsyncConnection will create just one single plain SQLiteConnection (not async) and give access to this single connection to just one thread/task at a time. The main job of SQLiteAsyncConnection is to just coordinate the access of multiple tasks to the same underlying SQLiteConnection, and to serialize transactions: just one task at a time can gain access to the underlying connection and write data, and it must conclude its transaction (by committing or rollbacking) before another task gets access to the same connection.

csm101 avatar Jul 14 '23 12:07 csm101