Android - database is locked

I have an application that I can insert data into a SQLite database. There, the service flow runs every sixty seconds, checking whether the condition is true, and raising an alarm if necessary. Think of the calendar app.

I have many actions with reference to their own SQLiteOpenHelper and their own SQLiteDatabase object.

The application worked very well, but one of the main update applications that I decided needed a progress dialog object is another topic, but if you can tell me how I can make the spinner spin, it will be great. However, it is displayed.

But to display it, I needed to put it in a stream.

Having created this thread, I began to load messages "database locked" in the log.

I realized that the problem might have been that I had a task in the background running every sixty seconds - although it only visually took a couple of seconds to complete the update, so I put the main write transactions for updater in the beginTransaction block. This is the only place I used it.

Everything seemed good, but then I began to get more false errors that the database is locked everywhere.

This prompted me to do some tricks, but it seems that after I do the first beginTransaction, any further database changes that I want to make with the database lock message, despite the fact that I call db.close. I even close the helper database object and the cursor just to be safe.

I managed to clear up most of the warnings that cursors are still open, like

E/Database( 678): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 

I could not understand what was blocking the database as everything was closing.

Then I decided to remove the "startTransaction", which stabilized the situation - now I could again move around my program. But I still get some random locking issues.

 E/Database( 1304): Failure 5 (database is locked) on 0x26e5d8 when executing 'INSERT INTO 

Can I find out what a database lock is? I see that he is grumbling about the castle, but not that it is blocking it.

What is the best way to handle simultaneous database updates / reads? I read a lot about synchronized and ContentProviders, but I have to admit that this is a bit for me.

Any pointers to the β€œbest” way to do things?

And can I find out what blocks the database?

Thanks Simon

+4
source share
2 answers

class NAME extends SQLiteOpenHelper

add:

 @Override public void onOpen(SQLiteDatabase db) { super.onOpen(db); if (!db.isReadOnly()) { db.execSQL("PRAGMA foreign_keys=ON;"); Cursor c = db.rawQuery("PRAGMA foreign_keys", null); if (c.moveToFirst()) { int result = c.getInt(0); } if (!c.isClosed()) { c.close(); } } } 
+2
source

You might want to take a look at the latest version of Berkeley DB as an option. It provides a SQLite3-compatible SQL API (so that your application can remain unchanged), and it provides finer granularity of the lock, and therefore better concurrency. Berkeley DB supports multiple simultaneous read and write streams for active access to the database simultaneously. You can find out more about DB Berkeley on our website .

-2
source

Source: https://habr.com/ru/post/1335745/


All Articles