Detect and handle when a database query goes wrong

淺唱寂寞╮ 提交于 2019-12-13 02:06:50

问题


My problem is I want all my queries must return results after a limited time. AFAIK, postgres has 2 options for this: connect_timeout when open a connection to database, and statement_timeout for a query.

This lead to 2 problems:

  1. I must estimate how much time the query is run. My approach is setup a worst case scenario: with a preset bandwidth to db server, a query with a lot record... to determine it, but I think this ain't a smart way. Are there any better ideas/patterns... to handle this?

  2. The network problem. Assume the network is bad with heavy packets loss, high ping as hell... the query from clients, and the result from the server are stuck ... Of course we can set a timeout from the code, but I think it will be complicated due to handle resource and other things, and it's duplicated with the database timeout mechanism. Are there anyway to handle this?

Another version of the story: when a query take a long time, I want to distinguish: this query is all good, just has too many records, wait for it, and no, the query is "broken",don't wait for it...

Ps : I found this link, but this is for SQL Server 2005 :(

http://www.mssqltips.com/sqlservertip/1338/finding-a-sql-server-process-percentage-complete-with-dmvs/


回答1:


As you already mentioned, it is hard to predict how long a query runs (due to the query itself and its parameters, due to network, due to server load).

Anyway you should move the SQL queries into QThreads. This allows your application from serving the GUI while the queries run.

Also I would not try to solve this by timeouts. You will get into a lot of trouble because you will fail to choose the right timeouts for each query and each situation. Instead provide a way of cancelling queries by a button or a dialog so the user can decide if it is sensible to continue waiting or not.




回答2:


What you want to do:

when a query take a long time, I want to distinguish: this query is all good, just has too many records, wait for it, and no, the query is "broken",don't wait for it.

is just not going to work out. You appear to require a solution to the halting problem, a fundamentally hard problem in computer science.

You must decide how long is acceptable for a query to run, and set a timeout. There is no reliable way to predict how long it should run, except by looking at how long other similar queries took to run before. Nor is there any way to tell the difference between a correct (but slow) query and one that is going to run forever. That's particularly true when things like WITH RECURSIVE or PL/PgSQL functions are involved.




回答3:


You can do the queries in a specific class the object of which resides in a separate thread and wait for a timeout for the object to quit :

databaseObject->performQuery();

QThread * th = databaseObject->thread();
th->quit();
th->wait(2000);

if(th->isRunning())
{
    th->terminate();
    return false;
}
else
    return true;


来源:https://stackoverflow.com/questions/25831313/detect-and-handle-when-a-database-query-goes-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!