library/src/blockclass/subclasses/BP_MySQLMultiThreadConnectionPool.cc
// class constructor (automatically sets up individual connections)
class constructor (automatically sets up individual connections)
class constructor ( automatically sets up individual connections )
BP_MySQLMultiThreadConnectionPool::BP_MySQLMultiThreadConnectionPool(char *host, char *user, char *pass, char *name, size_t port, size_t number_of_connections_to_init)
{
	this->conns  = NULL;
	this->conn_n = 0;
	this->init_status = false;
	this->gc = BP_CreateNewGarbageCollector("MySQLi Connection Pool Garbage Collector", 1);
	if(!this->gc)
		return;
	size_t conn_elems_size = number_of_connections_to_init * sizeof(BP_MYSQL_CONN_POOL_ENTRY);
	this->conns = (P_BP_MYSQL_CONN_POOL_ENTRY) BP_calloc(this->gc, "connection pool entries", conn_elems_size, 1);
	if(!this->conns)
	{
		BP_DestroyGarbageCollector(this->gc);
		return;
	}
	this->conn_n = number_of_connections_to_init;
	size_t n = 0;
	for(; n < this->conn_n; n++)
	{
		connect_try:
		this->conns[n].mysqli = new Block();
		if(this->conns[n].mysqli->MysqlConnect(host, user, pass, name, port) == ERR_FAILURE)
		{
			delete this->conns[n].mysqli;
			this->conns[n].mysqli = NULL;
			printf("\n [!!] Connect failed during pool creation.  Retrying in a few seconds.");
			sleep(BP_GenRandomUnsignedLong(1,5));
			goto connect_try;
		}
		this->conns[n].mutex = CreateNewMutex();
	}
	this->mutex = CreateNewMutex();
	this->init_status = true;
}
BP_MySQLMultiThreadConnectionPool :: BP_MySQLMultiThreadConnectionPool ( char * host , char * user , char * pass , char * name , size_t port , size_t number_of_connections_to_init ) { this -> conns = NULL ; this -> conn_n = 0 ; this -> init_status = false ; this -> gc = BP_CreateNewGarbageCollector ( " " , 1 ) ; if ( ! this -> gc ) return ; size_t conn_elems_size = number_of_connections_to_init * sizeof ( BP_MYSQL_CONN_POOL_ENTRY ) ; this -> conns = ( P_BP_MYSQL_CONN_POOL_ENTRY ) BP_calloc ( this -> gc , " " , conn_elems_size , 1 ) ; if ( ! this -> conns ) { BP_DestroyGarbageCollector ( this -> gc ) ; return ; } this -> conn_n = number_of_connections_to_init ; size_t n = 0 ; for ( ; n < this -> conn_n ; n ++ ) { connect_try : this -> conns [ n ] . mysqli = new Block ( ) ; if ( this -> conns [ n ] . mysqli -> MysqlConnect ( host , user , pass , name , port ) == ERR_FAILURE ) { delete this -> conns [ n ] . mysqli ; this -> conns [ n ] . mysqli = NULL ; printf ( " \n " ) ; sleep ( BP_GenRandomUnsignedLong ( 1 , 5 ) ) ; goto connect_try ; } this -> conns [ n ] . mutex = CreateNewMutex ( ) ; } this -> mutex = CreateNewMutex ( ) ; this -> init_status = true ; }
class constructor (automatically sets up individual connections)
class constructor ( automatically sets up individual connections )
// null initialize connection pool data // set initialization status to uninitialized // initialize the garbage collector // exit if we cannot create garbage collector (required) // calculate connection structure array size // allocate space for elements // if we can't allocate connections, exit // set connection size // now create individual connections // retry the connect // initialize new block object // connect to database // delete the object // nullify // go to the connect label // create new thread mutex // create mutex for object itself // set initialization status to true