Permalink
Browse files

try DB lock before open DB to prevent race condition

  • Loading branch information...
1 parent cb0f08b commit f20deaa1caa6075694b949b3fa6612c362b2e1b3 @LeBlue LeBlue committed May 24, 2012
Showing with 42 additions and 0 deletions.
  1. +1 −0 src/common/bks_status.c
  2. +1 −0 src/include/Bks_Status.h
  3. +40 −0 src/model/bks_model_sql.c
View
@@ -10,6 +10,7 @@ const char *BKS_STATUS_STRINGS[] = {
"BKS_MODEL_SALE_ERROR",
"BKS_MODEL_SQLITE_ERROR",
"BKS_MODEL_SQLITE_OPEN_ERROR",
+ "BKS_MODEL_SQLITE_OPEN_RUNNING",
"BKS_MODEL_SQLITE_WRITE_ERROR",
"BKS_MODEL_SQLITE_DATABASE_LOCKED",
"BKS_MODEL_SQLITE_ROW_MISSING",
View
@@ -10,6 +10,7 @@ enum _Bks_Status { BKS_MODEL_USERACCOUNTS_GET,
BKS_MODEL_SALE_ERROR,
BKS_MODEL_SQLITE_ERROR,
BKS_MODEL_SQLITE_OPEN_ERROR,
+ BKS_MODEL_SQLITE_OPEN_RUNNING,
BKS_MODEL_SQLITE_WRITE_ERROR,
BKS_MODEL_SQLITE_DATABASE_LOCKED,
BKS_MODEL_SQLITE_ROW_MISSING,
View
@@ -46,6 +46,13 @@ Bks_Status _bks_model_sql_products_get(Eina_List **list) {
int retval;
Bks_Status error = BKS_MODEL_SQLITE_ERROR;
+ if (!eina_lock_take_try(&ctrl.db_lock)) {
+ error = BKS_MODEL_SQLITE_OPEN_RUNNING;
+ goto _return;
+ } else {
+ eina_lock_release(&ctrl.db_lock);
+ }
+
retval = sqlite3_open_v2(mdl.db_path, &pDb, SQLITE_OPEN_URI | SQLITE_OPEN_READONLY , NULL);
if (retval ) {
error = BKS_MODEL_SQLITE_OPEN_ERROR;
@@ -84,6 +91,7 @@ Bks_Status _bks_model_sql_products_get(Eina_List **list) {
}
_close_db:
sqlite3_close(pDb);
+_return:
return error;
}
@@ -99,6 +107,13 @@ Bks_Status _bks_model_sql_favorite_products_get(Eina_List **list, const unsigned
int image_size;
Bks_Status error = BKS_MODEL_SQLITE_ERROR;
+ if (!eina_lock_take_try(&ctrl.db_lock)) {
+ error = BKS_MODEL_SQLITE_OPEN_RUNNING;
+ goto _return;
+ } else {
+ eina_lock_release(&ctrl.db_lock);
+ }
+
retval = sqlite3_open_v2(mdl.db_path, &pDb, SQLITE_OPEN_URI | SQLITE_OPEN_READONLY , NULL);
if (retval ) {
error = BKS_MODEL_SQLITE_OPEN_ERROR;
@@ -150,6 +165,7 @@ Bks_Status _bks_model_sql_favorite_products_get(Eina_List **list, const unsigned
}
_close_db:
sqlite3_close(pDb);
+_return:
return error;
}
@@ -163,6 +179,13 @@ Bks_Status _bks_model_sql_user_accounts_get(Eina_List **list) {
int retval;
Bks_Status error = BKS_MODEL_SQLITE_ERROR;
+ if (!eina_lock_take_try(&ctrl.db_lock)) {
+ error = BKS_MODEL_SQLITE_OPEN_RUNNING;
+ goto _return;
+ } else {
+ eina_lock_release(&ctrl.db_lock);
+ }
+
retval = sqlite3_open_v2(mdl.db_path, &pDb, SQLITE_OPEN_URI | SQLITE_OPEN_READONLY , NULL);
if (retval ) {
error = BKS_MODEL_SQLITE_OPEN_ERROR;
@@ -199,6 +222,7 @@ Bks_Status _bks_model_sql_user_accounts_get(Eina_List **list) {
}
_close_db:
sqlite3_close(pDb);
+_return:
return error;
}
@@ -215,6 +239,13 @@ Bks_Status _bks_model_sql_recent_user_accounts_get(Eina_List **list, const unsig
int image_size;
Bks_Status error = BKS_MODEL_SQLITE_ERROR;
+ if (!eina_lock_take_try(&ctrl.db_lock)) {
+ error = BKS_MODEL_SQLITE_OPEN_RUNNING;
+ goto _return;
+ } else {
+ eina_lock_release(&ctrl.db_lock);
+ }
+
retval = sqlite3_open_v2(mdl.db_path, &pDb, SQLITE_OPEN_URI | SQLITE_OPEN_READONLY , NULL);
if (retval ) {
error = BKS_MODEL_SQLITE_OPEN_ERROR;
@@ -263,6 +294,7 @@ Bks_Status _bks_model_sql_recent_user_accounts_get(Eina_List **list, const unsig
}
_close_db:
sqlite3_close(pDb);
+_return:
return error;
}
@@ -276,6 +308,13 @@ Bks_Status _bks_model_sql_commit_sale(const Bks_Model_Sale *sales) {
Bks_Model_User_Account *current_user;
Eina_List *current_node;
Bks_Status error = BKS_MODEL_SQLITE_ERROR;
+
+ if (!eina_lock_take_try(&ctrl.db_lock)) {
+ error = BKS_MODEL_SQLITE_OPEN_RUNNING;
+ goto _return;
+ } else {
+ eina_lock_release(&ctrl.db_lock);
+ }
retval = sqlite3_open_v2(mdl.db_path, &pDb, SQLITE_OPEN_URI | SQLITE_OPEN_READWRITE, NULL);
if(retval != SQLITE_OK) {
@@ -331,6 +370,7 @@ Bks_Status _bks_model_sql_commit_sale(const Bks_Model_Sale *sales) {
error = BKS_MODEL_SQLITE_OK;
}
sqlite3_close(pDb);
+_return:
return error;
}

0 comments on commit f20deaa

Please sign in to comment.