android / codelab-android-workmanager

Apache License 2.0
544 stars 240 forks source link

SQLiteException #142

Closed liweijieok closed 5 years ago

liweijieok commented 5 years ago

question: I have three process in my project and i call follow code in my application#oncreate,so the code was executed three times。

Configuration configuration = new Configuration.Builder()
                .setExecutor(CacheExecutor.getInstance().getExecutor())
                .build();
        WorkManager.initialize(this, configuration);

at the same time, i config follow code in manifest

<provider
            android:exported="false"
            android:enabled="false"
            android:name="androidx.work.impl.WorkManagerInitializer"
            android:authorities="com.example.github.workmanager-init" />

version:

androidx.work:work-runtime:2.1.0-alpha03

error msg:

com.example.github:pushservice E/CrashReport:android.database.sqlite.SQLiteException: index index_Dependency_work_spec_id already exists (code 1): , while compiling: CREATE  INDEX `index_Dependency_work_spec_id` ON `Dependency` (`work_spec_id`)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
        at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.execSQL(FrameworkSQLiteDatabase.java:242)
        at androidx.work.impl.WorkDatabase_Impl$1.createAllTables(WorkDatabase_Impl.java:53)
        at androidx.room.RoomOpenHelper.onCreate(RoomOpenHelper.java:74)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onCreate(FrameworkSQLiteOpenHelper.java:118)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
        at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:328)
        at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:85)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)

some time has follow error msg:

    Process: com.myapplication:pushservice, PID: 23397
    android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
        at android.database.sqlite.SQLiteConnection.nativeExecute(Native Method)
        at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:556)
        at android.database.sqlite.SQLiteSession.beginTransactionUnchecked(SQLiteSession.java:323)
        at android.database.sqlite.SQLiteSession.beginTransaction(SQLiteSession.java:298)
        at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:534)
        at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:445)
        at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.beginTransaction(FrameworkSQLiteDatabase.java:69)
        at androidx.work.impl.WorkDatabase$1.onOpen(WorkDatabase.java:131)
        at androidx.work.impl.WorkDatabase_Impl$1.onOpen(WorkDatabase_Impl.java:91)
        at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:120)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:142)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:349)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
        at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:328)
        at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:85)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
mcatta commented 5 years ago

Hi, I have the same issue with the version: android.arch.work:work-runtime-ktx:1.0.1

This is my stacktrace:

Fatal Exception: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
       at android.database.sqlite.SQLiteConnection.nativeExecute(SQLiteConnection.java)
       at android.database.sqlite.SQLiteConnection.execute + 564(SQLiteConnection.java:564)
       at android.database.sqlite.SQLiteSession.beginTransactionUnchecked + 323(SQLiteSession.java:323)
       at android.database.sqlite.SQLiteSession.beginTransaction + 298(SQLiteSession.java:298)
       at android.database.sqlite.SQLiteDatabase.beginTransaction + 522(SQLiteDatabase.java:522)
       at android.database.sqlite.SQLiteDatabase.beginTransaction + 430(SQLiteDatabase.java:430)
       at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.beginTransaction + 69(FrameworkSQLiteDatabase.java:69)
       at android.arch.persistence.room.RoomDatabase.beginTransaction + 254(RoomDatabase.java:254)
       at androidx.work.impl.WorkerWrapper.runWorker + 136(WorkerWrapper.java:136)
       at androidx.work.impl.WorkerWrapper.run + 128(WorkerWrapper.java:128)
       at java.util.concurrent.ThreadPoolExecutor.runWorker + 1112(ThreadPoolExecutor.java:1112)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run + 587(ThreadPoolExecutor.java:587)
       at java.lang.Thread.run + 818(Thread.java:818)
pfmaggi commented 5 years ago

Thanks for reporting this. I'm closing this issue because this forum is for issues with the codelab.

WorkManager v2.1.x adds supports for on-demand initialization, @liweijieok I suggest that you take a look at this functionality so that you don't have to call initialize(). This is done automatically the first time that you invoke getInstance(Context). The context is used to retrieve the Application Context to get the custom configuration.

@mcatta We fixed some SQLite exception in the latest v2.1.x releases and we updated the Room version by the library to v2.1 (further helping the stability). I suggest that you give a try with this version if you can (WorkManager v2.x is using androidx).

If you think that this is a problem of the WorkManager library, please open a bug on the WorkManager public issue tracker: https://issuetracker.google.com/issues/new?component=409906&template=1094197

Please reopen if you still think this is an issue with the codelab.