简体   繁体   中英

DB lock issue in android

The following error occured while downloading and extraction of image sets and then unfortunately closed popup appear. please help me

05-26 13:01:54.637: E/SQLiteLog(15753): (5) database is locked
05-26 13:01:54.657: E/SQLiteDatabase(15753): Failed to open database '/data/data/org.com.clipToonz/databases/clipToonz.db'.
05-26 13:01:54.657: E/SQLiteDatabase(15753): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:634)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:320)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:294)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at org.com.clipToonz.dbUtilities.ImageSetHandler.getImageSetIDByNameAndParentID(ImageSetHandler.java:187)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at org.apache.cordova.plugin.ExtractPlugin$unZipTask.doInBackground(ExtractPlugin.java:145)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at org.apache.cordova.plugin.ExtractPlugin$unZipTask.doInBackground(ExtractPlugin.java:1)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-26 13:01:54.657: E/SQLiteDatabase(15753):     at java.lang.Thread.run(Thread.java:841)
05-26 13:01:54.657: W/dalvikvm(15753): threadid=26: thread exiting with uncaught exception (group=0x41760ba8)

The following code used for insert image details

     DB_PATH=Constants.KEY_DBPATH;      
     String myPath = DB_PATH;
     db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);      
     ContentValues values = new ContentValues();
     values.put(KEY_NAME,imageset.getName());
     values.put(KEY_PARENTID,imageset.getParentID());
     values.put(KEY_ISDIRECTORY ,imageset.getIsDirectory());
     values.put(KEY_CATEGORYID, imageset.getCategoryID());
     values.put(KEY_IMAGESETNAME, imageset.getImagesetName());
     values.put(KEY_PRIMARYNAME, imageset.getPrimaryName());

    // String query = "INSERT INTO " +imageset.getTableName()+ "("+KEY_IMAGESETNAME+","+KEY_NAME+","+KEY_PARENTID+","+KEY_ISDIRECTORY+","+KEY_CATEGORYID+","+KEY_PRIMARYNAME+") VALUES('"+imageset.getImagesetName()+"','"+imageset.getName()+"',"+imageset.getParentID()+","+imageset.getIsDirectory()+","+imageset.getCategoryID()+",'"+imageset.getPrimaryName()+"')";
     //System.out.println(imageset.getTableName());
    // Inserting Row
     //db.execSQL(query);
     db.insert(imageset.getTableName(), null, values);
     db.close(); // Closing database connection

and to detect existing image details

int returnValue=0;
    //String num;
            DB_PATH=Constants.KEY_DBPATH;
            String name=imagesetp.getName();
            int categoryid = imagesetp.getCategoryID();             
            String myPath = DB_PATH;
         db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);
        String selectQuery="SELECT * FROM "+imagesetp.getTableName()+" WHERE "+KEY_NAME+"='"+String.valueOf(name)+"' AND parentid="+parentID+" AND categoryid ="+categoryid;
        System.out.println("selectQuery " + selectQuery);

        Cursor cursor = db.rawQuery(selectQuery,null);
        // try{
         if (cursor != null && cursor.getCount()!=0)             
             cursor.moveToFirst();           
            returnValue=Integer.parseInt(cursor.getString(0));

    // }

    // finally{
         //cursor.close();
        // db.close();
    // }
         cursor.close();
         db.close();
    return returnValue;

As I am not able to write comments yet (50 reputation needed), I write here - but more than that this should be your answer:

You try to open the database while it is still in usage at another line of code. To avoid inconsistence the database will automatically be locked for every other access, but this one. At first you have to allocate this resource (with db.close() - which seems to be missing somewhere at your code) before it can be used somewhere else.

Something more:

SQLiteDatabase db = new DataBaseHelper(context).getReadableDatabase();

and

SQLiteDatabase db = new DataBaseHelper(context).getWritableDatabase();

is the more common way to get your database.


edit:

I guess I found your bug. Please format your code next time - it will help you!

if (cursor != null && cursor.getCount()!=0)             
             cursor.moveToFirst();           
            returnValue=Integer.parseInt(cursor.getString(0));

This is where your database will not be closed before leaving the method! Your function will be left after return. Place a db.close() before this return.

Best regards Vince

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM