char* g_backup_path = NULL;
int g_fpid = -1;
int g_ref_cnt = 0;
+int g_db_cnt = 0;
+int g_backup_db_cnt = 0;
static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
{
return 0;
}
+bool __vc_db_connect_db(char** path, sqlite3** db_handle)
+{
+ bool is_connect = false;
+ int ret = __vc_db_open_db(path, db_handle);
+ if (0 != ret) {
+ int cnt = 0;
+ while (cnt <= 5) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
+
+ usleep(100000);
+ if (0 == __vc_db_open_db(path, db_handle)) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
+ is_connect = true;
+ break;
+ }
+ cnt++;
+ }
+ } else {
+ SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
+ is_connect = true;
+ }
+ return is_connect;
+}
+
int vc_db_initialize(void)
{
SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
}
snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
- if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
- int cnt = 0;
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
+ bool is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
+ if (false == is_connect) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing file");
if (0 != remove(g_path)) {
SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
+ g_db_cnt = (g_db_cnt++) % 1000;
+ snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
}
- if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
- }
- while (cnt <= 5) {
- if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
- } else {
- break;
+ is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
+ if (true == is_connect) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
+ is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
+ if (true == is_connect) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
+ if (0 != vc_db_restore_command()) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+ }
}
- cnt++;
- }
- if (0 != vc_db_restore_command()) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+ } else {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
+ return VC_DB_ERROR_OPERATION_FAILED;
}
}
if (SQLITE_CORRUPT == sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL)) {
- int cnt = 0;
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
if (0 != remove(g_path)) {
SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
+ g_db_cnt = (g_db_cnt++) % 1000;
+ snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
}
- if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
- }
- while (cnt <= 5) {
- if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
- } else {
- break;
+ is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
+ if (true == is_connect) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
+ is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
+ if (true == is_connect) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
+ if (0 != vc_db_restore_command()) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+ }
}
- cnt++;
- }
- if (0 != vc_db_restore_command()) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+ } else {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
+ return VC_DB_ERROR_OPERATION_FAILED;
}
}
- if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
+ is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
+ if (false == is_connect) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file");
if (0 != remove(g_backup_path)) {
+ g_backup_db_cnt = (g_backup_db_cnt++) % 1000;
SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
+ snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
}
- if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
- }
- if (0 != vc_db_backup_command()) {
- SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to backup command");
+ is_connect = __vc_db_connect_db(&g_path, &g_db_backup_handle);
+ if (true == is_connect) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
+ if (0 != vc_db_restore_command()) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+ }
}
}