int my_con(db_con_t* con) { struct my_con* ptr; struct my_uri* uri; /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ ptr = (struct my_con*)db_pool_get(con->uri); if (ptr) { DBG("mysql: Connection to %.*s:%.*s found in connection pool\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); goto found; } ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con)); if (!ptr) { LOG(L_ERR, "mysql: No memory left\n"); goto error; } memset(ptr, '\0', sizeof(struct my_con)); if (db_pool_entry_init(&ptr->gen, my_con_free, con->uri) < 0) goto error; ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL)); if (!ptr->con) { LOG(L_ERR, "mysql: No enough memory\n"); goto error; } mysql_init(ptr->con); uri = DB_GET_PAYLOAD(con->uri); DBG("mysql: Creating new connection to: %.*s:%.*s\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); /* Put the newly created mysql connection into the pool */ db_pool_put((struct db_pool_entry*)ptr); DBG("mysql: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, ptr); con->connect = my_con_connect; con->disconnect = my_con_disconnect; return 0; error: if (ptr) { db_pool_entry_free(&ptr->gen); if (ptr->con) pkg_free(ptr->con); pkg_free(ptr); } return 0; }
int bdb_con(db_con_t* con) { bdb_con_t* bcon; bdb_uri_t* buri; buri = DB_GET_PAYLOAD(con->uri); /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ bcon = (bdb_con_t*)db_pool_get(con->uri); if (bcon) { DBG("bdb: Connection to %s found in connection pool\n", buri->uri); goto found; } bcon = (bdb_con_t*)pkg_malloc(sizeof(bdb_con_t)); if (!bcon) { ERR("bdb: No memory left\n"); goto error; } memset(bcon, '\0', sizeof(bdb_con_t)); if (db_pool_entry_init(&bcon->gen, bdb_con_free, con->uri) < 0) goto error; DBG("bdb: Preparing new connection to %s\n", buri->uri); if(bdb_is_database(buri->path.s)!=0) { ERR("bdb: database [%.*s] does not exists!\n", buri->path.len, buri->path.s); goto error; } /* Put the newly created BDB connection into the pool */ db_pool_put((struct db_pool_entry*)bcon); DBG("bdb: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, bcon); con->connect = bdb_con_connect; con->disconnect = bdb_con_disconnect; return 0; error: if (bcon) { db_pool_entry_free(&bcon->gen); pkg_free(bcon); } return -1; }
/* * Close the connection and release memory */ static void my_con_free(db_con_t* con, struct my_con* payload) { if (!payload) return; /* Delete the structure only if there are no more references * to it in the connection pool */ if (db_pool_remove((db_pool_entry_t*)payload) == 0) return; db_pool_entry_free(&payload->gen); if (payload->con) pkg_free(payload->con); pkg_free(payload); }
/** Free all memory allocated for a bdb_con structure. * This function function frees all memory that is in use by * a bdb_con structure. * @param con A generic db_con connection structure. * @param payload BDB specific payload to be freed. */ static void bdb_con_free(db_con_t* con, bdb_con_t *payload) { if (!payload) return; /* Delete the structure only if there are no more references * to it in the connection pool */ if (db_pool_remove((db_pool_entry_t*)payload) == 0) return; db_pool_entry_free(&payload->gen); /* destroy and free BDB env */ pkg_free(payload); }
int pg_con(db_con_t *con) { struct pg_con *pcon; /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ pcon = (struct pg_con *)db_pool_get(con->uri); if(pcon) { DBG("postgres: Connection to %.*s:%.*s found in connection pool\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); goto found; } pcon = (struct pg_con *)pkg_malloc(sizeof(struct pg_con)); if(!pcon) { LOG(L_ERR, "postgres: No memory left\n"); goto error; } memset(pcon, '\0', sizeof(struct pg_con)); if(db_pool_entry_init(&pcon->gen, pg_con_free, con->uri) < 0) goto error; DBG("postgres: Preparing new connection to: %.*s:%.*s\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); /* Put the newly created postgres connection into the pool */ db_pool_put((struct db_pool_entry *)pcon); DBG("postgres: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, pcon); con->connect = pg_con_connect; con->disconnect = pg_con_disconnect; return 0; error: if(pcon) { db_pool_entry_free(&pcon->gen); pkg_free(pcon); } return -1; }
int ld_con(db_con_t* con) { struct ld_con* lcon; struct ld_uri* luri; luri = DB_GET_PAYLOAD(con->uri); /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ lcon = (struct ld_con*)db_pool_get(con->uri); if (lcon) { DBG("ldap: Connection to %s found in connection pool\n", luri->uri); goto found; } lcon = (struct ld_con*)pkg_malloc(sizeof(struct ld_con)); if (!lcon) { ERR("ldap: No memory left\n"); goto error; } memset(lcon, '\0', sizeof(struct ld_con)); if (db_pool_entry_init(&lcon->gen, ld_con_free, con->uri) < 0) goto error; DBG("ldap: Preparing new connection to %s\n", luri->uri); /* Put the newly created LDAP connection into the pool */ db_pool_put((struct db_pool_entry*)lcon); DBG("ldap: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, lcon); con->connect = ld_con_connect; con->disconnect = ld_con_disconnect; return 0; error: if (lcon) { db_pool_entry_free(&lcon->gen); pkg_free(lcon); } return -1; }
/** Free all memory allocated for a ld_con structure. * This function function frees all memory that is in use by * a ld_con structure. * @param con A generic db_con connection structure. * @param payload LDAP specific payload to be freed. */ static void ld_con_free(db_con_t* con, struct ld_con* payload) { struct ld_uri* luri; int ret; if (!payload) return; luri = DB_GET_PAYLOAD(con->uri); /* Delete the structure only if there are no more references * to it in the connection pool */ if (db_pool_remove((db_pool_entry_t*)payload) == 0) return; db_pool_entry_free(&payload->gen); if (payload->con) { ret = ldap_unbind_ext_s(payload->con, NULL, NULL); if (ret != LDAP_SUCCESS) { ERR("ldap: Error while unbinding from %s: %s\n", luri->uri, ldap_err2string(ret)); } } pkg_free(payload); }