Add on error callback

generic
mbandic 2 months ago
parent 777ee53355
commit 27074f9269
  1. 4
      .vscode/settings.json
  2. 6
      lib/mysql.hpp
  3. 22
      src/mysql.cpp
  4. 4
      test/test.cpp

@ -59,6 +59,8 @@
"stop_token": "cpp", "stop_token": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeinfo": "cpp" "typeinfo": "cpp",
"any": "cpp",
"variant": "cpp"
} }
} }

@ -106,6 +106,7 @@ class MySQL {
bool run_engin = true; bool run_engin = true;
future<void> periodic_engin; future<void> periodic_engin;
periodical_engine engine_type; periodical_engine engine_type;
function<void(const string&)> on_error;
/** /**
* Open one database server connection * Open one database server connection
@ -165,6 +166,11 @@ class MySQL {
*/ */
void set_connect_trys(const uint32_t _trys); void set_connect_trys(const uint32_t _trys);
/**
* Set callback on error
*/
void set_on_error(function<void(const string&)> _on_error);
/** /**
* Execute the SQL statement * Execute the SQL statement
*/ */

@ -42,8 +42,9 @@ Connection* marcelb::mysql::MySQL::create_connection() {
} }
} }
catch (const SQLException &error) { catch (const SQLException &error) {
cout << error.what() << endl; if (on_error) {
// on_error -- ako se ikad impelementira pozovi ga ovdje! on_error(error.what() + string(", SQL state: ") + error.getSQLState() + string(", Error code: ") + to_string(error.getErrorCode()));
}
usleep(reconnectSleep); usleep(reconnectSleep);
connect_trys == unlimited ? trys : trys++; connect_trys == unlimited ? trys : trys++;
} }
@ -78,7 +79,9 @@ bool marcelb::mysql::MySQL::disconnect_connection(Connection* connection) {
status = !connection->isClosed(); status = !connection->isClosed();
} }
catch (const SQLException &error) { catch (const SQLException &error) {
cout << error.what() << endl; if (on_error) {
on_error(error.what() + string(", SQL state: ") + error.getSQLState() + string(", Error code: ") + to_string(error.getErrorCode()));
}
status = true; status = true;
} }
} }
@ -101,6 +104,10 @@ void marcelb::mysql::MySQL::set_connect_trys(const uint32_t _trys) {
connect_trys = _trys; connect_trys = _trys;
} }
void marcelb::mysql::MySQL::set_on_error(function<void(const string&)> _on_error) {
on_error = _on_error;
}
Connection* marcelb::mysql::MySQL::occupy_connection() { Connection* marcelb::mysql::MySQL::occupy_connection() {
unique_lock<mutex> lock(io); unique_lock<mutex> lock(io);
while (connection_pool.empty()) { while (connection_pool.empty()) {
@ -136,8 +143,6 @@ marcelb::mysql::MySQL::~MySQL() {
run_engin = false; run_engin = false;
periodic_engin.get(); periodic_engin.get();
} else { } else {
// ne bi bilo loše ubiti periodic nekako?!
// iako disconnecta može periodic connect napraviti!!!
run_engin = false; run_engin = false;
} }
@ -147,6 +152,8 @@ marcelb::mysql::MySQL::~MySQL() {
void marcelb::mysql::MySQL::periodic_maintenance() { void marcelb::mysql::MySQL::periodic_maintenance() {
for (size_t i = 0; i < pool_size && run_engin; i++) { for (size_t i = 0; i < pool_size && run_engin; i++) {
try {
Connection *conn = occupy_connection(); Connection *conn = occupy_connection();
if (conn->isValid()) { if (conn->isValid()) {
release_connection(conn); release_connection(conn);
@ -157,5 +164,10 @@ void marcelb::mysql::MySQL::periodic_maintenance() {
Connection *n_conn = create_connection(); Connection *n_conn = create_connection();
release_connection(n_conn); 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()));
}
}
} }
} }

@ -17,6 +17,10 @@ int main() {
// MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 10, periodical_engine::external); // MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 10, periodical_engine::external);
MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5); MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5);
mydb.set_on_error( [](const string& error) {
cout << error << endl;
});
// periodic mysql_maintenance ( [&mydb] () { // periodic mysql_maintenance ( [&mydb] () {
// mydb.periodic_maintenance(); // mydb.periodic_maintenance();
// }, MYSQL_PERIODIC_INTERNAL_TIME); // }, MYSQL_PERIODIC_INTERNAL_TIME);

Loading…
Cancel
Save