Search in sources :

Example 1 with FinalSQLString

use of com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString in project atlasdb by palantir.

the class BasicSQL method selectLightResultSetSpecifyingDBType.

protected AgnosticLightResultSet selectLightResultSetSpecifyingDBType(final Connection c, final FinalSQLString sql, Object[] vs, final DBType dbType, @Nullable Integer fetchSize) throws PalantirSqlException, PalantirInterruptedException {
    if (SqlLoggers.LOGGER.isTraceEnabled()) {
        SqlLoggers.LOGGER.trace("SQL light result set selection query: {}", sql.getQuery());
    }
    // $NON-NLS-1$
    final ResourceCreationLocation alrsCreationException = new ResourceCreationLocation("This is where the AgnosticLightResultSet wrapper was created");
    PreparedStatementVisitor<AgnosticLightResultSet> preparedStatementVisitor = ps -> {
        // $NON-NLS-1$
        final ResourceCreationLocation creationException = new ResourceCreationLocation("This is where the ResultsSet was created", alrsCreationException);
        final ResultSetVisitor<AgnosticLightResultSet> resultSetVisitor = rs -> {
            try {
                return new AgnosticLightResultSetImpl(rs, dbType, rs.getMetaData(), ps, // $NON-NLS-1$
                "selectList", sql, getSqlTimer(), creationException);
            } catch (Exception e) {
                closeSilently(rs);
                BasicSQLUtils.throwUncheckedIfSQLException(e);
                throw Throwables.throwUncheckedException(e);
            }
        };
        try {
            return runCancellably(ps, resultSetVisitor, sql, AutoClose.FALSE, fetchSize);
        } catch (Exception e) {
            closeSilently(ps);
            BasicSQLUtils.throwUncheckedIfSQLException(e);
            throw Throwables.throwUncheckedException(e);
        }
    };
    return wrapPreparedStatement(c, sql, vs, preparedStatementVisitor, // $NON-NLS-1$
    "selectList", // don't close the ps
    AutoClose.FALSE);
}
Also used : PalantirSqlException(com.palantir.exception.PalantirSqlException) FinalSQLString(com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString) Connection(java.sql.Connection) Throwables(com.palantir.common.base.Throwables) ObjectInputStream(java.io.ObjectInputStream) BigDecimal(java.math.BigDecimal) Future(java.util.concurrent.Future) ByteArrayInputStream(java.io.ByteArrayInputStream) PTExecutors(com.palantir.common.concurrent.PTExecutors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSet(java.sql.ResultSet) Map(java.util.Map) ResultSets(com.palantir.sql.ResultSets) Method(java.lang.reflect.Method) DateTimeFormat(org.joda.time.format.DateTimeFormat) NamedThreadFactory(com.palantir.common.concurrent.NamedThreadFactory) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) JdbcHandler(com.palantir.db.oracle.JdbcHandler) BlobHandler(com.palantir.db.oracle.JdbcHandler.BlobHandler) PreparedStatement(java.sql.PreparedStatement) VerboseSQLException(com.palantir.util.sql.VerboseSQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) ResourceCreationLocation(com.palantir.nexus.db.ResourceCreationLocation) PalantirInterruptedException(com.palantir.exception.PalantirInterruptedException) SqlLoggers(com.palantir.nexus.db.sql.monitoring.logger.SqlLoggers) ResultSetMetaData(java.sql.ResultSetMetaData) Proxy(java.lang.reflect.Proxy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Callable(java.util.concurrent.Callable) ThreadNamingCallable(com.palantir.common.concurrent.ThreadNamingCallable) ArrayList(java.util.ArrayList) Connections(com.palantir.sql.Connections) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) PreparedStatements(com.palantir.sql.PreparedStatements) ObjectOutputStream(java.io.ObjectOutputStream) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) DBType(com.palantir.nexus.db.DBType) Iterator(java.util.Iterator) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Validate(org.apache.commons.lang3.Validate) SqlTimer(com.palantir.nexus.db.monitoring.timer.SqlTimer) Statement(java.sql.Statement) InvocationHandler(java.lang.reflect.InvocationHandler) InputStream(java.io.InputStream) ResourceCreationLocation(com.palantir.nexus.db.ResourceCreationLocation) PalantirSqlException(com.palantir.exception.PalantirSqlException) VerboseSQLException(com.palantir.util.sql.VerboseSQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PalantirInterruptedException(com.palantir.exception.PalantirInterruptedException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with FinalSQLString

use of com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString in project atlasdb by palantir.

the class SQL method fromResultSet.

public AgnosticLightResultSet fromResultSet(PreparedStatement preparedStatement, ResultSet resultSet, DBType dbType, String queryString) {
    FinalSQLString sqlString = SQLString.getUnregisteredQuery(queryString);
    String timingModule = "visitResultSet";
    ResultSetMetaData metaData = ResultSets.getMetaData(resultSet);
    SqlTimer sqlTimer = getSqlTimer();
    // $NON-NLS-1$
    ResourceCreationLocation creationException = new ResourceCreationLocation("This is where the ResultsSet was created");
    return new AgnosticLightResultSetImpl(resultSet, dbType, metaData, preparedStatement, timingModule, sqlString, sqlTimer, creationException);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) FinalSQLString(com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString) ResourceCreationLocation(com.palantir.nexus.db.ResourceCreationLocation) SqlTimer(com.palantir.nexus.db.monitoring.timer.SqlTimer) FinalSQLString(com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString)

Example 3 with FinalSQLString

use of com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString in project atlasdb by palantir.

the class BasicSQL method runCancellablyInternal.

private static <T> T runCancellablyInternal(final PreparedStatement ps, ResultSetVisitor<T> visitor, final FinalSQLString sql, AutoClose autoClose, @Nullable Integer fetchSize) throws PalantirInterruptedException, PalantirSqlException {
    final String threadString = sql.toString();
    Future<ResultSet> result = service.submit(ThreadNamingCallable.wrapWithThreadName(() -> {
        if (Thread.currentThread().isInterrupted()) {
            // $NON-NLS-1$
            SqlLoggers.CANCEL_LOGGER.error("Threadpool thread has interrupt flag set!");
            // we want to clear the interrupted status here -
            // we cancel via the prepared statement, not interrupts
            Thread.interrupted();
        }
        if (fetchSize != null) {
            ps.setFetchSize(fetchSize);
        }
        return ps.executeQuery();
    }, threadString, ThreadNamingCallable.Type.APPEND));
    ResultSet rs = null;
    long startTime = System.currentTimeMillis();
    final String oldName = Thread.currentThread().getName();
    final String currentTimestamp = DateTimeFormat.forPattern("HH:mm:ss").print(System.currentTimeMillis());
    Thread.currentThread().setName(oldName + " blocking on " + threadString + " started at " + currentTimestamp);
    try {
        rs = result.get();
        return visitor.visit(rs);
    } catch (InterruptedException e) {
        try {
            // $NON-NLS-1$
            SqlLoggers.CANCEL_LOGGER.debug("about to cancel a SQL call", e);
            PreparedStatements.cancel(ps);
            // $NON-NLS-1$
            throw new PalantirInterruptedException("SQL call interrupted", e);
        } finally {
            // we delay this as long as possible, but under no
            // circumstances lose it
            Thread.currentThread().interrupt();
        }
    } catch (ExecutionException ee) {
        throw handleInterruptions(startTime, ee);
    } finally {
        Thread.currentThread().setName(oldName);
        if (rs != null && autoClose == AutoClose.TRUE) {
            ResultSets.close(rs);
        }
    }
}
Also used : PalantirInterruptedException(com.palantir.exception.PalantirInterruptedException) ResultSet(java.sql.ResultSet) FinalSQLString(com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString) ExecutionException(java.util.concurrent.ExecutionException) PalantirInterruptedException(com.palantir.exception.PalantirInterruptedException)

Example 4 with FinalSQLString

use of com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString in project atlasdb by palantir.

the class BasicSQL method selectResultSetSpecifyingDBType.

protected AgnosticResultSet selectResultSetSpecifyingDBType(final Connection c, final FinalSQLString sql, Object[] vs, final DBType dbType) throws PalantirSqlException, PalantirInterruptedException {
    if (SqlLoggers.LOGGER.isTraceEnabled()) {
        SqlLoggers.LOGGER.trace("SQL result set query: {}", sql.getQuery());
    }
    return wrapPreparedStatement(c, sql, vs, ps -> runCancellably(ps, (ResultSetVisitor<AgnosticResultSet>) rs -> {
        List<List<Object>> rvs = new ArrayList<List<Object>>();
        ResultSetMetaData meta = ResultSets.getMetaData(rs);
        int columnCount = ResultSets.getColumnCount(meta);
        Map<String, Integer> columnMap = ResultSets.buildInMemoryColumnMap(meta, dbType);
        while (ResultSets.next(rs)) {
            List<Object> row = Lists.newArrayListWithCapacity(columnCount);
            for (int i = 0; i < columnCount; i++) {
                row.add(ResultSets.getObject(rs, i + 1));
            }
            rvs.add(row);
        }
        return new AgnosticResultSetImpl(rvs, dbType, columnMap);
    }, sql, null), // $NON-NLS-1$
    "selectList");
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) FinalSQLString(com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString)

Aggregations

FinalSQLString (com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString)4 ResultSetMetaData (java.sql.ResultSetMetaData)3 ImmutableList (com.google.common.collect.ImmutableList)2 PalantirInterruptedException (com.palantir.exception.PalantirInterruptedException)2 ResourceCreationLocation (com.palantir.nexus.db.ResourceCreationLocation)2 SqlTimer (com.palantir.nexus.db.monitoring.timer.SqlTimer)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Lists (com.google.common.collect.Lists)1 Throwables (com.palantir.common.base.Throwables)1 NamedThreadFactory (com.palantir.common.concurrent.NamedThreadFactory)1 PTExecutors (com.palantir.common.concurrent.PTExecutors)1 ThreadNamingCallable (com.palantir.common.concurrent.ThreadNamingCallable)1 JdbcHandler (com.palantir.db.oracle.JdbcHandler)1 BlobHandler (com.palantir.db.oracle.JdbcHandler.BlobHandler)1 PalantirSqlException (com.palantir.exception.PalantirSqlException)1 DBType (com.palantir.nexus.db.DBType)1 SqlLoggers (com.palantir.nexus.db.sql.monitoring.logger.SqlLoggers)1 Connections (com.palantir.sql.Connections)1 PreparedStatements (com.palantir.sql.PreparedStatements)1