@ -1,22 +1,22 @@
# include "../lib/mysql.hpp"
# 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 ;
path = _path ;
username = _username ;
username = _username ;
password = _password ;
password = _password ;
database = _db ;
database = _db ;
pool_size = _available > 0 ? _available : 1 ;
pool_size = _available > 0 ? _available : 1 ;
engine _type = _engine_type ;
tloop _type = _engine_type ;
drv = get_mysql_driver_instance ( ) ;
drv = get_mysql_driver_instance ( ) ;
connect_pool ( ) ;
connect_pool ( ) ;
if ( engine_type = = periodical_engin e: : internal ) {
if ( tloop_type = = time_loop_typ e: : internal ) {
periodic_engin = async ( launch : : async , [ & ] ( ) {
tloop_future = async ( launch : : async , [ & ] ( ) {
while ( run_engin ) {
while ( run_tloop ) {
usleep ( MYSQL_PERIODIC_INTERNAL_TIME * 1000 ) ;
usleep ( MYSQL_PERIODIC_INTERNAL_TIME * 1000 ) ;
periodic_maintenance ( ) ;
_tloop ( ) ;
}
}
return ;
return ;
} ) ;
} ) ;
@ -72,7 +72,6 @@ void marcelb::mysql::MySQL::disconnect_pool() {
bool marcelb : : mysql : : MySQL : : disconnect_connection ( Connection * connection ) {
bool marcelb : : mysql : : MySQL : : disconnect_connection ( Connection * connection ) {
bool status = ! connection - > isClosed ( ) ;
bool status = ! connection - > isClosed ( ) ;
if ( status ) {
if ( status ) {
try {
try {
connection - > close ( ) ;
connection - > close ( ) ;
@ -94,13 +93,30 @@ bool marcelb::mysql::MySQL::disconnect_connection(Connection* connection) {
return status ;
return status ;
}
}
void marcelb : : mysql : : MySQL : : _tloop ( ) {
/**
for ( size_t i = 0 ; i < pool_size & & run_tloop ; i + + ) {
* Broj pokušaja usljed povezivanja s bazom od 1 do unlimited ;
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 ) {
void marcelb : : mysql : : MySQL : : set_connect_trys ( const uint32_t _trys ) {
lock_guard < mutex > lock ( io ) ;
// lock_guard<mutex> lock(io);
connect_trys = _trys ;
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 ( ) {
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 ) ;
unique_lock < mutex > lock ( io ) ;
while ( connection_pool . empty ( ) ) {
while ( connection_pool . empty ( ) ) {
condition . wait ( lock ) ;
condition . wait ( lock ) ;
@ -116,20 +135,6 @@ Connection* marcelb::mysql::MySQL::occupy_connection() {
Connection * connection = connection_pool . front ( ) ;
Connection * connection = connection_pool . front ( ) ;
connection_pool . pop ( ) ;
connection_pool . pop ( ) ;
return connection ;
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 ) {
void marcelb : : mysql : : MySQL : : release_connection ( Connection * connection ) {
@ -139,35 +144,23 @@ void marcelb::mysql::MySQL::release_connection(Connection* connection) {
}
}
marcelb : : mysql : : MySQL : : ~ MySQL ( ) {
marcelb : : mysql : : MySQL : : ~ MySQL ( ) {
if ( engine_type = = periodical_engin e: : internal ) {
if ( tloop_type = = time_loop_typ e: : internal ) {
run_engin = false ;
run_tloop = false ;
periodic_engin . get ( ) ;
tloop_future . get ( ) ;
} else {
} else {
run_engin = false ;
run_tloop = false ;
}
}
disconnect_pool ( ) ;
disconnect_pool ( ) ;
}
}
void marcelb : : mysql : : MySQL : : periodic_maintenance ( ) {
void marcelb : : mysql : : MySQL : : tloop ( ) {
for ( size_t i = 0 ; i < pool_size & & run_engin ; i + + ) {
if ( tloop_type = = time_loop_type : : internal ) {
try {
if ( on_error ) {
on_error ( " Can't start external call tloop, internal is active! " ) ;
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 ( ) ) ) ;
}
}
}
return ;
}
}
_tloop ( ) ;
}
}