[ACCEPTED]-JDBC postgres query with a timeout-jdbc

Accepted answer
Score: 21

The "statement_timeout" looks like what you want.

SET statement_timeout TO 1000; -- for a second
<your_query_here>;
RESET statement_timeout; -- reset

0

Score: 1

One way might be to try running the query 4 in a Timer class. Throw an exception 3 if the Timer ends without a value returned.

Hibernate and 2 JDO supply such a construct. Maybe they'd 1 be good alternatives for you.

Score: 1

Using the LOCAL keyword limits the scope 3 of the statement_timeout to the current transaction. That 2 way, if anything goes wrong (e.g. it times 1 out) the timeout gets reset.

BEGIN TRANSACTION;
SET LOCAL statement_timeout TO 1000;    -- one-second timeout
SELECT COUNT(*) FROM really_huge_table; -- your slow query
ROLLBACK;                               -- reset

More Related questions