I’m working with MySQL++ library to connect to a MySQL server and run various database operations. My issue is that I need to read from tables that are frequently updated by other processes. To get consistent data, I have to lock these tables first.
The problem is MySQL doesn’t support NOWAIT functionality for table locking. When another process already has a lock that takes forever to release, my program just hangs waiting. Instead of waiting indefinitely, I want my code to give up after a few seconds, show an error like ‘Unable to acquire lock’, and retry later.
I tried implementing a timeout mechanism using signals but I’m stuck. When I test this with a pre-locked table, the timeout message appears correctly, but the database operation doesn’t actually stop. How can I properly cancel the ongoing query?
volatile sig_atomic_t completed = 1;
void timeout_handler(int signal_num) {
cout << "Operation timed out" << endl;
completed = 0;
signal(signal_num, timeout_handler);
}
// database connection setup
// *CODE OMITTED*
signal(SIGALRM, timeout_handler);
alarm(3);
mysql_query(db_connection, "LOCK TABLES ABC WRITE");
Any suggestions on how to properly terminate the mysql query when timeout occurs?