@ -1,22 +1,22 @@
# include "../lib/mysql.hpp"
marcelb : : mysql : : MySQL : : MySQL ( const string _path , const string _username , const string _password , const string _db , const uint32_t _available , const periodical_engin e _engine_type ) {
marcelb : : mysql : : MySQL : : MySQL ( const string _path , const string _username , const string _password , const string _db , const uint32_t _available , const time_loop_typ e _engine_type ) {
path = _path ;
username = _username ;
password = _password ;
database = _db ;
pool_size = _available > 0 ? _available : 1 ;
engine _type = _engine_type ;
tloop _type = _engine_type ;
drv = get_mysql_driver_instance ( ) ;
connect_pool ( ) ;
if ( engine_type = = periodical_engin e: : internal ) {
periodic_engin = async ( launch : : async , [ & ] ( ) {
while ( run_engin ) {
if ( tloop_type = = time_loop_typ e: : internal ) {
tloop_future = async ( launch : : async , [ & ] ( ) {
while ( run_tloop ) {
usleep ( MYSQL_PERIODIC_INTERNAL_TIME * 1000 ) ;
periodic_maintenance ( ) ;
_tloop ( ) ;
}
return ;
} ) ;
@ -72,7 +72,6 @@ void marcelb::mysql::MySQL::disconnect_pool() {
bool marcelb : : mysql : : MySQL : : disconnect_connection ( Connection * connection ) {
bool status = ! connection - > isClosed ( ) ;
if ( status ) {
try {
connection - > close ( ) ;
@ -94,13 +93,30 @@ bool marcelb::mysql::MySQL::disconnect_connection(Connection* connection) {
return status ;
}
/**
* Broj pokušaja usljed povezivanja s bazom od 1 do unlimited ;
*/
void marcelb : : mysql : : MySQL : : _tloop ( ) {
for ( size_t i = 0 ; i < pool_size & & run_tloop ; i + + ) {
try {
Connection * conn = occupy_connection ( ) ;
if ( conn - > isValid ( ) ) {
release_connection ( conn ) ;
} else {
if ( ! conn - > isClosed ( ) ) {
conn - > close ( ) ;
}
Connection * n_conn = create_connection ( ) ;
release_connection ( n_conn ) ;
}
} catch ( const SQLException & error ) {
if ( on_error ) {
on_error ( error . what ( ) + string ( " , SQL state: " ) + error . getSQLState ( ) + string ( " , Error code: " ) + to_string ( error . getErrorCode ( ) ) ) ;
}
}
}
last_loop_time = time ( nullptr ) ;
}
void marcelb : : mysql : : MySQL : : set_connect_trys ( const uint32_t _trys ) {
lock_guard < mutex > lock ( io ) ;
// lock_guard<mutex> lock(io);
connect_trys = _trys ;
}
@ -109,6 +125,9 @@ void marcelb::mysql::MySQL::set_on_error(function<void(const string&)> _on_erro
}
Connection * marcelb : : mysql : : MySQL : : occupy_connection ( ) {
if ( last_loop_time + ( MYSQL_PERIODIC_INTERNAL_TIME / 1000 ) * 3 < time ( nullptr ) & & on_error ) { // ako je zadnje vrijeme + 3 intervala manje od trenutnog vremena emitiraj grešku
on_error ( " The time loop is not executing properly " ) ;
}
unique_lock < mutex > lock ( io ) ;
while ( connection_pool . empty ( ) ) {
condition . wait ( lock ) ;
@ -116,20 +135,6 @@ Connection* marcelb::mysql::MySQL::occupy_connection() {
Connection * connection = connection_pool . front ( ) ;
connection_pool . pop ( ) ;
return connection ;
// while (true) {
// while(connection_pool.size()) {
// io.lock();
// Connection* connection = connection_pool.front();
// connection_pool.pop();
// if (connection->isValid()) {
// io.unlock();
// return connection;
// }
// io.unlock();
// }
// usleep(1000);
// }
}
void marcelb : : mysql : : MySQL : : release_connection ( Connection * connection ) {
@ -139,35 +144,23 @@ void marcelb::mysql::MySQL::release_connection(Connection* connection) {
}
marcelb : : mysql : : MySQL : : ~ MySQL ( ) {
if ( engine_type = = periodical_engin e: : internal ) {
run_engin = false ;
periodic_engin . get ( ) ;
if ( tloop_type = = time_loop_typ e: : internal ) {
run_tloop = false ;
tloop_future . get ( ) ;
} else {
run_engin = false ;
run_tloop = false ;
}
disconnect_pool ( ) ;
}
void marcelb : : mysql : : MySQL : : periodic_maintenance ( ) {
for ( size_t i = 0 ; i < pool_size & & run_engin ; i + + ) {
try {
Connection * conn = occupy_connection ( ) ;
if ( conn - > isValid ( ) ) {
release_connection ( conn ) ;
} else {
if ( ! conn - > isClosed ( ) ) {
conn - > close ( ) ;
}
Connection * n_conn = create_connection ( ) ;
release_connection ( n_conn ) ;
}
} catch ( const SQLException & error ) {
if ( on_error ) {
on_error ( error . what ( ) + string ( " , SQL state: " ) + error . getSQLState ( ) + string ( " , Error code: " ) + to_string ( error . getErrorCode ( ) ) ) ;
}
void marcelb : : mysql : : MySQL : : tloop ( ) {
if ( tloop_type = = time_loop_type : : internal ) {
if ( on_error ) {
on_error ( " Can't start external call tloop, internal is active! " ) ;
}
return ;
}
_tloop ( ) ;
}