10
I have a version of the App in production where it is allowed to mark a message as favorite, only that this totally random error happens provoking corruption of the database and resulting in application crash.
I know I realize operations a little risky according to Sqlite’s own staff.
The procedure is as follows:
- starts the upgrade procedure
- copies the user’s database to a temporary file
- copies the new database to the device
- attaches the old base with the new
- necessary data (such as favorite messages)
- detaches the temporary base
- excludes the temporary basis
This procedure is done on thousands of devices, mostly everything goes well, but it started to become frequent cases like this.
Does anyone know how to minimize that?
ERROR:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.redrails.torpedos/br.com.redrails.torpedos.MainActivity}: android.database.sqlite.SQLiteDatabaseCorruptException: error code 11: database disk image is malformed
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteDatabaseCorruptException: error code 11: database disk image is malformed
at android.database.sqlite.SQLiteStatement.native_1x1_long(Native Method)
at android.database.sqlite.SQLiteStatement.simpleQueryForLong(SQLiteStatement.java:138)
at android.database.DatabaseUtils.longForQuery(DatabaseUtils.java:791)
at android.database.DatabaseUtils.longForQuery(DatabaseUtils.java:779)
at br.com.redrails.torpedos.MensagemDAO.reloadQuantidadeTotal(MensagemDAO.java:144)
at br.com.redrails.torpedos.MensagemDAO.getQuantidadeTotal(MensagemDAO.java:149)
at br.com.redrails.torpedos.MainActivity.onCreate(MainActivity.java:85)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
... 11 more
Maybe block access to the database before doing these operations?
– LaPingvino
Do you think blocking the database will solve? and other I need to perform operations in the database during the process.
– Luiz Carvalho
Maybe put operations in a queue, block during copying, then unlock and continue with the queue... write to a file that at this point is copied gives problems qq way, pq n is atomic.
– LaPingvino
By chance I still read code today that confirmed me that in Sqlite this is important... (and suggested further that sometimes blocking the program side may not even be enough...)
– LaPingvino
Know where I can get more explanations on how to do this @Lapingvino ??
– Luiz Carvalho
http://www.mimec.org/node/306 this may help to block at the same sqlite level, otherwise you can also block in Java (see threading).
– LaPingvino
I’ll try it, thanks @Lapingvino :)
– Luiz Carvalho
It would be great to have an example of the operations you are doing. Are you trying to mess with the base while copying? Handles different threads at the same time? ?
– Alexandre Marcondes
I modified to try to minimize everything, I will add the example of the code @Alexandrer.L.eMarcondes
– Luiz Carvalho
One thing I notice developers forget about on Android devices is that these usually have solid state secondary memories like Flash, and that these have a number of restricted writing cycles (Windows Embbeded itself had option to reduce the number of scripts), so the more you can work on the primary memory (RAM) before writing on the secondary better.
– Diego C Nascimento