How to lock database from multiple threads in android?
From the few days, i am facing weird exception that is,
IllegalStateException : database not opened.
Actually I am updating databases from multiple threads so may be I am updating data after closing. So please any one tell me how to apply lock and unlock mechanism for sqlite database.
Here one of database function. Please tell me is there any thing wrong.
if(cBookmark != null && !checkIfBookmarkExists(cBookmark)){
SQLiteDatabase sqLiteDatabase = null;
try{
sqLiteDatabase = mDatabaseHelper.getWritableDatabase();
SQLiteStatement sqLiteStatement = sqLiteDatabase.compileStatement(
"insert into chart_bookmark ( "+
"package_id , chart_id , chart_title , offset_x, offset_y, zoomlevel )"+
"Values( ?,?,?,?,?,?)");
synchronized(sqLiteStatement){
sqLiteStatement.bindString(1, cBookmark.getPackag开发者_如何学编程e_id());
sqLiteStatement.bindString(2,cBookmark.getChart_id());
sqLiteStatement.bindString(3, cBookmark.getChartTitle());
sqLiteStatement.bindString(4, cBookmark.getOffsetX());
sqLiteStatement.bindString(5,cBookmark.getOffsetY());
sqLiteStatement.bindLong(6,cBookmark.getZoomlevel());
Log.v(TAG, ""+cBookmark.getChart_id()+cBookmark.getChartTitle()+
cBookmark.getOffsetX()+cBookmark.getOffsetY());
sqLiteStatement.execute();
sqLiteStatement.close();
}
}finally{
sqLiteDatabase.releaseReference();
}
}
精彩评论