Example usage for java.sql SQLTransientException SQLTransientException

List of usage examples for java.sql SQLTransientException SQLTransientException

Introduction

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

Prototype

public SQLTransientException(Throwable cause) 

Source Link

Document

Constructs a SQLTransientException object with a given cause.

Usage

From source file:com.github.adejanovski.cassandra.jdbc.CassandraStatement.java

private void doExecute(String cql) throws SQLException {

    List<ResultSetFuture> futures = new ArrayList<ResultSetFuture>();
    try {/*www. ja v a2 s.  c  om*/
        String[] cqlQueries = cql.split(semiColonRegex);
        if (cqlQueries.length > 1 && !(cql.trim().toLowerCase().startsWith("begin")
                && cql.toLowerCase().contains("batch") && cql.toLowerCase().contains("apply"))) {
            // several statements in the query to execute asynchronously               

            ArrayList<com.datastax.driver.core.ResultSet> results = Lists.newArrayList();
            if (cqlQueries.length > MAX_ASYNC_QUERIES * 1.1) {
                // Protect the cluster from receiving too many queries at once and force the dev to split the load
                throw new SQLNonTransientException("Too many queries at once (" + cqlQueries.length
                        + "). You must split your queries into more batches !");
            }
            StringBuilder prevCqlQuery = new StringBuilder();
            for (String cqlQuery : cqlQueries) {
                if ((cqlQuery.contains("'") && ((StringUtils.countMatches(cqlQuery, "'") % 2 == 1
                        && prevCqlQuery.length() == 0)
                        || (StringUtils.countMatches(cqlQuery, "'") % 2 == 0 && prevCqlQuery.length() > 0)))
                        || (prevCqlQuery.toString().length() > 0 && !cqlQuery.contains("'"))) {
                    prevCqlQuery.append(cqlQuery + ";");
                } else {
                    prevCqlQuery.append(cqlQuery);
                    if (logger.isTraceEnabled() || this.connection.debugMode)
                        logger.debug("CQL:: " + prevCqlQuery.toString());
                    SimpleStatement stmt = new SimpleStatement(prevCqlQuery.toString());
                    stmt.setConsistencyLevel(this.connection.defaultConsistencyLevel);
                    stmt.setFetchSize(this.fetchSize);
                    ResultSetFuture resultSetFuture = this.connection.getSession().executeAsync(stmt);
                    futures.add(resultSetFuture);
                    prevCqlQuery = new StringBuilder();
                }
            }

            //ListenableFuture<List<com.datastax.driver.core.ResultSet>> res = Futures.allAsList(futures);

            for (ResultSetFuture future : futures) {
                com.datastax.driver.core.ResultSet rows = future.getUninterruptibly();
                results.add(rows);
            }

            currentResultSet = new CassandraResultSet(this, results);

        } else {
            // Only one statement to execute so we go synchronous
            if (logger.isTraceEnabled() || this.connection.debugMode)
                logger.debug("CQL:: " + cql);
            SimpleStatement stmt = new SimpleStatement(cql);
            stmt.setConsistencyLevel(this.connection.defaultConsistencyLevel);
            stmt.setFetchSize(this.fetchSize);
            currentResultSet = new CassandraResultSet(this, this.connection.getSession().execute(stmt));
        }
    } catch (Exception e) {

        for (ResultSetFuture future : futures) {
            try {
                future.cancel(true);
            } catch (Exception e1) {

            }
        }
        throw new SQLTransientException(e);
    }

}