Search in sources :

Example 1 with DBIException

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);
}
Also used : DBIException(org.skife.jdbi.v2.exceptions.DBIException) TransactionFailedException(org.skife.jdbi.v2.exceptions.TransactionFailedException) Test(org.junit.Test)

Example 2 with 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);
}
Also used : NoResultsException(org.skife.jdbi.v2.exceptions.NoResultsException) SQLException(java.sql.SQLException) DBIException(org.skife.jdbi.v2.exceptions.DBIException) StatementContext(org.skife.jdbi.v2.StatementContext) Test(org.junit.Test)

Example 3 with 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();
}
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) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) DBIException(org.skife.jdbi.v2.exceptions.DBIException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with DBIException

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));
    }
}
Also used : ShardOrganizerDao(com.facebook.presto.raptor.storage.organization.ShardOrganizerDao) DBIException(org.skife.jdbi.v2.exceptions.DBIException) RaptorColumnHandle(com.facebook.presto.raptor.RaptorColumnHandle) Handle(org.skife.jdbi.v2.Handle)

Example 5 with DBIException

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);
            }
        }
    }
}
Also used : DBIException(org.skife.jdbi.v2.exceptions.DBIException) RaptorColumnHandle(com.facebook.presto.raptor.RaptorColumnHandle) Handle(org.skife.jdbi.v2.Handle)

Aggregations

DBIException (org.skife.jdbi.v2.exceptions.DBIException)6 RaptorColumnHandle (com.facebook.presto.raptor.RaptorColumnHandle)3 Handle (org.skife.jdbi.v2.Handle)3 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 ShardOrganizerDao (com.facebook.presto.raptor.storage.organization.ShardOrganizerDao)1 Domain (com.facebook.presto.spi.predicate.Domain)1 TupleDomain (com.facebook.presto.spi.predicate.TupleDomain)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 StringJoiner (java.util.StringJoiner)1 StatementContext (org.skife.jdbi.v2.StatementContext)1 NoResultsException (org.skife.jdbi.v2.exceptions.NoResultsException)1 TransactionFailedException (org.skife.jdbi.v2.exceptions.TransactionFailedException)1