library/src/databases/mysql/BP-DBMySQLConnectionPooling.cc
// This is the connection pool constructor.  How it works, is you provide it a set of
// credentials, and it tries to create a specified number of connections.  The connections
// are then made available by petitioning the connection pool structure with the acquisition
// routines below.
This is the connection pool constructor.  How it works, is you provide it a set of
credentials, and it tries to create a specified number of connections.  The connections
are then made available by petitioning the connection pool structure with the acquisition
routines below.
This is the connection pool constructor . How it works is you provide it a set of credentials and it tries to create a specified number of connections . The connections are then made available by petitioning the connection pool structure with the acquisition routines below .
P_BP_MYSQL_CONN_POOL BP_CreateMySQLConnectionPool
(
		size_t number_of_pool_entries_to_create,
		char * host,
		char * user,
		char * pass,
		char * db,
		size_t port,
		size_t delay_between_connects_in_secs
)
{
	if(!number_of_pool_entries_to_create)
		return NULL;
	if(!host)
		return NULL;
	if(!user)
		return NULL;
	if(!pass)
		return NULL;
	if(!port)
		port = 3306;
	if(!delay_between_connects_in_secs)
		delay_between_connects_in_secs = 2;
	P_BP_MYSQL_CONN_POOL pool = (P_BP_MYSQL_CONN_POOL) bpcalloc(sizeof(BP_MYSQL_CONN_POOL), 1);
	if(!pool)
		return NULL;
	pool->tq = BP_CreateLinkL("BP MySQL C Connection Pool", BP_TRUE);
	sem_init(&pool->lock, 1, 1);
	pool->entries = (P_BP_MYSQL_CONNECTION_POOL_ENTRY) tq_calloc(pool->tq, sizeof(BP_MYSQL_CONNECTION_POOL_ENTRY)*number_of_pool_entries_to_create, 1, BPLN);
	if(!pool->entries)
	{
		BP_DestroyMySQLConnectionPool(pool);
		return NULL;
	}
	size_t n = 0;
	for(; n < number_of_pool_entries_to_create; n++)
	{
		P_BP_MYSQL_SESSION session = BP_MySQLConnect(host, user, pass, db, port);
		if(!session)
		{
			BP_DestroyMySQLConnectionPool(pool);
			return NULL;
		}
		pool->entries_total++;
		pool->entries[n].conn = session;
		size_t pool_entry_id = NEW_RANDOM_ID;
		for(size_t x = 0; x < n; x++)
		{
			if(pool_entry_id == pool->entries[n].pool_entry_id)
			{
				pool_entry_id = NEW_RANDOM_ID;
				x = 0;
				continue;
			}
		}
		pool->entries[n].pool_entry_id = pool_entry_id;
		sem_init(&pool->entries[n].lock, 1, 1);
		sleep(delay_between_connects_in_secs);
	}
	return pool;
}
P_BP_MYSQL_CONN_POOL BP_CreateMySQLConnectionPool ( size_t number_of_pool_entries_to_create , char * host , char * user , char * pass , char * db , size_t port , size_t delay_between_connects_in_secs ) { if ( ! number_of_pool_entries_to_create ) return NULL ; if ( ! host ) return NULL ; if ( ! user ) return NULL ; if ( ! pass ) return NULL ; if ( ! port ) port = 3306 ; if ( ! delay_between_connects_in_secs ) delay_between_connects_in_secs = 2 ; P_BP_MYSQL_CONN_POOL pool = ( P_BP_MYSQL_CONN_POOL ) bpcalloc ( sizeof ( BP_MYSQL_CONN_POOL ) , 1 ) ; if ( ! pool ) return NULL ; pool -> tq = BP_CreateLinkL ( " " , BP_TRUE ) ; sem_init ( & pool -> lock , 1 , 1 ) ; pool -> entries = ( P_BP_MYSQL_CONNECTION_POOL_ENTRY ) tq_calloc ( pool -> tq , sizeof ( BP_MYSQL_CONNECTION_POOL_ENTRY ) * number_of_pool_entries_to_create , 1 , BPLN ) ; if ( ! pool -> entries ) { BP_DestroyMySQLConnectionPool ( pool ) ; return NULL ; } size_t n = 0 ; for ( ; n < number_of_pool_entries_to_create ; n ++ ) { P_BP_MYSQL_SESSION session = BP_MySQLConnect ( host , user , pass , db , port ) ; if ( ! session ) { BP_DestroyMySQLConnectionPool ( pool ) ; return NULL ; } pool -> entries_total ++ ; pool -> entries [ n ] . conn = session ; size_t pool_entry_id = NEW_RANDOM_ID ; for ( size_t x = 0 ; x < n ; x ++ ) { if ( pool_entry_id == pool -> entries [ n ] . pool_entry_id ) { pool_entry_id = NEW_RANDOM_ID ; x = 0 ; continue ; } } pool -> entries [ n ] . pool_entry_id = pool_entry_id ; sem_init ( & pool -> entries [ n ] . lock , 1 , 1 ) ; sleep ( delay_between_connects_in_secs ) ; } return pool ; }
This is the connection pool constructor.
This is the connection pool constructor .
// pool entry count is required for connect // host is required for connects // user is required for connects // pass is required for connects // set default port to 3306 if unset // set default connect interval to 2 seconds if unset // create space for the new pool structure // create a new tail queue linked list allocator // initialize the pool locking semaphore to prevent thread conflicts // create space for each individual session, zero out memory.  Allocate // data within the current tail queue allocator. // clear everything out if we have no entries allocated // destroy the pool as it exists and return if we cannot create the connection pool // create pools // attempt to connect // ensure we were able to create a session // destroy the pool as it exists and return if we cannot create the connection pool // increment the number of total entries // set the session within the indexed entry // create a new random id for the pool entry // walk pool entries and ensure we got a real-deal unique id // for the pool entry. // ensure id doesn't already exist in the pool, if it does // reset the id with a new random value, reset iterator, and restart // loop. // spawn a new id in // reset iterator and continue the loop // set a new random id // initialize the new session lock // sleep delay before next connect // return the connection pool