Android Database Locked

Android Database Locked

I think you have forgotten to close the database, or another thread is writing to the database when you are trying to write to it. SQLite locks the database when it is writing to it to avoid corruption if another entity tries to write to the same database at the same time. Android, will only show a error in log cat, and the query you supplied will be just forgotten...

So, I recommend:

  • You only access the database from one SQLOpenHelper
  • You make sure you close all instances of database helpers once you have finished with them
  • You make sure you always end transactions with endTransaction() also if you do not set them successful (i.e. if you want to roll 'em back), in case you use transactions
  • You could try using OrmLite, I've not used it, but I've heard others here rave about it.

Android threading and database locking

I solved this same exception just by making sure all my database opens have closes, and (more importantly) to assure this, making the scope of each database instance local ONLY to the method that needs it. ContentProvider is a good, safe class to use when accessing a db from multiple threads, but also make sure you're using good db practices:

  • Keep db instances local (no SQLiteDatabase class members!)
  • call close() on the db in the same method in which it's opened
  • call close() on the cursors you get from the db
  • listen to LogCat for any complaints that SQLiteDatabse might have

SQLite database is locked?

I think you forget to close the connection to database, and invoke the method endTransaction().

Database is locked - how to fix?

SQLiteException

A SQLite exception that indicates there was an error with SQL parsing
or execution.

Exception java.lang.Throwable: android.database.sqlite.SQLiteException: no such table: ProductsDrafts (code 1): , while compiling: select * from ProductsDrafts

Make sure ProductsDrafts is Present in Database.

FYI

If you add table after publish, then add onUpgrade()

Called when the database needs to be upgraded. The implementation
should use this method to drop tables, add tables, or do anything else
it needs to upgrade to the new schema version.

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(newVersion>oldVersion)

// ALTAR TABLE STATEMENT
// DROP TABLE STATEMENT

}

database lock error sqlite android

Try declaring a reentrantlock in your database helper and lock and unlock everywhere you access your database. This should allow threads to wait for database availability and avoid a crash/error.
e.g. In Database Helper:

    public static Lock l = new ReentrantLock(false);

Everywhere you access the database:

    l.lock();
try
{
//do database editing
}
finally
{
l.unlock();
}

However as previously mentioned, if you try to write to the database more times per second than your database edit can actually complete you will end up with a long backlog of database writes. My suggestion would be to use a timestamp comparrison everytime the trigger for your database writing occurs and skip writing to the database unless it has been, for example, half a second since you last wrote that information to the database.



Related Topics



Leave a reply



Submit