java.sql.SQLTimeoutException Java Examples

The following examples show how to use java.sql.SQLTimeoutException. 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
Source File: SQLTimeoutExceptionTests.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 File: SQLTimeoutExceptionTests.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #3
Source File: SQLTimeoutExceptionTests.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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
Source File: JdbcThinConnectionTimeoutSelfTest.java    From ignite with 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 #5
Source File: SQLTimeoutExceptionTests.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #6
Source File: TdsCore.java    From jTDS with 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 #7
Source File: SQLTimeoutExceptionTests.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 File: DatabaseExceptionHelper.java    From morf with 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 #9
Source File: Connections.java    From core-ng-project with 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 File: SQLTimeoutExceptionTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLTimeoutException with message, SQLState, errorCode, and Throwable
 */
@Test
public void test5() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == errorCode);
}
 
Example #11
Source File: SQLTimeoutExceptionTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using for-each loop
 */
@Test
public void test11() {
    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;
    for (Throwable e : ex) {
        assertTrue(msgs[num++].equals(e.getMessage()));
    }
}
 
Example #12
Source File: SQLTimeoutExceptionTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLTimeoutException and setting all objects to null
 */
@Test
public void test() {
    SQLTimeoutException e = new SQLTimeoutException(null,
            null, errorCode, null);
    assertTrue(e.getMessage() == null && e.getSQLState() == null
            && e.getCause() == null && e.getErrorCode() == errorCode);
}
 
Example #13
Source File: SQLExceptionSubclassTranslator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example #14
Source File: SQLTimeoutExceptionTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #15
Source File: SQLTimeoutExceptionTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLTimeoutException with message, SQLState, errorCode, and Throwable
 */
@Test
public void test5() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == errorCode);
}
 
Example #16
Source File: SQLTimeoutExceptionTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLTimeoutException with message, SQLState, and Throwable
 */
@Test
public void test6() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #17
Source File: SQLTimeoutExceptionTests.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLTimeoutException with message, SQLState, and Throwable
 */
@Test
public void test6() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #18
Source File: SQLTimeoutExceptionTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using for-each loop
 */
@Test
public void test11() {
    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;
    for (Throwable e : ex) {
        assertTrue(msgs[num++].equals(e.getMessage()));
    }
}
 
Example #19
Source File: QueryTimeoutIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryTimeout() throws Exception {
    int nRows = 30000;
    Connection conn;
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    conn = DriverManager.getConnection(getUrl(), props);
    conn.createStatement().execute(
            "CREATE TABLE " + TEST_TABLE_NAME + "(k BIGINT PRIMARY KEY, v VARCHAR)");
    PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + TEST_TABLE_NAME + " VALUES(?, 'AAAAAAAAAAAAAAAAAAAA')");
    for (int i = 1; i <= nRows; i++) {
        stmt.setLong(1, i);
        stmt.executeUpdate();
        if ((i % 2000) == 0) {
            conn.commit();
        }
    }
    conn.commit();
    conn.createStatement().execute("UPDATE STATISTICS " + TEST_TABLE_NAME);
    
    PhoenixStatement pstmt = conn.createStatement().unwrap(PhoenixStatement.class);
    pstmt.setQueryTimeout(1);
    long startTime = System.currentTimeMillis();
    try {
        ResultSet rs = pstmt.executeQuery("SELECT count(*) FROM " + TEST_TABLE_NAME);
        // Force lots of chunks so query is cancelled
        assertTrue(pstmt.getQueryPlan().getSplits().size() > 1000);
        rs.next();
        fail("Total time of query was " + (System.currentTimeMillis() - startTime) + " ms, but expected to be greater than 1000");
    } catch (SQLTimeoutException e) {
        long elapsedTimeMillis = System.currentTimeMillis() - startTime;
        assertEquals(SQLExceptionCode.OPERATION_TIMED_OUT.getErrorCode(), e.getErrorCode());
        assertTrue(elapsedTimeMillis > 1000);
    }
    conn.close();
}
 
Example #20
Source File: SQLTimeoutExceptionTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #21
Source File: SQLTimeoutExceptionTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #22
Source File: SQLTimeoutExceptionTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
Source File: SQLTimeoutExceptionTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLTimeoutException with message, SQLState, and Throwable
 */
@Test
public void test6() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #24
Source File: JdbcApiIT.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test(expected = SQLTimeoutException.class)
public void testTimeoutSpark() throws Exception {
    String sql = "select count(*) from a --splice-properties useSpark=true \n" +
            "natural join a a1 natural join a a2 natural join a a3";
    try(Statement s = conn.createStatement()){
        s.setQueryTimeout(2);
        ResultSet rs = s.executeQuery(sql);
    }
}
 
Example #25
Source File: JdbcAccessor.java    From pxf with Apache License 2.0 5 votes vote down vote up
/**
 * openForRead() implementation
 * Create query, open JDBC connection, execute query and store the result into resultSet
 *
 * @return true if successful
 * @throws SQLException if a database access error occurs
 * @throws SQLTimeoutException if a problem with the connection occurs
 * @throws ParseException if th SQL statement provided in PXF RequestContext is incorrect
 * @throws ClassNotFoundException if the JDBC driver was not found
 */
@Override
public boolean openForRead() throws SQLException, SQLTimeoutException, ParseException {
    if (statementRead != null && !statementRead.isClosed()) {
        return true;
    }

    Connection connection = super.getConnection();
    SQLQueryBuilder sqlQueryBuilder = new SQLQueryBuilder(context, connection.getMetaData(), getQueryText());

    // Build SELECT query
    if (quoteColumns == null) {
        sqlQueryBuilder.autoSetQuoteString();
    }
    else if (quoteColumns) {
        sqlQueryBuilder.forceSetQuoteString();
    }
    queryRead = sqlQueryBuilder.buildSelectQuery();
    LOG.trace("Select query: {}", queryRead);

    // Execute queries
    statementRead = connection.createStatement();
    statementRead.setFetchSize(fetchSize);

    if (queryTimeout != null) {
        LOG.debug("Setting query timeout to {} seconds", queryTimeout);
        statementRead.setQueryTimeout(queryTimeout);
    }
    resultSetRead = statementRead.executeQuery(queryRead);

    return true;
}
 
Example #26
Source File: SQLTimeoutExceptionTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #27
Source File: SQLTimeoutExceptionTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #28
Source File: SQLTimeoutExceptionTests.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validate that the ordering of the returned Exceptions is correct
 * using for-each loop
 */
@Test
public void test11() {
    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;
    for (Throwable e : ex) {
        assertTrue(msgs[num++].equals(e.getMessage()));
    }
}
 
Example #29
Source File: SQLTimeoutExceptionTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLTimeoutException with message, SQLState, and Throwable
 */
@Test
public void test6() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == 0);
}
 
Example #30
Source File: SQLTimeoutExceptionTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create SQLTimeoutException with message, SQLState, errorCode, and Throwable
 */
@Test
public void test5() {
    SQLTimeoutException ex = new SQLTimeoutException(reason, state, errorCode, t);
    assertTrue(ex.getMessage().equals(reason)
            && ex.getSQLState().equals(state)
            && cause.equals(ex.getCause().toString())
            && ex.getErrorCode() == errorCode);
}