PDA

View Full Version : [Solved] Using QSqlDatabase - long timeout if no network cornnection



ShamusVW
25th July 2016, 13:51
I am using QSqlDatabase to get a connection to 2 databases, a MySQL and MS SQL database.
I am trying to do some error checking for when it loses network connection, and thus cannot connect to the database.
With the MySQL database, it timeout very quickly before returning an error that it can't connect, but with the Microsoft SQL database, it takes quite a few seconds before it responds (gives the impression of hanging)
Is there any way for me to configure the connection options to shorten the timeout comparable to the MySQL connection?

This is my code for the MS SQL class...


{
m_ConnectionName = cname;
db = QSqlDatabase::addDatabase("QODBC", m_ConnectionName);
db.setDatabaseName("DRIVER={SQL Server};Server="+host+";Database="+dbase+";Uid="+user+";Pwd="+pw+";WSID=");
db.open();

m_LastError = db.lastError().text();
if (!db.isOpen()) {
db.setConnectOptions(); //clears connect options string
m_DBOpen = false;
}
else
m_DBOpen = true;
}


Added after 1 12 minutes:

Thanks, I seem to have figured it out, I use the following:


db.setConnectOptions("SQL_ATTR_LOGIN_TIMEOUT=2");