Add retry logic to all DB query exec tizen_2.1 accepted/tizen/20130520.101201 submit/tizen/20130517.045116 submit/tizen/20130619.101242 submit/tizen_2.1/20130516.133656
authorDonghee Ye <donghee.ye@samsung.com>
Wed, 15 May 2013 15:27:18 +0000 (00:27 +0900)
committerDonghee Ye <donghee.ye@samsung.com>
Wed, 15 May 2013 16:16:54 +0000 (01:16 +0900)
Change-Id: I450aa2f1e5b368da634753ca046a542f13c4e884

CMakeLists.txt
contacts-service2.manifest
native/ctsvc_sqlite.c
packaging/contacts-service.spec

index b26c0c4..90c0343 100755 (executable)
@@ -13,7 +13,7 @@ SET(PREFIX ${CMAKE_INSTALL_PREFIX})
 SET(EXEC_PREFIX "\${prefix}")
 SET(INCLUDEDIR "\${prefix}/${DEST_INCLUDE_DIR}")
 SET(VERSION_MAJOR 0)
-SET(VERSION "${VERSION_MAJOR}.9.84.1")
+SET(VERSION "${VERSION_MAJOR}.9.84.2")
 
 EXECUTE_PROCESS(COMMAND build-util/generator.sh)
 
index 7afaf3a..1f55882 100644 (file)
@@ -14,7 +14,7 @@
                </request>
        </define>
        <assign>
-               <filesystem path="/usr/lib/libcontacts-service2.so.0.9.84.1" label="_"/>
+               <filesystem path="/usr/lib/libcontacts-service2.so.0.9.84.2" label="_"/>
                <filesystem path="/usr/lib/libcontacts-service2.so.0" label="_"/>
                <filesystem path="/opt/usr/dbspace/.contacts-svc.db" label="contacts-service::db"/>
                <filesystem path="/opt/usr/dbspace/.contacts-svc.db-journal" label="contacts-service::db"/>
index e511911..fc912f8 100755 (executable)
@@ -38,6 +38,8 @@
 #include "ctsvc_phonelog.h"
 #include "ctsvc_person.h"
 
+#define CTSVC_QUERY_RETRY_TIME 2
+
 static __thread sqlite3 *ctsvc_db = NULL;
 
 static inline int __ctsvc_db_busyhandler(void *pData, int count)
@@ -135,14 +137,52 @@ int cts_db_get_next_id(const char *table) {
 
 int ctsvc_query_get_first_int_result(const char *query, int *result) {
        int ret;
+       struct timeval from, now, diff;
+       bool retry = false;
        cts_stmt stmt = NULL;
        RETVM_IF(NULL == ctsvc_db, CONTACTS_ERROR_DB /*CTS_ERR_DB_NOT_OPENED*/, "DB error : Database is not opended");
 
-       ret = sqlite3_prepare_v2(ctsvc_db, query, strlen(query), &stmt, NULL);
-       RETVM_IF(SQLITE_OK != ret, CONTACTS_ERROR_DB,
-                       "DB error : sqlite3_prepare_v2(%s) Failed(%s)", query, sqlite3_errmsg(ctsvc_db));
+       gettimeofday(&from, NULL);
+       do {
+               ret = sqlite3_prepare_v2(ctsvc_db, query, strlen(query), &stmt, NULL);
+
+               if (ret != SQLITE_OK)
+                       CTS_ERR("DB error : sqlite3_prepare_v2() Failed(%d, %s)", ret, sqlite3_errmsg(ctsvc_db));
+
+               if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
+                       gettimeofday(&now, NULL);
+                       timersub(&now, &from, &diff);
+                       retry = (diff.tv_sec < CTSVC_QUERY_RETRY_TIME)? true:false;
+                       if (retry)
+                               usleep(50*1000); // 50 ms
+               } else
+                       retry = false;
+       }while(retry);
+
+       if (SQLITE_OK != ret) {
+               CTS_ERR("DB error : sqlite3_prepare_v2(%s) Failed(%s)", query, sqlite3_errmsg(ctsvc_db));
+               return CONTACTS_ERROR_DB;
+       }
+
+       retry = false;
+       gettimeofday(&from, NULL);
+       do {
+               ret = sqlite3_step(stmt);
+
+               if (ret != SQLITE_ROW)
+                       CTS_ERR("DB error : sqlite3_step() Failed(%d, %s)", ret, sqlite3_errmsg(ctsvc_db));
+
+               if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
+                       gettimeofday(&now, NULL);
+                       timersub(&now, &from, &diff);
+                       retry = (diff.tv_sec < CTSVC_QUERY_RETRY_TIME)? true:false;
+                       if (retry)
+                               usleep(50*1000); // 50 ms
+               }
+               else
+                       retry = false;
+       }while(retry);
 
-       ret = sqlite3_step(stmt);
        if (SQLITE_ROW != ret) {
                sqlite3_finalize(stmt);
                CTS_DBG("query : %s", query);
@@ -163,12 +203,31 @@ int ctsvc_query_get_first_int_result(const char *query, int *result) {
 
 int ctsvc_query_exec(const char *query) {
        int ret;
+       struct timeval from, now, diff;
+       bool retry = false;
        char *err_msg = NULL;
 
        RETVM_IF(NULL == ctsvc_db, CONTACTS_ERROR_DB /*CTS_ERR_DB_NOT_OPENED*/, "DB error : Database is not opended");
        CTS_DBG("query : %s", query);
 
-       ret = sqlite3_exec(ctsvc_db, query, NULL, NULL, &err_msg);
+       gettimeofday(&from, NULL);
+       do {
+               ret = sqlite3_exec(ctsvc_db, query, NULL, NULL, &err_msg);
+
+               if (ret != SQLITE_OK)
+                       CTS_ERR("DB error : sqlite3_exec() Failed(%d, %s)", ret, sqlite3_errmsg(ctsvc_db));
+
+               if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
+                       gettimeofday(&now, NULL);
+                       timersub(&now, &from, &diff);
+                       retry = (diff.tv_sec < CTSVC_QUERY_RETRY_TIME)? true:false;
+                       if (retry)
+                               usleep(50*1000); // 50 ms
+               }
+               else
+                       retry = false;
+       } while(retry);
+
        if (SQLITE_OK != ret) {
                CTS_ERR("sqlite3_exec(%s) Failed(%d, %s)", query, ret, err_msg);
                sqlite3_free(err_msg);
@@ -207,7 +266,7 @@ cts_stmt cts_query_prepare(char *query) {
                if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
                        gettimeofday(&now, NULL);
                        timersub(&now, &from, &diff);
-                       retry = (diff.tv_sec < 2)? true:false; // retry it during 2 second
+                       retry = (diff.tv_sec < CTSVC_QUERY_RETRY_TIME)? true:false;
                        if (retry)
                                usleep(50*1000); // 50 ms
                } else
@@ -251,7 +310,7 @@ int cts_stmt_step(cts_stmt stmt) {
                if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
                        gettimeofday(&now, NULL);
                        timersub(&now, &from, &diff);
-                       retry = (diff.tv_sec < 1)? true:false; // retry it during 1 second
+                       retry = (diff.tv_sec < CTSVC_QUERY_RETRY_TIME)? true:false;
                        if (retry)
                                usleep(50*1000); // 50 ms
                }
index 06bc589..7ec2c5e 100644 (file)
@@ -1,6 +1,6 @@
 Name:       contacts-service
 Summary:    Contacts Service
-Version: 0.9.84.1
+Version: 0.9.84.2
 Release:    1
 Group:      TO_BE/FILLED_IN
 License:    Apache-2.0