use of org.skife.jdbi.v2.exceptions.DBIException in project dropwizard by dropwizard.
the class LoggingDBIExceptionMapperTest method testPlainDBIException.
@Test
public void testPlainDBIException() throws Exception {
DBIException dbiException = new TransactionFailedException("Transaction failed for unknown reason");
dbiExceptionMapper.logException(9812, dbiException);
verify(logger).error("Error handling a request: 0000000000002654", dbiException);
}
use of org.skife.jdbi.v2.exceptions.DBIException in project dropwizard by dropwizard.
the class LoggingDBIExceptionMapperTest method testSqlExceptionIsCause.
@Test
public void testSqlExceptionIsCause() throws Exception {
StatementContext statementContext = mock(StatementContext.class);
RuntimeException runtimeException = new RuntimeException("DB is down");
SQLException sqlException = new SQLException("DB error", runtimeException);
DBIException dbiException = new NoResultsException("Unable get a result set", sqlException, statementContext);
dbiExceptionMapper.logException(9812, dbiException);
verify(logger).error("Error handling a request: 0000000000002654", sqlException);
verify(logger).error("Error handling a request: 0000000000002654", runtimeException);
verify(logger, never()).error("Error handling a request: 0000000000002654", dbiException);
}
use of org.skife.jdbi.v2.exceptions.DBIException in project presto by prestodb.
the class ShardMetadataRecordCursor method getTableIds.
@VisibleForTesting
static Iterator<Long> getTableIds(IDBI dbi, TupleDomain<Integer> tupleDomain) {
Map<Integer, Domain> domains = tupleDomain.getDomains().get();
Domain schemaNameDomain = domains.get(getColumnIndex(SHARD_METADATA, SCHEMA_NAME));
Domain tableNameDomain = domains.get(getColumnIndex(SHARD_METADATA, TABLE_NAME));
List<String> values = new ArrayList<>();
StringBuilder sql = new StringBuilder("SELECT table_id FROM tables ");
if (schemaNameDomain != null || tableNameDomain != null) {
sql.append("WHERE ");
List<String> predicates = new ArrayList<>();
if (tableNameDomain != null && tableNameDomain.isSingleValue()) {
predicates.add("table_name = ?");
values.add(getStringValue(tableNameDomain.getSingleValue()));
}
if (schemaNameDomain != null && schemaNameDomain.isSingleValue()) {
predicates.add("schema_name = ?");
values.add(getStringValue(schemaNameDomain.getSingleValue()));
}
sql.append(Joiner.on(" AND ").join(predicates));
}
ImmutableList.Builder<Long> tableIds = ImmutableList.builder();
try (Connection connection = dbi.open().getConnection();
PreparedStatement statement = connection.prepareStatement(sql.toString())) {
for (int i = 0; i < values.size(); i++) {
statement.setString(i + 1, values.get(i));
}
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
tableIds.add(resultSet.getLong("table_id"));
}
}
} catch (SQLException | DBIException e) {
throw metadataError(e);
}
return tableIds.build().iterator();
}
use of org.skife.jdbi.v2.exceptions.DBIException in project presto by prestodb.
the class DatabaseShardManager method dropTable.
@Override
public void dropTable(long tableId) {
runTransaction(dbi, (handle, status) -> {
lockTable(handle, tableId);
ShardDao shardDao = shardDaoSupplier.attach(handle);
shardDao.insertDeletedShards(tableId);
shardDao.dropShardNodes(tableId);
shardDao.dropShards(tableId);
handle.attach(ShardOrganizerDao.class).dropOrganizerJobs(tableId);
MetadataDao dao = handle.attach(MetadataDao.class);
dao.dropColumns(tableId);
dao.dropTable(tableId);
return null;
});
// It is not possible to drop the index tables in a transaction.
try (Handle handle = dbi.open()) {
handle.execute("DROP TABLE " + shardIndexTable(tableId));
} catch (DBIException e) {
log.warn(e, "Failed to drop index table %s", shardIndexTable(tableId));
}
}
use of org.skife.jdbi.v2.exceptions.DBIException in project presto by prestodb.
the class DatabaseShardManager method addColumn.
@Override
public void addColumn(long tableId, ColumnInfo column) {
String columnType = sqlColumnType(column.getType());
if (columnType == null) {
return;
}
String sql = format("ALTER TABLE %s ADD COLUMN (%s %s, %s %s)", shardIndexTable(tableId), minColumn(column.getColumnId()), columnType, maxColumn(column.getColumnId()), columnType);
int attempts = 0;
while (true) {
attempts++;
try (Handle handle = dbi.open()) {
handle.execute(sql);
} catch (DBIException e) {
if (isSyntaxOrAccessError(e)) {
// exit when column already exists
return;
}
if (attempts >= MAX_ADD_COLUMN_ATTEMPTS) {
throw metadataError(e);
}
}
}
}
Aggregations