mercredi 25 février 2015

Android SQLite transactions and multi threads

In my application, I call some webservices to update a database. Each webservice call is made in a specific thread, resulting in multiple threads updating the database object "at a time".


In each thread, I use transactions like that :



Thread 1 (webservice 1)
beginTransaction()
insert a row in the table 1
update a row in the table 1
endTransaction()

Thread 2 (webservice 2)
beginTransaction()
update a row in the table 2
update a row in the table 2
endTransaction()


But I struggle with one question I can't answer myself after googling it; As transactions are handled in different threads, are they distinct from each other? In other words, does the DB use a distinct "statement stack" or a common one?


From my readings, I understand transactions are in the same stack, i.e commiting the transaction in thread 1 may commit updates on table 2. For example, DB statement stack as I think it may happen:



beginTransaction() //Thread 1 begins a transaction
insert a row in the table 1
update a row in the table 1
beginTransaction() //Thread 2 begins a transaction
update a row in the table 2
update a row in the table 2
endTransaction() //Thread 2 ends a transaction
endTransaction() //Thread 1 ends a transaction


If it's true, how can I make my transactions really exclusives? Do I have to BEGIN EXCLUSIVE TRANSACTION and handle the SQLITE_BUSY error everywhere or is there something easier?


Aucun commentaire:

Enregistrer un commentaire