Bug 843029 - Add more retries to ensureDatabaseIsNotLocked r=blassey r=lucasr a=lsblakk
authorMark Finkle <mfinkle@mozilla.com>
Wed, 20 Feb 2013 13:32:35 -0500
changeset 127565 cf2437d4159eee6d24b5eb7897ad5b06c5141a88
parent 127562 016db78208562c8b899b86cb571079a343aae153
child 127566 4e8de70b500e884169eddb78322196d1e4afb348
push id2161
push usermfinkle@mozilla.com
push dateWed, 20 Feb 2013 19:36:46 +0000
treeherdermozilla-beta@cf2437d4159e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey, lucasr, lsblakk
bugs843029
milestone20.0
Bug 843029 - Add more retries to ensureDatabaseIsNotLocked r=blassey r=lucasr a=lsblakk
mobile/android/base/db/DBUtils.java
--- a/mobile/android/base/db/DBUtils.java
+++ b/mobile/android/base/db/DBUtils.java
@@ -6,16 +6,18 @@ package org.mozilla.gecko.db;
 
 import org.mozilla.gecko.GeckoAppShell;
 
 import android.content.ContentValues;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.text.TextUtils;
 import android.util.Log;
 
+import java.io.IOException;
+
 public class DBUtils {
     private static final String LOGTAG = "GeckoDBUtils";
 
     public static final String qualifyColumn(String table, String column) {
         return table + "." + column;
     }
 
     // This is available in Android >= 11. Implemented locally to be
@@ -59,24 +61,25 @@ public class DBUtils {
         }
 
         if (!aValues.containsKey(aNewKey)) {
             aValues.put(aNewKey, value);
         }
     }
 
     public static void ensureDatabaseIsNotLocked(SQLiteOpenHelper dbHelper, String databasePath) {
-        try {
-            dbHelper.getWritableDatabase();
-        } catch (Exception e) {
-            Log.d(LOGTAG, "Database is locked, trying to kill any zombie processes: " + databasePath);
-
-            GeckoAppShell.killAnyZombies();
-
-            // This call should not throw if the forced unlocking
-            // actually fixed the situation.
-            dbHelper.getWritableDatabase();
-
-            // TODO: maybe check if the database is still locked and let the
-            // user know that the device needs rebooting?
+        for (int retries = 0; retries < 5; retries++) {
+            try {
+                // Try a simple test and exit the loop
+                dbHelper.getWritableDatabase();
+                return;
+            } catch (Exception e) {
+                // Things could get very bad if we don't find a way to unlock the DB
+                Log.d(LOGTAG, "Database is locked, trying to kill any zombie processes: " + databasePath);
+                GeckoAppShell.killAnyZombies();
+                try {
+                    Thread.sleep(retries * 100);
+                } catch (InterruptedException ie) { }
+            }
         }
+        Log.d(LOGTAG, "Failed to unlock database");
     }
 }