In my Android Studio project, I am utilizing SQLite with Room for database operations. The background service contains several suspend functions that are responsible for updating rows within the SQLite database.
Considering that SQLite applies table-level locks, it raises the question of what occurs when multiple suspend functions attempt to update different rows of the same SQLite database simultaneously.
Will the SQLite database become locked, leading to a crash in the application?
when multiple suspend functions attempt to update different rows of the same SQLite database simultaneously.
The suspend functions are not separate threads; they just provide continuation that allows different tasks to run concurrently within a coroutine. Running concurrently doesn't mean simultaneously/in parallel, but run for a short period of time in successive manner.
So, if you update different rows with Room in multiple suspend functions within the same coroutine/job; they actually doesn't run simultaneously.
But, what you'd worry about, is when you run these updates in different threads (different coroutine Dispatchers); Generally speaking, some database systems restrict the use of the same database connection at the same time in more than one thread; Room doesn't, Room takes care of this as it's thread safe and support multithreaded.
Fortunately, Room is Thread Safe, and run each Dao
method annotated with Room CRUD annotations in a separate standalone database Transaction (Documentation Reference).