Add widget db busy handler 04/123904/2
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 7 Apr 2017 10:26:21 +0000 (19:26 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 7 Apr 2017 10:31:49 +0000 (19:31 +0900)
While some process is writing on the widget db, the widget parser plugin fails
query with SQLITE_BUSY error. This patch is to fix this issue.

Change-Id: I4ed4782656bf6585c3bf6d7389405e23010dcbd2
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
parser/widget_plugin_parser_internal.c

index dc424eb..0ac5335 100644 (file)
@@ -26,6 +26,9 @@
 
 #include "widget_plugin_parser_internal.h"
 
+#define BUSY_WAITING_USEC 50000 /* 0.05 sec */
+#define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
+
 void _free_support_size(gpointer data)
 {
        struct support_size *size = (struct support_size *)data;
@@ -104,6 +107,17 @@ static const char *_get_db_path(uid_t uid)
        return path;
 }
 
+static int __db_busy_handler(void *data, int count)
+{
+       if (count < BUSY_WAITING_MAX) {
+               usleep(BUSY_WAITING_USEC);
+               return 1;
+       }
+
+       /* sqlite3_prepare_v2 will return SQLITE_BUSY */
+       return 0;
+}
+
 sqlite3 *_open_db(uid_t uid, bool readonly)
 {
        int ret;
@@ -125,6 +139,14 @@ sqlite3 *_open_db(uid_t uid, bool readonly)
                return NULL;
        }
 
+       ret = sqlite3_busy_handler(db, __db_busy_handler, NULL);
+       if (ret != SQLITE_OK) {
+               LOGE("Failed to register busy handler: %s",
+                               sqlite3_errmsg(db));
+               sqlite3_close_v2(db);
+               return NULL;
+       }
+
        /* turn on foreign keys */
        if (sqlite3_exec(db, "PRAGMA foreign_keys = ON", NULL, NULL, NULL)) {
                sqlite3_close_v2(db);