how to set the connect blocking time when use QSqlDatabase::open()?
-
wrote on 14 Jan 2021, 03:17 last edited by
now i use QSqlDatabase::open() to connect the sql sever.
After reading Qt help,i use QSqlDatabase::setConnectOptions() to set the blocking time.QSqlDatabase db; /*......*/ db.setConnectOptions("SQL_ATTR_LOGIN_TIMEOUT=3");
when i input the wrong ip,like196.168.0.999,the code work,db.open() will blocking 3s .
but when i input a legal but unconnectable ip like 196.168.0.1, the code doesn‘t work,db.open() will blocking about 50s .How to solve this problem?how to set the blocking time?
-
now i use QSqlDatabase::open() to connect the sql sever.
After reading Qt help,i use QSqlDatabase::setConnectOptions() to set the blocking time.QSqlDatabase db; /*......*/ db.setConnectOptions("SQL_ATTR_LOGIN_TIMEOUT=3");
when i input the wrong ip,like196.168.0.999,the code work,db.open() will blocking 3s .
but when i input a legal but unconnectable ip like 196.168.0.1, the code doesn‘t work,db.open() will blocking about 50s .How to solve this problem?how to set the blocking time?
wrote on 14 Jan 2021, 07:44 last edited by@boom-stack
Do you mean it only blocks 50s when you have that line, or would it still block 50s without that line?You don't say anything about what your database is. In any case, that option will be passed on directly from Qt, Qt does nothing about it, so you should search the web for solutions relevant to your database.
-
wrote on 14 Jan 2021, 08:24 last edited by
i use QSqlDatabase to connect the sql server.
With that line or without that line,when i input a legal but unconnectable ip like 196.168.0.1,they both will block 50s. -
i use QSqlDatabase to connect the sql server.
With that line or without that line,when i input a legal but unconnectable ip like 196.168.0.1,they both will block 50s.wrote on 14 Jan 2021, 08:55 last edited by@boom-stack said in how to set the connect blocking time when use QSqlDatabase::open()?:
i use QSqlDatabase to connect the sql server.
I know that. But that says nothing about what the SQL server is. It might be SQLite, MySQL, MariaDB, PostgreSQL, ... Whichever it is, that is who interprets the
SQL_ATTR_LOGIN_TIMEOUT=3
--- I think it's actually the ODBC driver, so that may be what you have to Google to address your question. -
wrote on 14 Jan 2021, 09:07 last edited by
-
yeah it's actually the ODBC driver,but i just don’t know how to set the connect blocking time by use QSqlDatabase::setConnectOptions().
except this linedb.setConnectOptions("SQL_ATTR_LOGIN_TIMEOUT=3");
what should i set?

wrote on 14 Jan 2021, 09:09 last edited by JonB@boom-stack
I don't know either, that's why I said you'll want to Google!Purely at a guess, since you're saying it spends 50 seconds trying and failing to connect, did you investigate
SQL_ATTR_CONNECTION_TIMEOUT
? -
wrote on 14 Jan 2021, 09:17 last edited by
yes i try,but it doesn't work
hhhhokok,whatever thank you bro. -
yes i try,but it doesn't work
hhhhokok,whatever thank you bro.wrote on 14 Jan 2021, 09:27 last edited by JonB@boom-stack
Then I think you have a problem! I see e.g. https://stackoverflow.com/questions/2309684/connection-timeout-in-odbc saying you should do this viaSQL_ATTR_CONNECTION_TIMEOUT
, but then as per the last answer there https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2012/ms131709(v%3Dsql.110) statesThe SQL Server Native Client ODBC driver ignores the setting of SQL_ATTR_CONNECTION_TIMEOUT.
So it may depend on on which client ODBC driver you use.....
I would do a lot of Googling :)
-
wrote on 14 Jan 2021, 09:43 last edited by
i did a lot google,but maybe i ignore something.
thank you bro!it help
1/9