Skip to content

Commit

Permalink
test: add statement.getQueryTimeoutMs, so millisecond-scale timeouts …
Browse files Browse the repository at this point in the history
…can be tested
  • Loading branch information
vlsi committed Oct 31, 2015
1 parent ad7fc43 commit 892d7af
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
34 changes: 29 additions & 5 deletions org/postgresql/jdbc2/AbstractJdbc2Statement.java
Expand Up @@ -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;
Expand Down Expand Up @@ -757,6 +757,30 @@ public void setEscapeProcessing(boolean enable) throws SQLException
* @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;
Expand All @@ -768,14 +792,14 @@ public int getQueryTimeout() throws SQLException
* @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;
}

/**
Expand Down Expand Up @@ -3475,7 +3499,7 @@ public void run()
}
};

connection.addTimerTask(cancelTimerTask, timeout * 1000);
connection.addTimerTask(cancelTimerTask, timeout);
}

private synchronized void killTimerTask()
Expand Down
25 changes: 25 additions & 0 deletions org/postgresql/test/jdbc2/StatementTest.java
Expand Up @@ -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.*;
Expand Down Expand Up @@ -505,6 +506,30 @@ public void run()
}
}

/**
* 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
Expand Down

0 comments on commit 892d7af

Please sign in to comment.