3

i have a question about a specific example in android. Lets say iam running a Room database insertion within a thread/asyncTask.

Thread
{
  mDatabase.repoMethods().insert1
  mDatabase.repoMethods().insert2
  mDatabase.repoMethods().insert3                    
}

If i stop the thread when the insertion is happening what is the end result? For the sake of the example lets say i stop the thread when insertion1 is happening. Will it complete first and then terminate? or it will execute the other insertions and then?

Nick
  • 2,818
  • 5
  • 42
  • 60

1 Answers1

1

I'll try to answer this question in an investigative manner because I wasn't sure about the answer so I did some research. Some of the facts are assumptions drawn from the knowledge I have of SQL. So anyone, feel free to correct me. There are two things in this question:

1) First is the interrupted insert query. Room is just an abstraction layer over SQLite, and SQLite follows ACID properties. In your case what concerns us is the Atomicity:

Atomicity means all the operations of transaction must be finished or rollback the complete transaction in case of any failures.

2) Second is the interrupted Thread, but if a Thread is interrupted using Thread.interrupt() it will not affect the query as mentioned here and to some extent here. Therefore the first insert will be executed and only then the Thread will be interrupted.

Thinking logically, if for some reason the query is interrupted somehow (I'm not sure how it translates to Java), then the Atomicity of SQLite will make sure to abort the transaction.

Suleyman
  • 2,765
  • 2
  • 18
  • 31