/* * Get a connection from the pool, reuse existing * if possible, otherwise create a new one */ struct flat_con* flat_get_connection(const str* dir, const str* table) { struct flat_id* id; struct flat_con* ptr; int pid; if (!dir || !table) { LM_ERR("invalid parameter value\n"); return 0; } pid = getpid(); if (pool && (pool_pid != pid)) { LM_ERR("inherited open database connections, " "this is not a good idea\n"); return 0; } pool_pid = pid; id = new_flat_id(dir, table); if (!id) return 0; ptr = pool; while (ptr) { if (cmp_flat_id(id, ptr->id)) { LM_DBG("connection found in the pool\n"); ptr->ref++; free_flat_id(id); return ptr; } ptr = ptr->next; } LM_DBG("connection not found in the pool\n"); ptr = flat_new_connection(id); if (!ptr) { free_flat_id(id); return 0; } ptr->next = pool; pool = ptr; return ptr; }
/* * Close the connection and release memory */ void flat_free_connection(struct flat_con* con) { if (!con) return; if (con->id) free_flat_id(con->id); if (con->file) { fclose(con->file); } pkg_free(con); }