postgresql-jdbc.git / summary / log / commit / refs

commit    892d7af82c4e077613a077ac950e63a8a5d069cc
Author:   Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
Date:     Sat Oct 31 11:38:00 2015 +0000

    test: add statement.getQueryTimeoutMs, so millisecond-scale timeouts can be tested


org/postgresql/jdbc2/AbstractJdbc2Statement.java | 34 ++++++++++++++++++++---- org/postgresql/test/jdbc2/StatementTest.java | 25 +++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/org/postgresql/jdbc2/AbstractJdbc2Statement.java b/org/postgresql/jdbc2/AbstractJdbc2Statement.java index 19b8921f..cb014bf8 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java @@ -115,7 +115,7 @@ public abstract class AbstractJdbc2Statement implements BaseStatement /** Number of rows to get in a batch. */ protected int fetchSize = 0; - /** Timeout (in seconds) for a query */ + /** Timeout (in milli-seconds) for a query */ protected int timeout = 0; protected boolean replaceProcessingEnabled = true; @@ -757,6 +757,30 @@ public abstract class AbstractJdbc2Statement implements BaseStatement * @exception SQLException if a database access error occurs */ public int getQueryTimeout() throws SQLException + { + return getQueryTimeoutMs() / 1000; + } + + /* + * Sets the queryTimeout limit + * + * @param seconds - the new query timeout limit in seconds + * @exception SQLException if a database access error occurs + */ + public void setQueryTimeout(int seconds) throws SQLException + { + setQueryTimeoutMs(seconds * 1000); + } + + /* + * The queryTimeout limit is the number of seconds the driver + * will wait for a Statement to execute. If the limit is + * exceeded, a SQLException is thrown. + * + * @return the current query timeout limit in seconds; 0 = unlimited + * @exception SQLException if a database access error occurs + */ + public int getQueryTimeoutMs() throws SQLException { checkClosed(); return timeout; @@ -768,14 +792,14 @@ public abstract class AbstractJdbc2Statement implements BaseStatement * @param seconds - the new query timeout limit in seconds * @exception SQLException if a database access error occurs */ - public void setQueryTimeout(int seconds) throws SQLException + public void setQueryTimeoutMs(int millis) throws SQLException { checkClosed(); - if (seconds < 0) + if (millis < 0) throw new PSQLException(GT.tr("Query timeout must be a value greater than or equals to 0."), PSQLState.INVALID_PARAMETER_VALUE); - timeout = seconds; + timeout = millis; } /** @@ -3475,7 +3499,7 @@ public abstract class AbstractJdbc2Statement implements BaseStatement } }; - connection.addTimerTask(cancelTimerTask, timeout * 1000); + connection.addTimerTask(cancelTimerTask, timeout); } private synchronized void killTimerTask() diff --git a/org/postgresql/test/jdbc2/StatementTest.java b/org/postgresql/test/jdbc2/StatementTest.java index 62a6610e..830b0418 100644 --- a/org/postgresql/test/jdbc2/StatementTest.java +++ b/org/postgresql/test/jdbc2/StatementTest.java @@ -8,6 +8,7 @@ package org.postgresql.test.jdbc2; import junit.framework.TestCase; +import org.postgresql.jdbc2.AbstractJdbc2Statement; import org.postgresql.test.TestUtil; import java.sql.*; @@ -505,6 +506,30 @@ public class StatementTest extends TestCase } } + /** + * Test executes two queries one after another. + * The first one has timeout of 1ms, and the second one does not. + * The timeout of the first query should not impact the second one. + * @throws SQLException + */ + public void testShortQueryTimeout() throws SQLException + { + long deadLine = System.currentTimeMillis() + 10000; + Statement stmt = con.createStatement(); + ((AbstractJdbc2Statement) stmt).setQueryTimeoutMs(1); + Statement stmt2 = con.createStatement(); + while(System.currentTimeMillis() < deadLine) { + try + { + stmt.execute("select 1"); + } catch (SQLException e) + { + // ignore "statement cancelled" + } + stmt2.executeQuery("select 1"); + } + } + public void testSetQueryTimeoutWithSleep() throws SQLException, InterruptedException { // check that the timeout starts ticking at execute, not at the [parent: ad7fc43c9627]