Search in sources :

Example 11 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project metrics by dropwizard.

the class InstrumentedTimingCollectorTest method updatesTimerForShortSqlObjectStrategy.

@Test
public void updatesTimerForShortSqlObjectStrategy() throws Exception {
    final StatementNameStrategy strategy = new ShortNameStrategy("jdbi");
    final InstrumentedTimingCollector collector = new InstrumentedTimingCollector(registry, strategy);
    final StatementContext ctx = mock(StatementContext.class);
    doReturn("SELECT 1").when(ctx).getRawSql();
    doReturn(getClass()).when(ctx).getSqlObjectType();
    doReturn(getClass().getMethod("updatesTimerForShortSqlObjectStrategy")).when(ctx).getSqlObjectMethod();
    collector.collect(TimeUnit.SECONDS.toNanos(1), ctx);
    final String name = strategy.getStatementName(ctx);
    final Timer timer = registry.timer(name);
    assertThat(name).isEqualTo(name("jdbi", getClass().getSimpleName(), "updatesTimerForShortSqlObjectStrategy"));
    assertThat(timer.getSnapshot().getMax()).isEqualTo(1000000000);
}
Also used : StatementNameStrategy(com.codahale.metrics.jdbi.strategies.StatementNameStrategy) Timer(com.codahale.metrics.Timer) ShortNameStrategy(com.codahale.metrics.jdbi.strategies.ShortNameStrategy) StatementContext(org.skife.jdbi.v2.StatementContext) Test(org.junit.Test)

Example 12 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project metrics by dropwizard.

the class InstrumentedTimingCollectorTest method updatesTimerForContextClass.

@Test
public void updatesTimerForContextClass() throws Exception {
    final StatementNameStrategy strategy = new SmartNameStrategy();
    final InstrumentedTimingCollector collector = new InstrumentedTimingCollector(registry, strategy);
    final StatementContext ctx = mock(StatementContext.class);
    doReturn("SELECT 1").when(ctx).getRawSql();
    doReturn(getClass().getName()).when(ctx).getAttribute(NameStrategies.STATEMENT_CLASS);
    doReturn("updatesTimerForContextClass").when(ctx).getAttribute(NameStrategies.STATEMENT_NAME);
    collector.collect(TimeUnit.SECONDS.toNanos(3), ctx);
    final String name = strategy.getStatementName(ctx);
    final Timer timer = registry.timer(name);
    assertThat(name).isEqualTo(name(getClass(), "updatesTimerForContextClass"));
    assertThat(timer.getSnapshot().getMax()).isEqualTo(3000000000L);
}
Also used : StatementNameStrategy(com.codahale.metrics.jdbi.strategies.StatementNameStrategy) Timer(com.codahale.metrics.Timer) SmartNameStrategy(com.codahale.metrics.jdbi.strategies.SmartNameStrategy) StatementContext(org.skife.jdbi.v2.StatementContext) Test(org.junit.Test)

Example 13 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project druid by druid-io.

the class JDBCExtractionNamespaceCacheFactory method populateCache.

@Override
@Nullable
public CacheScheduler.VersionedCache populateCache(final JDBCExtractionNamespace namespace, final CacheScheduler.EntryImpl<JDBCExtractionNamespace> entryId, final String lastVersion, final CacheScheduler scheduler) {
    final long lastCheck = lastVersion == null ? JodaUtils.MIN_INSTANT : Long.parseLong(lastVersion);
    final Long lastDBUpdate = lastUpdates(entryId, namespace);
    if (lastDBUpdate != null && lastDBUpdate <= lastCheck) {
        return null;
    }
    final long dbQueryStart = System.currentTimeMillis();
    final DBI dbi = ensureDBI(entryId, namespace);
    final String table = namespace.getTable();
    final String valueColumn = namespace.getValueColumn();
    final String keyColumn = namespace.getKeyColumn();
    LOG.debug("Updating %s", entryId);
    final List<Pair<String, String>> pairs = dbi.withHandle(new HandleCallback<List<Pair<String, String>>>() {

        @Override
        public List<Pair<String, String>> withHandle(Handle handle) throws Exception {
            final String query;
            query = String.format("SELECT %s, %s FROM %s", keyColumn, valueColumn, table);
            return handle.createQuery(query).map(new ResultSetMapper<Pair<String, String>>() {

                @Override
                public Pair<String, String> map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException {
                    return new Pair<>(r.getString(keyColumn), r.getString(valueColumn));
                }
            }).list();
        }
    });
    final String newVersion;
    if (lastDBUpdate != null) {
        newVersion = lastDBUpdate.toString();
    } else {
        newVersion = String.format("%d", dbQueryStart);
    }
    final CacheScheduler.VersionedCache versionedCache = scheduler.createVersionedCache(entryId, newVersion);
    try {
        final Map<String, String> cache = versionedCache.getCache();
        for (Pair<String, String> pair : pairs) {
            cache.put(pair.lhs, pair.rhs);
        }
        LOG.info("Finished loading %d values for %s", cache.size(), entryId);
        return versionedCache;
    } catch (Throwable t) {
        try {
            versionedCache.close();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        throw t;
    }
}
Also used : SQLException(java.sql.SQLException) DBI(org.skife.jdbi.v2.DBI) SQLException(java.sql.SQLException) Handle(org.skife.jdbi.v2.Handle) StatementContext(org.skife.jdbi.v2.StatementContext) ResultSet(java.sql.ResultSet) List(java.util.List) CacheScheduler(io.druid.server.lookup.namespace.cache.CacheScheduler) Pair(io.druid.java.util.common.Pair) Nullable(javax.annotation.Nullable)

Example 14 with StatementContext

use of org.skife.jdbi.v2.StatementContext 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 15 with StatementContext

use of org.skife.jdbi.v2.StatementContext in project SimpleFlatMapper by arnaudroger.

the class SfmResultSetMapperFactory method mapperFor.

@SuppressWarnings("unchecked")
@Override
public ResultSetMapper mapperFor(Class aClass, StatementContext statementContext) {
    ResultSetMapper mapper = cache.get(aClass);
    if (mapper == null) {
        Mapper<ResultSet, ?> resultSetMapper = mapperFactory.newInstance(aClass);
        mapper = toResultSetMapper(resultSetMapper);
        ResultSetMapper<?> cachedMapper = cache.putIfAbsent(aClass, mapper);
        if (cachedMapper != null) {
            mapper = cachedMapper;
        }
    }
    return mapper;
}
Also used : ResultSetMapper(org.skife.jdbi.v2.tweak.ResultSetMapper) ResultSet(java.sql.ResultSet)

Aggregations

StatementContext (org.skife.jdbi.v2.StatementContext)20 Test (org.junit.Test)13 Timer (com.codahale.metrics.Timer)11 StatementNameStrategy (com.codahale.metrics.jdbi.strategies.StatementNameStrategy)11 SmartNameStrategy (com.codahale.metrics.jdbi.strategies.SmartNameStrategy)9 ResultSet (java.sql.ResultSet)8 SQLException (java.sql.SQLException)5 Handle (org.skife.jdbi.v2.Handle)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DataSegment (org.apache.druid.timeline.DataSegment)3 Interval (org.joda.time.Interval)3 ShortNameStrategy (com.codahale.metrics.jdbi.strategies.ShortNameStrategy)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DataSegment (io.druid.timeline.DataSegment)2 HashSet (java.util.HashSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Nullable (javax.annotation.Nullable)2 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)2