Java Code Examples for java.sql.Statement#cancel()

The following examples show how to use java.sql.Statement#cancel() . 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: RDBArchiveReader.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void cancel()
{
    synchronized (cancellable_statements)
    {
        for (Statement statement : cancellable_statements)
        {
            try
            {
                // Note that
                //    statement.getConnection().close()
                // does NOT stop an ongoing Oracle query!
                // Only this seems to do it:
                statement.cancel();
            }
            catch (Exception ex)
            {
                logger.log(Level.WARNING, "Failed to cancel statement " + statement, ex);
            }
        }
        cancellable_statements.clear();
    }
}
 
Example 2
Source File: StatementAdapterTest.java    From sharding-jdbc-1.5.1 with Apache License 2.0 5 votes vote down vote up
@Test
public void assertCancel() throws SQLException {
    for (Statement each : statements.values()) {
        each.executeQuery(sql);
        each.cancel();
    }
}
 
Example 3
Source File: Database.java    From hop with Apache License 2.0 5 votes vote down vote up
/**
 * Cancel an open/running SQL statement
 *
 * @param statement the statement to cancel
 * @throws HopDatabaseException
 */
public void cancelStatement( Statement statement ) throws HopDatabaseException {
  try {
    if ( statement != null ) {
      statement.cancel();
    }
    if ( log.isDebug() ) {
      log.logDebug( "Statement canceled!" );
    }
  } catch ( SQLException ex ) {
    throw new HopDatabaseException( "Error cancelling statement", ex );
  }
}
 
Example 4
Source File: QueryCancelTestHelper.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Cancel the query either using JDBC {@link Statement#cancel()} method or by
 * using {@link GfxdSystemProcedures#CANCEL_STATEMENT(String)} system
 * procedure. </br> If system procedure is used, will also make sure that the
 * given statement is present in sessions VTI.
 */
private void cancelQuery(final Statement stmt, final String testKey,
    boolean useCancelProc) throws SQLException {
  if (!useCancelProc) {
    // cancel using JDBC Statement#cancel
    stmt.cancel();
  } else {
    // cancel using system procedure
    String stmtUUID = getStatementUUIDfromBBMap(testKey);
    getLogWriter().info("UUID for " + testKey + "=" + stmtUUID);
    Connection c = TestUtil.getConnection();
    Statement s = c.createStatement();

    // make sure that the sessions VTI contains the statement to be cancelled
    ResultSet rs = s
        .executeQuery("select current_statement_UUID, CURRENT_STATEMENT from sys.sessions");
    boolean UUIDfound = false;
    while (rs.next()) {
      if (rs.getString(1).equals(stmtUUID)) {
        UUIDfound = true;
      } else {
        getLogWriter().info("UUID in the sessions is :" + rs.getString(1)
            + "and statement text is : " + rs.getString(2));
      }
    }
    assertTrue("Statement UUID " + stmtUUID
        + " is not present in sessions VTI", UUIDfound);
    rs.close();

    String cancelStmt = "CALL SYS.CANCEL_STATEMENT(?)";
    CallableStatement cs = c.prepareCall(cancelStmt);
    cs.setString(1, stmtUUID);
    cs.execute();
  }
}
 
Example 5
Source File: StatementAdapterTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertCancel() throws SQLException {
    for (Statement each : statements.values()) {
        each.executeQuery(sql);
        each.cancel();
    }
}
 
Example 6
Source File: QueryCancelTestHelper.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Cancel the query either using JDBC {@link Statement#cancel()} method or by
 * using {@link GfxdSystemProcedures#CANCEL_STATEMENT(String)} system
 * procedure. </br> If system procedure is used, will also make sure that the
 * given statement is present in sessions VTI.
 */
private void cancelQuery(final Statement stmt, final String testKey,
    boolean useCancelProc) throws SQLException {
  if (!useCancelProc) {
    // cancel using JDBC Statement#cancel
    stmt.cancel();
  } else {
    // cancel using system procedure
    String stmtUUID = getStatementUUIDfromBBMap(testKey);
    getLogWriter().info("UUID for " + testKey + "=" + stmtUUID);
    Connection c = TestUtil.getConnection();
    Statement s = c.createStatement();

    // make sure that the sessions VTI contains the statement to be cancelled
    ResultSet rs = s
        .executeQuery("select current_statement_UUID, CURRENT_STATEMENT from sys.sessions");
    boolean UUIDfound = false;
    while (rs.next()) {
      if (rs.getString(1).equals(stmtUUID)) {
        UUIDfound = true;
      } else {
        getLogWriter().info("UUID in the sessions is :" + rs.getString(1)
            + "and statement text is : " + rs.getString(2));
      }
    }
    assertTrue("Statement UUID " + stmtUUID
        + " is not present in sessions VTI", UUIDfound);
    rs.close();

    String cancelStmt = "CALL SYS.CANCEL_STATEMENT(?)";
    CallableStatement cs = c.prepareCall(cancelStmt);
    cs.setString(1, stmtUUID);
    cs.execute();
  }
}
 
Example 7
Source File: StatementWrapper.java    From Oceanus with Apache License 2.0 5 votes vote down vote up
@Override
public void cancel() throws SQLException {
	for (Statement stmt : statements) {
		stmt.cancel();
	}

}
 
Example 8
Source File: CsvTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a command that cancels a statement. */
private Callable<Void> cancel(final Statement statement) {
  return () -> {
    statement.cancel();
    return null;
  };
}
 
Example 9
Source File: Database.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Cancel an open/running SQL statement
 *
 * @param statement the statement to cancel
 * @throws KettleDatabaseException
 */
public void cancelStatement( Statement statement ) throws KettleDatabaseException {
  try {
    if ( statement != null ) {
      statement.cancel();
    }
    if ( log.isDebug() ) {
      log.logDebug( "Statement canceled!" );
    }
  } catch ( SQLException ex ) {
    throw new KettleDatabaseException( "Error cancelling statement", ex );
  }
}
 
Example 10
Source File: AbstractStatementAdapter.java    From sharding-jdbc-1.5.1 with Apache License 2.0 4 votes vote down vote up
@Override
public final void cancel() throws SQLException {
    for (Statement each : getRoutedStatements()) {
        each.cancel();
    }
}
 
Example 11
Source File: My_JdbcHandler.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
protected boolean closeStreaming(Statement stmt, ResultSet rs) throws SQLException {
    stmt.cancel();
    return false;
}
 
Example 12
Source File: SWStatementTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Test
public void testPreparedStatementConfig() throws SQLException {
    Statement statement = swConnection.createStatement();
    statement.cancel();
    statement.getUpdateCount();
    statement.setFetchDirection(1);
    statement.getFetchDirection();
    statement.getResultSetConcurrency();
    statement.getResultSetType();
    statement.isClosed();
    statement.setPoolable(false);
    statement.isPoolable();
    statement.getWarnings();
    statement.clearWarnings();
    statement.setCursorName("test");
    statement.setMaxFieldSize(11);
    statement.getMaxFieldSize();
    statement.setMaxRows(10);
    statement.getMaxRows();
    statement.setEscapeProcessing(true);
    statement.setFetchSize(1);
    statement.getFetchSize();
    statement.setQueryTimeout(1);
    statement.getQueryTimeout();
    Connection connection = statement.getConnection();

    statement.execute("SELECT * FROM test");
    statement.getMoreResults();
    statement.getMoreResults(1);
    statement.getResultSetHoldability();
    statement.getResultSet();

    statement.close();
    verify(mysqlStatement).getUpdateCount();
    verify(mysqlStatement).getMoreResults();
    verify(mysqlStatement).setFetchDirection(anyInt());
    verify(mysqlStatement).getFetchDirection();
    verify(mysqlStatement).getResultSetType();
    verify(mysqlStatement).isClosed();
    verify(mysqlStatement).setPoolable(anyBoolean());
    verify(mysqlStatement).getWarnings();
    verify(mysqlStatement).clearWarnings();
    verify(mysqlStatement).setCursorName(anyString());
    verify(mysqlStatement).setMaxFieldSize(anyInt());
    verify(mysqlStatement).getMaxFieldSize();
    verify(mysqlStatement).setMaxRows(anyInt());
    verify(mysqlStatement).getMaxRows();
    verify(mysqlStatement).setEscapeProcessing(anyBoolean());
    verify(mysqlStatement).getResultSetConcurrency();
    verify(mysqlStatement).getResultSetConcurrency();
    verify(mysqlStatement).getResultSetType();
    verify(mysqlStatement).getMoreResults(anyInt());
    verify(mysqlStatement).setFetchSize(anyInt());
    verify(mysqlStatement).getFetchSize();
    verify(mysqlStatement).getQueryTimeout();
    verify(mysqlStatement).setQueryTimeout(anyInt());
    verify(mysqlStatement).getResultSet();
    assertThat(connection, CoreMatchers.<Connection>is(swConnection));

    TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
    assertThat(spans.size(), is(1));
    assertDBSpan(spans.get(0), "Mysql/JDBI/Statement/execute", "SELECT * FROM test");
}