I am using Qt 6.4.1
. The code is written in C++.
mysql Ver 8.0.35-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
Linux 6.2.0-37-generic #38~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 2 18:01:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
I am using Qt SQL module for operating on a MySQL database that I have created. I can successfully connect to the database and perform operations on it.
Sample code demonstrating SQL queries:
namespace queries
{
constexpr std::string_view delete_from_Station_by_test_id = "DELETE FROM Station WHERE test_set_id=?;";
}
bool remove(unsigned long long test_set_id) const
{
QSqlQuery query;
query.prepare(queries::delete_from_Station_by_test_id.data());
query.addBindValue(test_set_id);
return query.exec();
}
Problem
Everything is working perfectly unless I lose the connection with the database server machine.
If I already lost the connection and trying to perform some operation, the application will hang. It is hanging on the query.prepare(querys::delete_from_Station_by_test_id.data());
line.
If I understand correctly, it is waiting for a response from the server.
Question
How to make it not hang?
I know that I can create some ping mechanisms that will check the server status before applying a query, so I am not looking for a workaround unless there is no MySQL or Qt SQL solution.
Investigations
I have tried to set the following properties MYSQL_OPT_CONNECT_TIMEOUT
, MYSQL_OPT_READ_TIMEOUT
, MYSQL_OPT_WRITE_TIMEOUT
, MYSQL_OPT_RECONNECT
using QSqlDatabase::setConnectOptions
, but without any result.
The lines below are a list of all properties:
The Qt MySQL/MariaDB plugin honors the following connection options:
Attribute Possible value
CLIENT_COMPRESS If set, switches to the compressed protocol after successful authentication
CLIENT_FOUND_ROWS If set, send found rows instead of affected rows
CLIENT_IGNORE_SPACE If set, ignore spaces before '('
CLIENT_NO_SCHEMA If set, don't allow database.table.column
CLIENT_INTERACTIVE If set, client is treated as interactive
MYSQL_OPT_PROTOCOL explicitly specify the protocol to use:
MYSQL_PROTOCOL_TCP: use tcp connection (ip/hostname specified through setHostname()) MYSQL_PROTOCOL_SOCKET: connect through a socket specified in UNIX_SOCKET MYSQL_PROTOCOL_PIPE: connect through a named pipe specified in UNIX_SOCKET MYSQL_PROTOCOL_MEMORY: connect through shared memory specified in MYSQL_SHARED_MEMORY_BASE_NAME
UNIX_SOCKET Specifies the socket or named pipe to use, even it's called UNIX_SOCKET it can also be used on windows
MYSQL_SHARED_MEMORY_BASE_NAME Specified the shared memory segment name to use
MYSQL_OPT_RECONNECT TRUE or 1: Automatically reconnect after connection loss
FALSE or 0: No automatic reconnect after connection loss (default)
See Automatic Reconnection Control
MYSQL_OPT_CONNECT_TIMEOUT The connect timeout in seconds
MYSQL_OPT_READ_TIMEOUT The timeout in seconds for each attempt to read from the server
MYSQL_OPT_WRITE_TIMEOUT The timeout in seconds for each attempt to write to the server
MYSQL_OPT_LOCAL_INFILE Set to 1 to enable the support for local LOAD_DATA, disabled if not set or 0
MYSQL_OPT_SSL_MODE The security state to use for the connection to the server: SSL_MODE_DISABLED, SSL_MODE_PREFERRED, SSL_MODE_REQUIRED, SSL_MODE_VERIFY_CA, SSL_MODE_VERIFY_IDENTITY.
MYSQL_OPT_TLS_VERSION A list of protocols the client permits for encrypted connections. The value can be a combination of 'TLSv1' ,' TLSv1.1', 'TLSv1.2' or 'TLSv1.3' depending on the used MySQL server version.
MYSQL_OPT_SSL_KEY / SSL_KEY (deprecated) The path name of the client private key file
MYSQL_OPT_SSL_CERT / SSL_CERT (deprecated) The path name of the client public key certificate file
MYSQL_OPT_SSL_CA / SSL_CA (deprecated) The path name of the Certificate Authority (CA) certificate file
MYSQL_OPT_SSL_CAPATH / SSL_CAPATH (deprecated) The path name of the directory that contains trusted SSL CA certificate files
MYSQL_OPT_SSL_CIPHER / SSL_CIPHER (deprecated) The list of permissible ciphers for SSL encryption
MYSQL_OPT_SSL_CRL The path name of the file containing certificate revocation lists
MYSQL_OPT_SSL_CRLPATH The path name of the directory that contains files containing certificate revocation lists