mgolokhov / dodroid

May the knowledge be with you!
MIT License
20 stars 12 forks source link

Race condition in the DB #60

Closed mgolokhov closed 8 years ago

mgolokhov commented 8 years ago

One activity (code) writing to the DB by calling getContentResolver().applyBatch(operations), which should be atomic.

Second activity (code) reading from the DB with the help of Cursor Loader, and sometimes gets old data (race condition).

D/NSA:QuizProvider:138: update db: selected=false _id = 4
D/NSA:QuizProvider:138: update db: selected=false _id = 5
D/NSA:QuizProvider:138: update db: selected=false _id = 26
D/NSA:QuizProvider:138: update db: selected=false _id = 19
D/NSA:QuizProvider:138: update db: selected=false _id = 28
D/NSA:QuizProvider:138: update db: selected=false _id = 10
D/NSA:QuizProvider:138: update db: selected=false _id = 12
D/NSA:QuizProvider:138: update db: selected=false _id = 15
D/NSA:QuizProvider:138: update db: selected=false _id = 18
D/NSA:QuizProvider:138: update db: selected=false _id = 25
D/NSA:QuizProvider:138: update db: selected=false _id = 16
D/NSA:QuizProvider:138: update db: selected=false _id = 17
D/NSA:QuizProvider:138: update db: selected=false _id = 8
D/NSA:QuizProvider:138: update db: selected=false _id = 3
D/NSA:QuizProvider:138: update db: selected=false _id = 20
D/NSA:QuizProvider:138: update db: selected=false _id = 29
D/NSA:QuizProvider:138: update db: selected=false _id = 24
D/NSA:QuizProvider:138: update db: selected=false _id = 23
D/NSA:QuizProvider:138: update db: selected=false _id = 30
D/NSA:QuestionsPagerAdapter:42: counter: 0
D/NSA:QuizProvider:103: query db: content://doit.study.droid/question/rand/280 null null
D/NSA:QuizProvider:138: update db: selected=false _id = 6
D/NSA:QuestionsPagerAdapter:42: counter: 0
D/NSA:QuestionsPagerAdapter:42: counter: 0
D/NSA:QuizProvider:138: update db: selected=false _id = 1
D/NSA:QuizProvider:138: update db: selected=false _id = 14
D/NSA:QuestionsPagerAdapter:42: counter: 0
D/NSA:QuizProvider:138: update db: selected=false _id = 7
D/NSA:QuizProvider:138: update db: selected=false _id = 27
D/NSA:QuestionsPagerAdapter:42: counter: 0
D/NSA:QuestionsActivity:84: load finished: 154982045
D/NSA:QuestionsPagerAdapter:66: swap cursor, cnt: 104
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:35: instantiateItem, pos=0
D/NSA:QuestionsPagerAdapter:25: getItem, pos=0
D/NSA:QuestionsPagerAdapter:35: instantiateItem, pos=1
D/NSA:QuestionsPagerAdapter:25: getItem, pos=1
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuizProvider:138: update db: selected=false _id = 2
D/NSA:QuizProvider:138: update db: selected=false _id = 11
D/NSA:QuizProvider:138: update db: selected=false _id = 22
D/NSA:QuizProvider:138: update db: selected=false _id = 9
D/NSA:QuizProvider:138: update db: selected=false _id = 31
D/NSA:QuizProvider:138: update db: selected=false _id = 21
D/NSA:QuizProvider:138: update db: selected=false _id = 32
D/NSA:QuizProvider:138: update db: selected=false _id = 13
D/NSA:QuizProvider:138: update db: selected=false _id = 4
W/FragmentManager: moveToState: Fragment state for QuestionFragment{5c25e01 #0 id=0x7f0f00e5} not updated inline; expected state 3 found 2
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:52: title pos: 0, questions: tags: [User Interfaces]
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:52: title pos: 1, questions: tags: [User Interfaces]
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuizProvider:103: query db: content://doit.study.droid/tag null null
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuestionsPagerAdapter:42: counter: 104
D/NSA:QuizProvider:138: update db: selected=false _id = 5
D/NSA:QuizProvider:138: update db: selected=false _id = 26
D/NSA:QuizProvider:103: query db: content://doit.study.droid/tag null null
D/NSA:QuizProvider:138: update db: selected=false _id = 19
D/NSA:QuizProvider:138: update db: selected=false _id = 28
D/NSA:QuizProvider:138: update db: selected=false _id = 10
D/NSA:QuizProvider:138: update db: selected=false _id = 12
D/NSA:QuizProvider:138: update db: selected=false _id = 15
D/NSA:QuizProvider:138: update db: selected=false _id = 18
D/NSA:QuizProvider:138: update db: selected=false _id = 25
D/NSA:QuizProvider:138: update db: selected=false _id = 16
D/NSA:QuizProvider:138: update db: selected=false _id = 17
D/NSA:QuizProvider:138: update db: selected=false _id = 8
D/NSA:QuizProvider:138: update db: selected=false _id = 3
D/NSA:QuizProvider:138: update db: selected=false _id = 20
D/NSA:QuizProvider:138: update db: selected=false _id = 29
D/NSA:QuizProvider:138: update db: selected=false _id = 24
D/NSA:QuizProvider:138: update db: selected=false _id = 23
D/NSA:QuizProvider:138: update db: selected=false _id = 30
D/NSA:QuizProvider:138: update db: selected=false _id = 6
D/NSA:QuizProvider:138: update db: selected=false _id = 1
D/NSA:QuizProvider:138: update db: selected=false _id = 14
D/NSA:QuizProvider:138: update db: selected=false _id = 7
D/NSA:QuizProvider:138: update db: selected=false _id = 27
D/NSA:QuizProvider:138: update db: selected=false _id = 2
D/NSA:QuizProvider:138: update db: selected=false _id = 11
D/NSA:QuizProvider:138: update db: selected=false _id = 22
D/NSA:QuizProvider:138: update db: selected=false _id = 9
D/NSA:QuizProvider:138: update db: selected=false _id = 31
D/NSA:QuizProvider:138: update db: selected=false _id = 21
D/NSA:QuizProvider:138: update db: selected=false _id = 32
D/NSA:QuizProvider:138: update db: selected=false _id = 13
D/NSA:QuizProvider:103: query db: content://doit.study.droid/question/rand/280 null null
D/NSA:QuestionsActivity:84: load finished: 19434496
D/NSA:QuestionsPagerAdapter:66: swap cursor, cnt: 0
D/NSA:QuestionsPagerAdapter:42: counter: 0
D/NSA:QuestionsPagerAdapter:42: counter: 0

Some resources:

http://developer.android.com/guide/topics/providers/content-provider-basics.html#Batch

http://www.androiddesignpatterns.com/2012/10/sqlite-contentprovider-thread-safety.html

http://stackoverflow.com/questions/8104832/sqlite-simultaneous-reading-and-writing

http://kagii-blog.tumblr.com/post/6828016869/android-sqlite-locking