2

The response received from web service I am writing it to the database in background thread. At the same time I am performing other database operation because that application facing deadlock as explained here. As mentioned in the link there are two possible approaches

  • Don't make writer thread to join reader thread.As mentioned here I am trying to use java thread in a such way that deadlock should not occur while performing database operation. I have used synchronized block to achieve this but it didn't helped.

  • I am not using second approach because it risk to lose data.

How should I resolve this issue?

Community
  • 1
  • 1
Vivek
  • 4,170
  • 6
  • 36
  • 50
  • Perhaps it's something to do with Android, but the linked article doesn't make much sense. If there's only one connection and both threads share it, then the reader thread should be using the writer thread's transaction, and thus shouldn't block. – Colonel Thirty Two Oct 15 '14 at 14:25

1 Answers1

0

The issue is because of the I am using same database object through my application. Although I am using synchronized methods to access the database and performing database operations the database object was the same.

Suppose I have a three synchronized methods getdatabase(), writedb(), readdb() which return database object, write in to the database and read from the database respectively. I have two thread T1 and T2.

  1. In thread T1 call getdabase()
  2. Using thread T1 start writing into the database.
  3. Using thread T2 call getdabase()(Access will be grated since T1 is not using this method)
  4. Using thread T2 call readdb(). --Deadlock

To Avoid this problem I have used Application variable(static variable in Application class) which will grant access to database only if no other thread is using the database.

luizfzs
  • 1,328
  • 2
  • 18
  • 34
Vivek
  • 4,170
  • 6
  • 36
  • 50
  • I'm experiencing the deadlock problem aswell. As for what I've already read, the methods for getting writtable/readable database returns the same object e.g. you can still write to the database even though it is the object returned from `getReadableDb()`. Maybe that's the case. – luizfzs Mar 28 '16 at 12:22