Search in sources :

Example 1 with JdbiException

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);
}
Also used : NoResultsException(org.jdbi.v3.core.result.NoResultsException) SQLException(java.sql.SQLException) JdbiException(org.jdbi.v3.core.JdbiException) StatementContext(org.jdbi.v3.core.statement.StatementContext) Test(org.junit.jupiter.api.Test)

Example 2 with 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();
    }
}
Also used : ResourceGroupId(io.prestosql.spi.resourcegroups.ResourceGroupId) JdbiException(org.jdbi.v3.core.JdbiException)

Example 3 with JdbiException

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();
    }
}
Also used : PrestoException(io.prestosql.spi.PrestoException) JdbiException(org.jdbi.v3.core.JdbiException)

Example 4 with JdbiException

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();
}
Also used : SQLException(java.sql.SQLException) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) JdbiException(org.jdbi.v3.core.JdbiException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with JdbiException

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);
    }
}
Also used : Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) SQLException(java.sql.SQLException) TrinoException(io.trino.spi.TrinoException) JdbiException(org.jdbi.v3.core.JdbiException)

Aggregations

JdbiException (org.jdbi.v3.core.JdbiException)11 SQLException (java.sql.SQLException)4 RaptorColumnHandle (io.trino.plugin.raptor.legacy.RaptorColumnHandle)3 Handle (org.jdbi.v3.core.Handle)3 Type (io.trino.spi.type.Type)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Test (org.junit.jupiter.api.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 VerifyException (com.google.common.base.VerifyException)1 ImmutableList (com.google.common.collect.ImmutableList)1 PrestoException (io.prestosql.spi.PrestoException)1 ResourceGroupId (io.prestosql.spi.resourcegroups.ResourceGroupId)1 ShardOrganizerDao (io.trino.plugin.raptor.legacy.storage.organization.ShardOrganizerDao)1 TrinoException (io.trino.spi.TrinoException)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 FixedPageSource (io.trino.spi.connector.FixedPageSource)1 Domain (io.trino.spi.predicate.Domain)1 TupleDomain (io.trino.spi.predicate.TupleDomain)1 ResourceGroupId (io.trino.spi.resourcegroups.ResourceGroupId)1