#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)
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);
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);
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
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
}