Java Code Examples for java.sql.SQLTimeoutException

The following examples show how to use java.sql.SQLTimeoutException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
    SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
    SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 2
Source Project: jTDS   Source File: TdsCore.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Waits for the first byte of the server response.
 *
 * @param timeOut the timeout period in seconds or 0
 */
private void wait(int timeOut) throws IOException, SQLException {
    Object timer = null;
    try {
        if (timeOut > 0) {
            // Start a query timeout timer
            timer = TimerThread.getInstance().setTimer(timeOut * 1000,
                    new TimerThread.TimerListener() {
                        public void timerExpired() {
                            TdsCore.this.cancel(true);
                        }
                    });
        }
        in.peek();
    } finally {
        if (timer != null) {
            if (!TimerThread.getInstance().cancelTimer(timer)) {
                throw new SQLTimeoutException(
                      Messages.get("error.generic.timeout"), "HYT00");
            }
        }
    }
}
 
Example 3
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
    SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
    SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 4
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
    SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
    SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 5
Source Project: morf   Source File: DatabaseExceptionHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * <p>Checks if the throwable was caused by timeout exception.</p>
 * <b>This method has been tested for Oracle and MySQL only and might not work
 * for other DB engines.</b>
 *
 * @param throwable to check
 * @return true if the throwable is caused by a timeout, false otherwise
 */
public boolean isCausedByTimeoutException(Throwable throwable) {
  // Valid test for Oracle timeout exception and some (not all!) MySQL
  // exceptions.
  if (ExceptionUtils.indexOfType(throwable, SQLTimeoutException.class) != -1) {
    return true;
  }
  // MySQL database has two timeout exceptions in two packages. One of them
  // doesn't extend SQLTimeoutException but only SQLException. It is therefore
  // necessary to do ugly name check...
  for (Throwable causeThrowable : ExceptionUtils.getThrowables(throwable)) {
    if (MYSQL_TIMEOUT_EXCEPTION_NAME.equals(causeThrowable.getClass().getSimpleName())) {
      return true;
    }
  }
  return false;
}
 
Example 6
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
    SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
    SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 7
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using traditional while loop
 */
@Test
public void test12() {
    SQLTimeoutException ex = new SQLTimeoutException("Exception 1", t1);
    SQLTimeoutException ex1 = new SQLTimeoutException("Exception 2");
    SQLTimeoutException ex2 = new SQLTimeoutException("Exception 3", t2);
    ex.setNextException(ex1);
    ex.setNextException(ex2);
    int num = 0;
    SQLException sqe = ex;
    while (sqe != null) {
        assertTrue(msgs[num++].equals(sqe.getMessage()));
        Throwable c = sqe.getCause();
        while (c != null) {
            assertTrue(msgs[num++].equals(c.getMessage()));
            c = c.getCause();
        }
        sqe = sqe.getNextException();
    }
}
 
Example 8
Source Project: ignite   Source File: JdbcThinConnectionTimeoutSelfTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testUrlQueryTimeoutProperty() throws Exception {
    try (final Connection conn = DriverManager.getConnection(URL + "?connectionTimeout=10000&queryTimeout=1")) {
        conn.setSchema('"' + DEFAULT_CACHE_NAME + '"');

        final Statement stmt = conn.createStatement();

        GridTestUtils.assertThrows(log, () -> {
            stmt.executeQuery("select sleep_func(3) from Integer;");

            return null;
        }, SQLTimeoutException.class, "The query was cancelled while executing.");

        stmt.execute("select 1");
    }
}
 
Example 9
Source Project: core-ng-project   Source File: Connections.java    License: Apache License 2.0 6 votes vote down vote up
static void checkConnectionState(PoolItem<Connection> connection, SQLException e) {
    String state = e.getSQLState();
    // for state starts with "08"
    // refer to com.mysql.jdbc.integration.c3p0.MysqlConnectionTester, and standard sql state list:
    // http://dev.mysql.com/doc/connector-j/en/connector-j-reference-error-sqlstates.html
    //
    // for state: S1009
    // refer to com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException() and com.mysql.cj.exceptions.MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT
    // MySQL jdbc connector will translate "statement is closed" and other errors to "S1009"
    if (state != null && (state.startsWith("08") || "S1009".equals(state))) {
        connection.broken = true;
    }

    // if query timeout, com.mysql.cj.CancelQueryTaskImpl sends "KILL QUERY" to mysql server in cancel task thread
    // and as result, in current thread it triggers com.mysql.cj.jdbc.ConnectionImpl.handleCleanup with whyCleanedUp = com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure, The last packet successfully received from the server was x milliseconds ago.
    // so for query timeout, the connection needs to be marked as broken and close,
    // otherwise when other thread retrieves this connection, will encounter "statement is already closed" exception
    if (e instanceof SQLTimeoutException) {
        connection.broken = true;
    }
}
 
Example 10
Source Project: Tomcat8-Source-Read   Source File: TestValidationQueryTimeout.java    License: MIT License 5 votes vote down vote up
@Test
public void testLongValidationQueryTime() throws Exception {
    // use our mock driver
    Connection con = this.datasource.getConnection();
    Statement stmt = null;
    long start = 0, end = 0;
    try {
        stmt = con.createStatement();
        // set the query timeout to 2 sec
        //  this keeps this test from slowing things down too much
        stmt.setQueryTimeout(2);
        // assert that our long query takes longer than one second to run
        //  this is a requirement for other tests to run properly
        start = System.currentTimeMillis();
        stmt.execute(longQuery);
    } catch (SQLTimeoutException ex) {

    } catch (SQLException x) {
        Assert.fail("We should have got a timeout exception.");
    } finally {
        end = System.currentTimeMillis();

        if (stmt != null) { stmt.close(); }
        if (con != null) { con.close(); }

        Assert.assertTrue(start != 0 && end != 0);
        //we're faking it
        //Assert.assertTrue((end - start) > 1000);
    }
}
 
Example 11
Source Project: Tomcat8-Source-Read   Source File: TestValidationQueryTimeout.java    License: MIT License 5 votes vote down vote up
@Override
public boolean execute(String sql) throws SQLException {
    if (longQuery.equals(sql)) {
        throw new SQLTimeoutException();
    } else {
        return super.execute(sql);
    }
}
 
Example 12
Source Project: ignite   Source File: JdbcThinStatementTimeoutSelfTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Setting timeout that is greater than query execution time. <code>SQLTimeoutException</code> is expected.
 *
 * @throws Exception If failed.
 */
@Test
public void testQueryTimeout() throws Exception {
    stmt.setQueryTimeout(2);

    GridTestUtils.assertThrows(log, () -> {
        stmt.executeQuery("select sleep_func(10) from Integer;");

        return null;
    }, SQLTimeoutException.class, "The query was cancelled while executing.");
}
 
Example 13
/**
 * Serialize a SQLTimeoutException and make sure you can read it back properly
 */
@Test
public void test10() throws Exception {
    SQLTimeoutException e =
            new SQLTimeoutException(reason, state, errorCode, t);
    SQLTimeoutException ex1 =
            createSerializedException(e);
    assertTrue(reason.equals(ex1.getMessage())
            && ex1.getSQLState().equals(state)
            && cause.equals(ex1.getCause().toString())
            && ex1.getErrorCode() == errorCode);
}
 
Example 14
/**
 * Blocks until an existing connection is available.
 *
 * @return An existing connection.
 * @throws SQLException If the connection is invalid.
 */
private Connection acquireConnection() throws SQLException {
  Connection connection;

  try {
    connection = connectionQueue.poll(MAX_TIMEOUT_MS, TimeUnit.MILLISECONDS);
  } catch (InterruptedException e) {
    throw new SQLTimeoutException();
  }

  if (connection == null) releaseConnection(connection = newConnection());
  if (connection.isClosed()) throw new SQLTimeoutException();

  return connection;
}
 
Example 15
/**
 * Create SQLTimeoutException with Throwable
 */
@Test
public void test9() {
    SQLTimeoutException ex = new SQLTimeoutException(t);
    assertTrue(ex.getMessage().equals(cause)
            && ex.getSQLState() == null
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example 16
/**
 * Create SQLTimeoutException with no-arg constructor
 */
@Test
public void test1() {
    SQLTimeoutException ex = new SQLTimeoutException();
    assertTrue(ex.getMessage() == null
            && ex.getSQLState() == null
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}
 
Example 17
/**
 * Create SQLTimeoutException with message, and SQLState
 */
@Test
public void test3() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}
 
Example 18
/**
 * Create SQLTimeoutException with message, SQLState, and error code
 */
@Test
public void test4() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && ex.getCause() == null
            && ex.getErrorCode() == errorCode);
}
 
Example 19
/**
 * Create SQLTimeoutException with message, and Throwable
 */
@Test
public void test7() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState() == null
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example 20
/**
 * Create SQLTimeoutException with null Throwable
 */
@Test
public void test8() {
    SQLTimeoutException ex = new SQLTimeoutException((Throwable)null);
    assertTrue(ex.getMessage() == null
            && ex.getSQLState() == null
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}
 
Example 21
/**
 * Create SQLTimeoutException with Throwable
 */
@Test
public void test9() {
    SQLTimeoutException ex = new SQLTimeoutException(t);
    assertTrue(ex.getMessage().equals(cause)
            && ex.getSQLState() == null
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example 22
/**
 * Create SQLTimeoutException with no-arg constructor
 */
@Test
public void test1() {
    SQLTimeoutException ex = new SQLTimeoutException();
    assertTrue(ex.getMessage() == null
            && ex.getSQLState() == null
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}
 
Example 23
/**
 * Create SQLTimeoutException with message, SQLState, and error code
 */
@Test
public void test4() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && ex.getCause() == null
            && ex.getErrorCode() == errorCode);
}
 
Example 24
/**
 * Serialize a SQLTimeoutException and make sure you can read it back properly
 */
@Test
public void test10() throws Exception {
    SQLTimeoutException e =
            new SQLTimeoutException(reason, state, errorCode, t);
    SQLTimeoutException ex1 =
            createSerializedException(e);
    assertTrue(reason.equals(ex1.getMessage())
            && ex1.getSQLState().equals(state)
            && cause.equals(ex1.getCause().toString())
            && ex1.getErrorCode() == errorCode);
}
 
Example 25
/**
 * Create SQLTimeoutException with message, and SQLState
 */
@Test
public void test3() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}
 
Example 26
/**
 * Create SQLTimeoutException with message, and SQLState
 */
@Test
public void test3() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}
 
Example 27
/**
 * Create SQLTimeoutException with message, SQLState, and error code
 */
@Test
public void test4() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && ex.getCause() == null
            && ex.getErrorCode() == errorCode);
}
 
Example 28
/**
 * Create SQLTimeoutException with message
 */
@Test
public void test2() {
    SQLTimeoutException ex = new SQLTimeoutException(reason);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState() == null
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}
 
Example 29
/**
 * Create SQLTimeoutException with message, and Throwable
 */
@Test
public void test7() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState() == null
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example 30
/**
 * Create SQLTimeoutException with message, and SQLState
 */
@Test
public void test3() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && ex.getCause() == null
            && ex.getErrorCode() == 0);
}