use of org.jdbi.v3.core.JdbiException in project dropwizard by dropwizard.
the class LoggingJdbiExceptionMapperTest method testSqlExceptionIsCause.
@Test
void testSqlExceptionIsCause() throws Exception {
StatementContext statementContext = mock(StatementContext.class);
RuntimeException runtimeException = new RuntimeException("DB is down");
SQLException sqlException = new SQLException("DB error", runtimeException);
JdbiException jdbiException = new NoResultsException("Unable get a result set", sqlException, statementContext);
jdbiExceptionMapper.logException(9812, jdbiException);
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", jdbiException);
}
use of org.jdbi.v3.core.JdbiException in project hetu-core by openlookeng.
the class DbSourceExactMatchSelector method match.
@Override
public Optional<SelectionContext<ResourceGroupIdTemplate>> match(SelectionCriteria criteria) {
if (!criteria.getSource().isPresent()) {
return Optional.empty();
}
try {
String resourceGroupId = dao.getExactMatchResourceGroup(environment, criteria.getSource().get(), criteria.getQueryType().orElse(""));
Long start = daoOfflineStart.get();
if (start != null && daoOfflineStart.compareAndSet(start, null)) {
log.info("Successfully fetched exact match selectors after %s", nanosSince(start));
}
if (resourceGroupId == null) {
return Optional.empty();
}
ResourceGroupId groupId;
try {
groupId = resourceGroupIdCodec.fromJson(resourceGroupId);
} catch (IllegalArgumentException e) {
log.warn("Failed to decode resource group from DB: %s", resourceGroupId);
return Optional.empty();
}
return Optional.of(new SelectionContext<>(groupId, toTemplate(groupId)));
} catch (JdbiException e) {
if (daoOfflineStart.compareAndSet(null, System.nanoTime())) {
log.warn(e, "Failed to fetch exact match resource group selectors");
}
return Optional.empty();
}
}
use of org.jdbi.v3.core.JdbiException in project hetu-core by openlookeng.
the class JdbcMetadataUtil method runTransactionWithLock.
/**
* run the transaction with lock
*
* @param jdbi jdbi
* @param callback callback
*/
public static void runTransactionWithLock(Jdbi jdbi, HandleConsumer<PrestoException> callback) {
JdbcBasedLock jdbcLock = new JdbcBasedLock(jdbi);
try {
jdbcLock.lock();
jdbi.useTransaction(callback);
} catch (JdbiException e) {
if (e.getCause() != null) {
throwIfInstanceOf(e.getCause(), PrestoException.class);
}
throw new PrestoException(HETU_METASTORE_CODE, "Hetu metastore operation failed.", e);
} finally {
jdbcLock.unlock();
}
}
use of org.jdbi.v3.core.JdbiException in project trino by trinodb.
the class ShardMetadataRecordCursor method getTableIds.
@VisibleForTesting
static Iterator<Long> getTableIds(Jdbi 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 | JdbiException e) {
throw metadataError(e);
}
return tableIds.build().iterator();
}
use of org.jdbi.v3.core.JdbiException in project trino by trinodb.
the class ShardMetadataRecordCursor method getNextResultSet.
private ResultSet getNextResultSet() {
closeCurrentResultSet();
if (!tableIds.hasNext()) {
return null;
}
Long tableId = tableIds.next();
Long columnId = metadataDao.getTemporalColumnId(tableId);
List<String> columnNames;
if (columnId == null) {
columnNames = getMappedColumnNames("null", "null", "null", "null");
} else {
Type temporalType = metadataDao.getTableColumn(tableId, columnId).getDataType();
if (temporalType.equals(DATE)) {
columnNames = getMappedColumnNames("null", "null", minColumn(columnId), maxColumn(columnId));
} else if (temporalType.equals(TIMESTAMP_MILLIS)) {
columnNames = getMappedColumnNames(minColumn(columnId), maxColumn(columnId), "null", "null");
} else {
throw new TrinoException(RAPTOR_CORRUPT_METADATA, "Temporal column should be of type date or timestamp, not " + temporalType.getDisplayName());
}
}
try {
connection = dbi.open().getConnection();
statement = PreparedStatementBuilder.create(connection, constructSqlTemplate(columnNames, tableId), columnNames, TYPES, ImmutableSet.of(getColumnIndex(SHARD_METADATA, SHARD_UUID)), tupleDomain);
return statement.executeQuery();
} catch (SQLException | JdbiException e) {
close();
throw metadataError(e);
}
}
Aggregations