5

I need to call multiple api's each executing in separate thread and insert the respective data from response to sqlite database without causing locks. Can anyone help me in this regard with a working example that I can refer to.

0

1 Answer 1

9

I need to call multiple api's each executing in separate thread and insert the respective data from response to sqlite database without causing locks.

You don't need to do anything special. Multiple threads can make use of the same SQLiteDatabase object without you doing locking at the application level. Sqlite does it's own locking under the covers. You should never get deadlocks but one thread will have to wait for the other thread to finish making its insert.

See these questions/answers:

Sqlite under Android is single threaded. Even if multiple threads were using the same database connection, my understanding is that they would be blocked from running concurrently. There is no way to get around this limitation. If you open two connections to the same database, this would corrupt the database because database updates would not be coordinated.

Sign up to request clarification or add additional context in comments.

4 Comments

What about multiprocessing instead of threading where multiple processes are spawned at a time, can I use SQLIte for that purpose by using a single connection across the processes?
I found this via Google @Volatil3: sqlite.org/….
so how do I make sure inserts are not being done at same time while I am spawning multiple processes?
Read that page. Sqlite uses file locking so only one process can update at a time. If anyone is updating then no one can be reading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.