java - Execute a query but get an error if it is in waiting state for too long -


is there way application send query database have database terminate query , throw error if query waits long? like:

wait cast('1 min' interval) <your query here> 

if value of waiting in pg_stat_activity has been true longer given interval, database should cancel query , return error, interpreted application exception.

does exist? or there workaround (preferably written in java) can achieve same effect?

a particular use case i'm targeting (in simplified form of java code):

try {     statement.executeupdate("wait cast('30 sec' interval) truncate my_table");     //truncate may wait if other processes accessing table,     //example backup process has locked table. } catch (waittimeoutexception wte) {     statement.executeupdate("delete my_table"); } 

the idea preferably truncate faster, if query ends waiting long, should fall delete instead job done.

this used time queries out in other use cases, don't want application waiting forever. instead, application can detected condition of "waiting long" , act appropriately (such alerting user or trying again after logging).

try:

set lock_timeout 1000; or set statement_timeout 1000; 

more information at: http://www.postgresql.org/docs/current/static/runtime-config-client.html#runtime-config-client-statement


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -