Dev.Sinto
Dev.Sinto

Reputation: 6852

database lock issue in HTC Desire

In my application one service is getting data from server and inserting it to table A.

If I go to particular UI, I need to list data from another table B if background operation is doing it will generate database locked exception. I got two database operation done in parallel each on two different table.

It is working fine in samsung gt15801. But htc desire it will generate database locked error.

HTC desire - insertion process takes 91 seconds.

Samsung gt15801 - insertion process takes 21 seconds.

Upvotes: 3

Views: 5226

Answers (1)

Dharmendra
Dharmendra

Reputation: 33996

Try to use one SqliteDatabaseHelper and make it single instance. After that don't close the SqliteDatabase instance after complete your operations.

You can implement lock on database for that see this

http://www.sqlite.org/lockingv3.html

When you start a database operation which has many concurrent operation then you have to use

database.beginTransaction(); /* for start transaction */

and after completing operation on database you can use

database.setTransactionSuccessful();    
database.endTransaction();

But if you give error in between transaction then don't set database.setTransactionSuccessful(); so that the transaction will be rollback.

Also you can check at the time of error whether currently the database is in transaction or not by database.inTransaction(); if it return true then you are in transaction else you are not in transaction

Also You can check for whether current database has locked or not by calling

database.isDbLockedByCurrentThread();
database.isDbLockedByOtherThreads();

this will return Boolean.

Also you can set whether you want to lock your database if multiple threads are trying to read and write your database at the same time by

database.setLockingEnabled(boolean);

Above deleted methods are deprecated so please do not use.

Upvotes: 14

Related Questions