Example usage for java.sql Statement cancel

List of usage examples for java.sql Statement cancel

Introduction

In this page you can find the example usage for java.sql Statement cancel.

Prototype

void cancel() throws SQLException;

Source Link

Document

Cancels this Statement object if both the DBMS and driver support aborting an SQL statement.

Usage

From source file:mondrian.util.UtilCompatibleJdk14.java

public void cancelStatement(Statement stmt) {
    try {//from  w  ww.  j a  va 2 s .  co m
        stmt.cancel();
    } catch (Throwable t) {
        // We can't call stmt.isClosed(); the method doesn't exist until
        // JDK 1.6. So, mask out the error.
        if (t.getMessage().equals("org.apache.commons.dbcp.DelegatingStatement is closed.")) {
            return;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(MondrianResource.instance().ExecutionStatementCleanupException.ex(t.getMessage(), t),
                    t);
        }
    }
}

From source file:mondrian.util.UtilCompatibleJdk15.java

public void cancelStatement(Statement stmt) {
    try {/*from  ww  w  .  j  a  v  a 2  s .c  o m*/
        stmt.cancel();
    } catch (Throwable t) {
        // We can't call stmt.isClosed(); the method doesn't exist until
        // JDK 1.6. So, mask out the error.

        // Also, we MUST catch all throwables. Some drivers (ie. Hive)
        // will choke on canceled queries and throw a OutOfMemoryError.
        // We can't protect ourselves against this. That's a bug on their
        // side.
        if (t.getMessage().equals("org.apache.commons.dbcp.DelegatingStatement is closed.")) {
            return;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(MondrianResource.instance().ExecutionStatementCleanupException.ex(t.getMessage(), t),
                    t);
        }
    }
}

From source file:mondrian.util.UtilCompatibleJdk16.java

@Override
public void cancelAndCloseStatement(Statement stmt) {
    try {/* w  w w.j  a  va 2s  .  c om*/
        // A call to statement.isClosed() would be great here, but in
        // reality, some drivers will block on this check and the
        // cancellation will never happen.  This is due to the
        // non-thread-safe nature of JDBC and driver implementations. If a
        // thread is currently using the statement, calls to isClosed() are
        // synchronized internally and won't return until the query
        // completes.
        stmt.cancel();
    } catch (SQLException e) {
        // We crush this one. A lot of drivers will complain if cancel() is
        // called on a closed statement, but a call to isClosed() isn't
        // thread safe and might block. See above.
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(MondrianResource.instance().ExecutionStatementCleanupException.ex(e.getMessage(), e),
                    e);
        }
    }
    try {
        // We used to call Statement.isClosed, but DBCP gave error:
        //   java.lang.IllegalAccessError:
        //   org.apache.commons.dbcp.DelegatingStatement.isClosed()Z
        // JDBC says it is OK to call close on a closed statement, so
        // why check?
        stmt.close();
    } catch (SQLException e) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(MondrianResource.instance().ExecutionStatementCleanupException.ex(e.getMessage(), e),
                    e);
        }
    }
}

From source file:com.fer.hr.service.olap.ThinQueryService.java

CellSet executeInternalQuery(ThinQuery query) throws Exception {
    String runId = "RUN#:" + ID_GENERATOR.getAndIncrement();
    QueryContext queryContext = context.get(query.getName());

    if (queryContext == null) {
        queryContext = new QueryContext(Type.OLAP, query);
        this.context.put(query.getName(), queryContext);
    }/*from   w w w.  j  a va2  s .  c o  m*/

    OlapConnection con = olapDiscoverService.getNativeConnection(query.getCube().getConnection());
    if (StringUtils.isNotBlank(query.getCube().getCatalog())) {
        con.setCatalog(query.getCube().getCatalog());
    }

    if (queryContext.contains(ObjectKey.STATEMENT)) {
        Statement s = queryContext.getStatement();
        s.cancel();
        s.close();
        s = null;
        queryContext.remove(ObjectKey.STATEMENT);
    }

    OlapStatement stmt = con.createStatement();
    queryContext.store(ObjectKey.STATEMENT, stmt);

    query = updateQuery(query);

    try {
        String mdx = query.getParameterResolvedMdx();
        log.info(runId + "\tType:" + query.getType() + ":\n" + mdx);

        CellSet cs = stmt.executeOlapQuery(mdx);
        queryContext.store(ObjectKey.RESULT, cs);
        if (query != null) {
            queryContext.store(ObjectKey.QUERY, query);
        }
        return cs;
    } finally {
        stmt.close();
        queryContext.remove(ObjectKey.STATEMENT);
    }
}

From source file:com.fer.hr.service.olap.ThinQueryService.java

public void cancel(String name) throws SQLException {
    if (context.containsKey(name)) {
        QueryContext queryContext = context.get(name);
        if (queryContext.contains(ObjectKey.STATEMENT)) {
            Statement stmt = queryContext.getStatement();
            if (stmt != null && !stmt.isClosed()) {
                stmt.cancel();
                stmt.close();/*from   ww  w .java2 s  . co  m*/
            }
            stmt = null;
            queryContext.remove(ObjectKey.STATEMENT);
        }
    }
}

From source file:org.apache.hive.jdbc.TestJdbcDriver2.java

@Test
public void testCloseResultSet() throws Exception {
    Statement stmt = con.createStatement();

    // execute query, ignore exception if any
    ResultSet res = stmt.executeQuery("select * from " + tableName);
    // close ResultSet, ignore exception if any
    res.close();//  w  w  w . j a v a2 s.com
    // A statement should be open even after ResultSet#close
    assertFalse(stmt.isClosed());
    // A Statement#cancel after ResultSet#close should be a no-op
    try {
        stmt.cancel();
    } catch (SQLException e) {
        failWithExceptionMsg(e);
    }
    stmt.close();

    stmt = con.createStatement();
    // execute query, ignore exception if any
    res = stmt.executeQuery("select * from " + tableName);
    // close ResultSet, ignore exception if any
    res.close();
    // A Statement#execute after ResultSet#close should be fine too
    try {
        stmt.executeQuery("select * from " + tableName);
    } catch (SQLException e) {
        failWithExceptionMsg(e);
    }
    // A Statement#close after ResultSet#close should close the statement
    stmt.close();
    assertTrue(stmt.isClosed());
}

From source file:org.apache.hive.jdbc.TestJdbcDriver2.java

/**
 * Test the cancellation of a query that is running.
 * We spawn 2 threads - one running the query and
 * the other attempting to cancel.//www. j  a  va2 s. c om
 * We're using a dummy udf to simulate a query,
 * that runs for a sufficiently long time.
 * @throws Exception
 */
@Test
public void testQueryCancel() throws Exception {
    String udfName = SleepUDF.class.getName();
    Statement stmt1 = con.createStatement();
    stmt1.execute("create temporary function sleepUDF as '" + udfName + "'");
    stmt1.close();
    final Statement stmt = con.createStatement();
    // Thread executing the query
    Thread tExecute = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                System.out.println("Executing query: ");
                stmt.executeQuery(
                        "select sleepUDF(t1.under_col) as u0, t1.under_col as u1, " + "t2.under_col as u2 from "
                                + tableName + "t1 join " + tableName + " t2 on t1.under_col = t2.under_col");
                fail("Expecting SQLException");
            } catch (SQLException e) {
                // This thread should throw an exception
                assertNotNull(e);
                System.out.println(e.toString());
            }
        }
    });
    // Thread cancelling the query
    Thread tCancel = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                System.out.println("Cancelling query: ");
                stmt.cancel();
            } catch (Exception e) {
                // No-op
            }
        }
    });
    tExecute.start();
    tCancel.start();
    tExecute.join();
    tCancel.join();
    stmt.close();
}

From source file:org.pentaho.di.core.database.Database.java

/**
 * Cancel an open/running SQL statement/*from  w w w. ja v  a  2s. co  m*/
 *
 * @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);
    }
}