Example usage for java.sql SQLNonTransientException SQLNonTransientException

List of usage examples for java.sql SQLNonTransientException SQLNonTransientException

Introduction

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

Prototype

public SQLNonTransientException(Throwable cause) 

Source Link

Document

Constructs a SQLNonTransientException 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 {//from w  w w. j a  v  a 2  s.com
        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);
    }

}

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

public ResultSet executeQuery(String query) throws SQLException {
    checkNotClosed();/*from  w  w  w. j  ava  2 s  .  co  m*/
    doExecute(query);
    if (currentResultSet == null)
        throw new SQLNonTransientException(NO_RESULTSET);
    return currentResultSet;
}

From source file:org.apache.cassandra.cql.jdbc.HandleObjects.java

private static final SQLException makeBadMapping(Class<?> badclass, String javatype, String jdbctype) {
    return new SQLNonTransientException(String.format(BAD_MAPPING, badclass, javatype, jdbctype));
}

From source file:org.apache.cassandra.cql.jdbc.HandleObjects.java

public static final ByteBuffer makeBytes(Object object, int baseType, int scaleOrLength) throws SQLException {
    Class<? extends Object> objectClass = object.getClass();
    boolean isCollection = (Collection.class.isAssignableFrom(objectClass));
    int targetSqlType = isCollection ? Types.OTHER : baseType;
    // Type check first
    switch (targetSqlType) {
    case Types.TINYINT:
        // Only Numeric classes, Strings and Booleans are supported for transformation to TINYINT
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "TINYINT");
        break;/*from   w  w  w . j ava2 s. co  m*/

    case Types.SMALLINT:
        // Only Numeric classes, Strings and Booleans are supported for transformation to SMALLINT
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "SMALLINT");
        break;

    case Types.INTEGER:
        // Only Numeric classes, Strings and Booleans are supported for transformation to INTEGER
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "INTEGER");
        break;

    case Types.BIGINT:
        // Only Numeric classes, Strings and Booleans are supported for transformation to BIGINT
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "BIGINT");
        break;

    case Types.REAL:
    case Types.FLOAT:
    case Types.DOUBLE:
    case Types.DECIMAL:
        // Only Numeric classes Strings and Booleans are supported for transformation to REAL,FLOAT,DOUBLE,DECIMAL
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "the floating point types");
        break;

    case Types.NUMERIC:
        //NB This as a special case variation for Cassandra!! NUMERIC is transformed to java BigInteger (varint CQL type)
        //
        // Only Numeric classes Strings and Booleans are supported for transformation to NUMERIC
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "NUMERIC");
        break;

    case Types.BIT:
        // Only Numeric classes Strings and Booleans are supported for transformation to BIT
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "BIT");
        break;

    case Types.BOOLEAN:
        // Only Numeric classes Strings and Booleans are supported for transformation to BOOLEAN
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "BOOLEAN");
        break;

    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
    case Types.NVARCHAR:
    case Types.LONGNVARCHAR:
        if (!objectClass.isAssignableFrom(String.class))
            throw makeBadMapping(objectClass, "String", "the various VARCHAR types");
        break;

    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        if (!(objectClass.isAssignableFrom(ByteBuffer.class) || objectClass.getSimpleName().equals("byte[]")))
            throw makeBadMapping(objectClass, "ByteBuffer or byte[]", "the BINARY Types");
        break;

    case Types.DATE:
        if (!(objectClass == String.class || objectClass == java.util.Date.class || objectClass == Date.class
                || objectClass == Timestamp.class))
            throw makeBadMapping(objectClass, "String, Date(java and sql) or Timestamp types", "DATE");
        break;

    case Types.TIME:
        if (!(objectClass == String.class || objectClass == java.util.Date.class || objectClass == Time.class
                || objectClass == Timestamp.class))
            throw makeBadMapping(objectClass, "String, Date (java), Time or Timestamp types", "TIME");
        break;

    case Types.TIMESTAMP:
        if (!(objectClass == String.class || objectClass == java.util.Date.class || objectClass == Date.class
                || objectClass == Timestamp.class))
            throw makeBadMapping(objectClass, "String, Date(java and sql) or Timestamp types", "TIMESTAMP");
        break;

    case Types.DATALINK:
        if (objectClass != URL.class)
            throw makeBadMapping(objectClass, "a URL type", "DATALINK");
        break;

    case Types.JAVA_OBJECT:
        break;

    case Types.OTHER:
        // Only Collection classes for transformation to OTHER
        if (!(List.class.isAssignableFrom(object.getClass()) || Set.class.isAssignableFrom(object.getClass())
                || Map.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "OTHER");
        break;

    case Types.ROWID:
        if (objectClass != RowId.class)
            throw makeBadMapping(objectClass, "a RowId type", "ROWID");
        break;

    default:
        throw new SQLNonTransientException("Unsupported transformation to Jdbc Type: " + targetSqlType);
    }

    // see if we can map to an supported Type
    switch (targetSqlType) {
    case Types.BIT:
        BigInteger bitvalue = objectToBITorTINYINTorSMALLINTorNUMERIC(objectClass, object);
        assert bitvalue != null;
        return JdbcInteger.instance.decompose((bitvalue == BigInteger.ZERO) ? BigInteger.ZERO : BigInteger.ONE);

    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.NUMERIC:
        BigInteger varint = objectToBITorTINYINTorSMALLINTorNUMERIC(objectClass, object);
        assert varint != null;
        return JdbcInteger.instance.decompose(varint);

    case Types.INTEGER:
        Integer value = objectToINTEGER(objectClass, object);
        assert value != null;
        return JdbcInt32.instance.decompose(value);

    case Types.BIGINT:
        Long longvalue = objectToBIGINT(objectClass, object);
        assert longvalue != null;
        return JdbcLong.instance.decompose(longvalue);

    case Types.BOOLEAN:
        Boolean bool = objectToBOOLEAN(objectClass, object);
        assert bool != null;
        return JdbcBoolean.instance.decompose(bool);

    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
    case Types.NVARCHAR:
    case Types.LONGNVARCHAR:
        return ByteBufferUtil.bytes((String) object);

    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        if (objectClass.isAssignableFrom(ByteBuffer.class)) {
            return ((ByteBuffer) object);
        } else if (objectClass.getSimpleName().equals("byte[]")) {
            return ByteBuffer.wrap((byte[]) object);
        } else
            return null; // this should not happen

    case Types.DATE:
    case Types.TIME:
    case Types.TIMESTAMP:
        Long millis = objectToDATEorTIMEorTIMESTAMP(objectClass, object);
        assert millis != null;
        return JdbcLong.instance.decompose(millis);

    case Types.DATALINK:
        String urlAsString = ((URL) object).toExternalForm();
        return JdbcUTF8.instance.decompose(urlAsString);

    case Types.JAVA_OBJECT:
        return javaObject(object);

    case Types.OTHER:
        if (List.class.isAssignableFrom(objectClass)) {
            return handleAsList(objectClass, object);
        } else if (Set.class.isAssignableFrom(objectClass)) {
            return handleAsSet(objectClass, object);
        } else if (Map.class.isAssignableFrom(objectClass)) {
            return handleAsMap(objectClass, object);
        } else
            return null;

    case Types.ROWID:
        byte[] bytes = ((RowId) object).getBytes();
        return ByteBuffer.wrap(bytes);

    default:
        LOG.warn("Unhandled JDBC type: " + targetSqlType);
        return null;
    }
}