use of com.palantir.exception.PalantirSqlException in project atlasdb by palantir.
the class AbstractDbWriteTable method putSentinels.
@Override
public void putSentinels(Iterable<Cell> cells) {
byte[] value = new byte[0];
long ts = Value.INVALID_VALUE_TIMESTAMP;
for (List<Cell> batch : Iterables.partition(Ordering.natural().immutableSortedCopy(cells), 1000)) {
List<Object[]> args = Lists.newArrayListWithCapacity(batch.size());
for (Cell cell : batch) {
args.add(new Object[] { cell.getRowName(), cell.getColumnName(), ts, value, cell.getRowName(), cell.getColumnName(), ts });
}
while (true) {
try {
String prefixedTableName = prefixedTableNames.get(tableRef, conns);
conns.get().insertManyUnregisteredQuery("/* INSERT_WHERE_NOT_EXISTS (" + prefixedTableName + ") */" + " INSERT INTO " + prefixedTableName + " (row_name, col_name, ts, val) " + " SELECT ?, ?, ?, ? FROM DUAL" + " WHERE NOT EXISTS (SELECT * FROM " + prefixedTableName + " WHERE" + " row_name = ? AND" + " col_name = ? AND" + " ts = ?)", args);
break;
} catch (PalantirSqlException e) {
// TODO(jboreiko): Actually you can. Evaluate use of MERGE or UPSERT here.
if (!ExceptionCheck.isUniqueConstraintViolation(e)) {
throw e;
}
}
}
}
}
use of com.palantir.exception.PalantirSqlException in project atlasdb by palantir.
the class BasicSQL method insertMany.
public boolean insertMany(final Connection c, final FinalSQLString sql, final Object[][] vs) throws PalantirSqlException {
if (SqlLoggers.LOGGER.isTraceEnabled()) {
SqlLoggers.LOGGER.trace("SQL insert many query: {}", sql.getQuery());
}
return BasicSQLUtils.runUninterruptably(() -> {
int[] inserted = null;
PreparedStatement ps = null;
// $NON-NLS-1$ //$NON-NLS-2$
SqlTimer.Handle timerKey = getSqlTimer().start("insertMany(" + vs.length + ")", sql.getKey(), sql.getQuery());
List<BlobHandler> cleanups = Lists.newArrayList();
try {
ps = c.prepareStatement(sql.getQuery());
for (int i = 0; i < vs.length; i++) {
for (int j = 0; j < vs[i].length; j++) {
Object obj = vs[i][j];
BlobHandler cleanup = setObject(c, ps, j + 1, obj);
if (cleanup != null) {
cleanups.add(cleanup);
}
}
ps.addBatch();
}
inserted = ps.executeBatch();
} catch (SQLException sqle) {
SqlLoggers.SQL_EXCEPTION_LOG.debug("Caught SQLException", sqle);
throw wrapSQLExceptionWithVerboseLogging(sqle, sql.getQuery(), vs);
} finally {
closeSilently(ps);
timerKey.stop();
for (BlobHandler cleanup : cleanups) {
try {
cleanup.freeTemporary();
} catch (Exception e) {
// $NON-NLS-1$
SqlLoggers.LOGGER.error("failed to free temp blob", e);
}
}
}
if (inserted == null || inserted.length != vs.length) {
assert false;
return false;
}
for (int numInsertedForRow : inserted) {
if (numInsertedForRow == Statement.EXECUTE_FAILED) {
// $NON-NLS-1$
assert DBType.getTypeFromConnection(c) != DBType.ORACLE : "numInsertedForRow: " + numInsertedForRow;
return false;
}
}
return true;
}, sql.toString(), c);
}
use of com.palantir.exception.PalantirSqlException 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.PalantirSqlException 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.PalantirSqlException in project atlasdb by palantir.
the class OracleOverflowWriteTable method put.
private void put(List<Object[]> args, List<Object[]> overflowArgs) {
if (!overflowArgs.isEmpty()) {
if (config.overflowMigrationState() == OverflowMigrationState.UNSTARTED) {
conns.get().insertManyUnregisteredQuery("/* INSERT_OVERFLOW */" + " INSERT INTO " + config.singleOverflowTable() + " (id, val) VALUES (?, ?) ", overflowArgs);
} else {
String shortOverflowTableName = getShortOverflowTableName();
conns.get().insertManyUnregisteredQuery("/* INSERT_OVERFLOW (" + shortOverflowTableName + ") */" + " INSERT INTO " + shortOverflowTableName + " (id, val) VALUES (?, ?) ", overflowArgs);
}
}
try {
String shortTableName = oraclePrefixedTableNames.get(tableRef, conns);
conns.get().insertManyUnregisteredQuery("/* INSERT_ONE (" + shortTableName + ") */" + " INSERT INTO " + shortTableName + " (row_name, col_name, ts, val, overflow) " + " VALUES (?, ?, ?, ?, ?) ", args);
} catch (PalantirSqlException e) {
if (ExceptionCheck.isUniqueConstraintViolation(e)) {
throw new KeyAlreadyExistsException("primary key violation", e);
}
throw e;
}
}
Aggregations