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 132199 5d609a1cf7be5624fdc42f6095bc8b56584ba3b2
parent 132198 a5a524dd41f08ff7be5a855538994eae72017e77
child 132200 10fe9f95dadb094c6ac4170b62d4f25cd1abfe5c
push id2323
push userbbajaj@mozilla.com
push dateMon, 01 Apr 2013 19:47:02 +0000
treeherdermozilla-beta@7712be144d91 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey, lucasr, lsblakk
bugs843029
milestone21.0a2
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");
     }
 }