2

First of all, the database corruption is INTENTIONAL, it is a test for a invalid database file upload. My problem is that the exception (SQLiteDatabaseCorruptException) appears inside the DAO autogenerated java code:

at com.cormorant.fishingtracker.database.DBDao_Impl$234.compute(DBDao_Impl.java:10024)

So I can't try-catch it! It occurs inside AsyncTask but the exception was not captured! I have tried:

protected void openDatabase(OnOpenDatabaseResult onOpenDatabaseResult) {
        dataRepository.openDatabase(getApplication());
        try {
            dataRepository.getRecordsCount(null, new DataRepository.AsyncResponse<Long>() {
                @Override
                public void processFinish(Long output) {
                    onOpenDatabaseResult.onResult(output!=null);
                }
            });
        } catch (Exception exception) {
            exception.printStackTrace();
            onOpenDatabaseResult.onResult(false);
        }
    }

And also inside the AsyncTask:

public class getRecordsCountAsyncTask extends executeOutAsyncTask<Long> {

        getRecordsCountAsyncTask(DBDao dao, Class<? extends Base> klass, AsyncResponse<Long> delegate) {
            super(dao, klass, delegate);
        }

        @Override
        protected Long newDoInBackground() {
            if (mClass== null) {
                Long ret;
                try {
                    ret = mAsyncTaskDao.countTables();
                } catch (Exception exception) {
                    ret =null;
                    exception.printStackTrace();
                }
                return ret;
            } 
...

How to detect the corrupted file when opening the database (code below)?

    public static DBRoom getDatabase(final Context context) {
        context_ = context;
        if (INSTANCE == null) {
            synchronized (DBRoom.class) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        DBRoom.class, dbFileName)
                        .addMigrations(MIGRATION_1_2,MIGRATION_2_3,MIGRATION_3_4,MIGRATION_4_5)
                        .fallbackToDestructiveMigration() // Wipes and rebuilds instead of migrating if no Migration object.
                        .addCallback(sRoomDatabaseCallback)
                        .build();
            }
        }
        return INSTANCE;
    }

The entire log:


04-28 11:20:44.218 12854-12879/com.cormorant.fishingtracker E/SQLiteLog: (26) file is encrypted or is not a database
04-28 11:20:44.219 12854-12879/com.cormorant.fishingtracker E/SQLiteLog: (26) file is encrypted or is not a database
04-28 11:20:44.219 12854-12879/com.cormorant.fishingtracker E/SQLiteDatabase: Failed to open database '/data/user/0/com.cormorant.fishingtracker/databases/fishingtracker'.
    android.database.sqlite.SQLiteDatabaseCorruptException: file is encrypted or is not a database (code 26): , while compiling: PRAGMA journal_mode
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
        at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:632)
        at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:318)
        at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:289)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:213)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:794)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
        at androidx.room.RoomDatabase.query(RoomDatabase.java:238)
        at com.cormorant.fishingtracker.database.DBDao_Impl$209.compute(DBDao_Impl.java:8046)
        at com.cormorant.fishingtracker.database.DBDao_Impl$209.compute(DBDao_Impl.java:8032)
        at androidx.lifecycle.ComputableLiveData$2.run(ComputableLiveData.java:101)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
04-28 11:20:44.222 12854-12881/com.cormorant.fishingtracker E/SQLiteLog: (26) file is encrypted or is not a database
04-28 11:20:44.223 12854-12881/com.cormorant.fishingtracker E/SQLiteLog: (26) file is encrypted or is not a database
    
    
    --------- beginning of crash
04-28 11:20:44.224 12854-12879/com.cormorant.fishingtracker E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0
    Process: com.cormorant.fishingtracker, PID: 12854
    android.database.sqlite.SQLiteDatabaseCorruptException: file is encrypted or is not a database (code 26): , while compiling: PRAGMA journal_mode
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
        at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:632)
        at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:318)
        at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:289)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:213)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:794)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
        at androidx.room.RoomDatabase.query(RoomDatabase.java:238)
        at com.cormorant.fishingtracker.database.DBDao_Impl$209.compute(DBDao_Impl.java:8046)
        at com.cormorant.fishingtracker.database.DBDao_Impl$209.compute(DBDao_Impl.java:8032)
        at androidx.lifecycle.ComputableLiveData$2.run(ComputableLiveData.java:101)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
04-28 11:20:44.228 12854-12881/? E/SQLiteDatabase: Failed to open database '/data/user/0/com.cormorant.fishingtracker/databases/fishingtracker'.
    android.database.sqlite.SQLiteDatabaseCorruptException: file is encrypted or is not a database (code 26): , while compiling: PRAGMA journal_mode
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
        at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:632)
        at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:318)
        at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:289)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:213)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:794)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
        at androidx.room.RoomDatabase.query(RoomDatabase.java:238)
        at com.cormorant.fishingtracker.database.DBDao_Impl$217.compute(DBDao_Impl.java:8772)
        at com.cormorant.fishingtracker.database.DBDao_Impl$217.compute(DBDao_Impl.java:8758)
        at androidx.lifecycle.ComputableLiveData$2.run(ComputableLiveData.java:101)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
04-28 11:20:44.229 12854-12880/? E/SQLiteLog: (26) file is encrypted or is not a database
04-28 11:20:44.229 12854-12880/? E/SQLiteLog: (26) file is encrypted or is not a database
04-28 11:20:44.235 12854-12881/? I/Process: Sending signal. PID: 12854 SIG: 9
sgm
  • 196
  • 3
  • 14
  • Could you post the entire stacktrace of your error? It might help to find where that method in DBDao_Impl is called from. – cd1 Apr 28 '21 at 14:14
  • Hi cd1, the entire stacktrace was added. The DBDao_Impl is called when a observer is added to a LiveData table of the DBDao. – sgm Apr 28 '21 at 14:29
  • Would checking for the corrupt database before it is accessed by room be suitable? – MikeT Apr 29 '21 at 01:31
  • Hi MikeT, indeed what I want is that, but how do that? I need the room apply Migrations too. – sgm Apr 29 '21 at 08:45

0 Answers0