问题
I'm trying to delete a row from my database. When I first delete it for the first time, the row gets removed successfully. After I try to remove another row, I get an error that the db is not open/ is locked. I have posted the logcat and the code.
Also, if I delete a row for the first time, it gets delete and when I close the app and open it back again and delete another row, it works. Only if I continue to delete another row without closing the app, I get the error.
Here's the log:
Caused by: java.lang.IllegalStateException: The database '/data/user/0/com.basusingh.blingoo/databases/wordLayList' is not open.
at android.database.sqlite.SQLiteDatabase.throwIfNotOpenLocked(SQLiteDatabase.java:2943)
at android.database.sqlite.SQLiteDatabase.createSession(SQLiteDatabase.java:564)
at android.database.sqlite.-$$Lambda$RBWjWVyGrOTsQrLCYzJ_G8Uk25Q.get(Unknown Source:2)
at java.lang.ThreadLocal$SuppliedThreadLocal.initialValue(ThreadLocal.java:284)
at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:180)
at java.lang.ThreadLocal.get(ThreadLocal.java:170)
at android.database.sqlite.SQLiteDatabase.getThreadSession(SQLiteDatabase.java:558)
at android.database.sqlite.SQLiteProgram.getSession(SQLiteProgram.java:123)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeUpdateDelete(FrameworkSQLiteStatement.java:46)
at com.basusingh.blingoo.Database.WordLayList.WordLayListDao_Impl.DeleteWordLayList(WordLayListDao_Impl.java:94)
at com.basusingh.blingoo.Activity.WordLayListViewer$1doTask.doInBackground(WordLayListViewer.java:156)
at com.basusingh.blingoo.Activity.WordLayListViewer$1doTask.doInBackground(WordLayListViewer.java:148)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
E/ROOM: Cannot run invalidation tracker. Is the db closed?
android.database.sqlite.SQLiteException: no such table: room_table_modification_log (code 1 SQLITE_ERROR[1]): , while compiling: SELECT * FROM room_table_modification_log WHERE invalidated = 1;
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1229)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:703)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1865)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1840)
at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.query(FrameworkSQLiteDatabase.java:161)
at androidx.room.RoomDatabase.query(RoomDatabase.java:328)
at androidx.room.RoomDatabase.query(RoomDatabase.java:311)
at androidx.room.InvalidationTracker$1.checkUpdatedTable(InvalidationTracker.java:415)
at androidx.room.InvalidationTracker$1.run(InvalidationTracker.java:389)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Here's the DAO code:
@Dao
public interface WordLayListDao {
@Query("DELETE FROM WordLayListItems WHERE uid = :id")
void DeleteWordLayList(int id);
}
And other class,
public class WordLayListDatabaseClient {
private Context mCtx;
private static WordLayListDatabaseClient mInstance;
private WordLayListAppDatabase appDatabase;
private WordLayListDatabaseClient(Context mCtx) {
this.mCtx = mCtx;
appDatabase = Room.databaseBuilder(mCtx, WordLayListAppDatabase.class, "wordLayList").build();
}
public static synchronized WordLayListDatabaseClient getInstance(Context mCtx) {
if (mInstance == null) {
mInstance = new WordLayListDatabaseClient(mCtx);
}
return mInstance;
}
public WordLayListAppDatabase getAppDatabase() {
return appDatabase;
}
}
@Database(entities = {WordLayListItems.class}, version = 1)
public abstract class WordLayListAppDatabase extends RoomDatabase {
public abstract WordLayListDao wordLayListDao();
}
This is how I'm calling the database:
private void deleteLayList(){
class doTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute(){
progressDialog.setTitle("Please wait");
progressDialog.show();
}
@Override
protected String doInBackground(Void... aa){
WordLayListDatabaseClient.getInstance(WordLayListViewer.this).getAppDatabase().wordLayListDao().DeleteWordLayList(o.getID());
return null;
}
@Override
protected void onPostExecute(String var){
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "WordLay List Deleted", Toast.LENGTH_LONG).show();
setResult(RESULT_OK);
finish();
}
}
doTask dt = new doTask();
dt.execute();
}
回答1:
I've solved the issue this way.
Instead of using the singleton class to insert or delete, I'm simply calling this code directly:
WordLayListAppDatabase db = Room.databaseBuilder(getApplicationContext(),
WordLayListAppDatabase.class, "wordLayList").build();
db.wordLayListDao().DeleteWordLayListByObject(o);
Now, I'm using the singleton class to only fetch the data. To update the database, I'm using the code above (The above is always called in Async Task).
来源:https://stackoverflow.com/questions/58970801/room-database-gets-locked-after-a-delete-query