use of com.palantir.exception.PalantirInterruptedException 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);
}
use of com.palantir.exception.PalantirInterruptedException in project atlasdb by palantir.
the class BasicSQL method handleInterruptions.
public static PalantirSqlException handleInterruptions(long startTime, SQLException cause) throws PalantirSqlException {
SqlLoggers.SQL_EXCEPTION_LOG.debug("Caught SQLException", cause);
String message = cause.getMessage().trim();
// check for oracle and postgres
if (!message.contains(ORACLE_CANCEL_ERROR) && !message.contains(POSTGRES_CANCEL_ERROR)) {
throw PalantirSqlException.create(cause);
}
String elapsedTime = "N/A";
if (startTime > 0) {
// $NON-NLS-1$
elapsedTime = String.valueOf(System.currentTimeMillis() - startTime);
}
SqlLoggers.CANCEL_LOGGER.info(// $NON-NLS-1$
"We got an execution exception that was an interrupt, most likely from someone " + "incorrectly ignoring an interrupt. " + "Elapsed time: {}\n" + "Error message: {} " + "Error code: {} " + "Error state: {} " + "Error cause: {}", elapsedTime, // $NON-NLS-1$
cause.getMessage(), // $NON-NLS-1$
cause.getErrorCode(), // $NON-NLS-1$
cause.getSQLState(), // $NON-NLS-1$
cause.getNextException(), new Exception("Just for a stack trace"));
Thread.currentThread().interrupt();
// $NON-NLS-1$
throw new PalantirInterruptedException("SQL call interrupted", cause);
}
use of com.palantir.exception.PalantirInterruptedException 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);
}
}
}
Aggregations