diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c index 3752a06b23..c41ce0e65e 100644 --- a/src/switch_core_sqldb.c +++ b/src/switch_core_sqldb.c @@ -180,9 +180,10 @@ static switch_cache_db_handle_t *get_handle(const char *db_str, const char *user switch_mutex_lock(sql_manager.dbh_mutex); for (dbh_ptr = sql_manager.handle_pool; dbh_ptr; dbh_ptr = dbh_ptr->next) { - if (dbh_ptr->thread_hash == thread_hash && dbh_ptr->hash == hash && !dbh_ptr->use_count && + if (dbh_ptr->hash == hash && (dbh_ptr->thread_hash == thread_hash || !dbh_ptr->use_count) && !switch_test_flag(dbh_ptr, CDF_PRUNE) && switch_mutex_trylock(dbh_ptr->mutex) == SWITCH_STATUS_SUCCESS) { r = dbh_ptr; + break; } } @@ -197,11 +198,13 @@ static switch_cache_db_handle_t *get_handle(const char *db_str, const char *user } if (r) { + if (!r->use_count) { + r->thread_hash = thread_hash; + } + r->use_count++; r->total_used_count++; sql_manager.total_used_handles++; - r->hash = switch_ci_hashfunc_default(db_str, &hlen); - r->thread_hash = thread_hash; switch_set_string(r->last_user, user_str); } @@ -350,7 +353,7 @@ SWITCH_DECLARE(void) switch_cache_db_release_db_handle(switch_cache_db_handle_t if ((*dbh)->use_count) { if (--(*dbh)->use_count == 0) { - (*dbh)->thread_hash = 1; + (*dbh)->thread_hash = 0; } } switch_mutex_unlock((*dbh)->mutex);