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