How to prevent program from crashing if mysql host is not reachable
I'm using the MySQL Connector for c++
I'm trying to connect to a remote MySQL server which happens to be offline at the moment and I noticed that my program throws an exception and aborts!!
It is OK that it could not connect however I'd like to keep my program running.
because in case of a failed connection I'd like to try connecting to a different server but I cant if the program crashes after the first attempt.
Is there a way to do prevent this crash?
Thanks,
here is the connection code:
Connection * conn;
mysql::MySQL_Driver *driver;
try
{
driver = mysql::get_mysql_driver_instance();
conn = driver->connect("host1", "user", "pswd");
} catch (SQLException &e) {
cout << "ERROR: SQLException in " << __FILE__;
cout << " (" << __func__<< ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << ")" << endl;
}
catch (std::runtime_error &e){
cout << "ERROR: runtime_error in " << __FILE__;
cout << " (" << __func__ << ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what() << endl;
}
Subject
Views
Written By
Posted
How to prevent program from crashing if mysql host is not reachable
2439
October 19, 2012 09:42AM
1350
October 22, 2012 07:03AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.