{
PGconn *link = (PGconn *)rsrc->ptr;
PGresult *res;
+ zval *hash;
while ((res = PQgetResult(link))) {
PQclear(res);
}
PQfinish(link);
PGG(num_links)--;
+
+ /* Remove connection hash for this link */
+ hash = zend_hash_index_find(&PGG(hashes), (uintptr_t) link);
+ if (hash) {
+ zend_hash_index_del(&PGG(hashes), (uintptr_t) link);
+ zend_hash_del(&EG(regular_list), Z_STR_P(hash));
+ }
}
/* }}} */
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
/* Initilize notice message hash at MINIT only */
zend_hash_init_ex(&pgsql_globals->notices, 0, NULL, ZVAL_PTR_DTOR, 1, 0);
+ zend_hash_init_ex(&pgsql_globals->hashes, 0, NULL, ZVAL_PTR_DTOR, 1, 0);
}
/* }}} */
{
UNREGISTER_INI_ENTRIES();
zend_hash_destroy(&PGG(notices));
+ zend_hash_destroy(&PGG(hashes));
return SUCCESS;
}
{
/* clean up notice messages */
zend_hash_clean(&PGG(notices));
+ zend_hash_clean(&PGG(hashes));
/* clean up persistent connection */
zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions);
return SUCCESS;
}
link = (zend_resource *)index_ptr->ptr;
- if (link->ptr && (link->type == le_link || link->type == le_plink)) {
- php_pgsql_set_default_link(link);
- GC_REFCOUNT(link)++;
- RETVAL_RES(link);
- goto cleanup;
- } else {
- zend_hash_del(&EG(regular_list), str.s);
- }
+ ZEND_ASSERT(link->ptr && (link->type == le_link || link->type == le_plink));
+ php_pgsql_set_default_link(link);
+ GC_REFCOUNT(link)++;
+ RETVAL_RES(link);
+ goto cleanup;
}
if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
php_error_docref(NULL, E_WARNING, "Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links));
if (zend_hash_update_mem(&EG(regular_list), str.s, (void *) &new_index_ptr, sizeof(zend_resource)) == NULL) {
goto err;
}
+
+ /* Keep track of link => hash mapping, so we can remove the hash entry from regular_list
+ * when the connection is closed. This uses the address of the connection rather than the
+ * zend_resource, because the resource destructor is passed a stack copy of the resource
+ * structure. */
+ {
+ zval tmp;
+ ZVAL_STR_COPY(&tmp, str.s);
+ zend_hash_index_update(&PGG(hashes), (uintptr_t) pgsql, &tmp);
+ }
PGG(num_links)++;
}
/* set notice processor */