Search in sources :

Example 1 with UnableToObtainConnectionException

use of org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException in project presto by prestodb.

the class SchemaDaoUtil method createTablesWithRetry.

public static void createTablesWithRetry(IDBI dbi) {
    Duration delay = new Duration(2, TimeUnit.SECONDS);
    while (true) {
        try (Handle handle = dbi.open()) {
            createTables(handle.attach(SchemaDao.class));
            alterTables(handle.getConnection(), handle.attach(SchemaDao.class));
            return;
        } catch (UnableToObtainConnectionException | SQLTransientException e) {
            log.warn("Failed to connect to database. Will retry again in %s. Exception: %s", delay, e.getMessage());
            sleep(delay);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) SQLTransientException(java.sql.SQLTransientException) UnableToObtainConnectionException(org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException) Duration(io.airlift.units.Duration) Handle(org.skife.jdbi.v2.Handle)

Example 2 with UnableToObtainConnectionException

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

the class JdbcCacheGenerator method generateCache.

@Override
@Nullable
public CacheScheduler.VersionedCache generateCache(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;
    final long dbQueryStart;
    try {
        lastDBUpdate = lastUpdates(entryId, namespace);
        if (lastDBUpdate != null && lastDBUpdate <= lastCheck) {
            return null;
        }
    } catch (UnableToObtainConnectionException e) {
        if (e.getMessage().contains(NO_SUITABLE_DRIVER_FOUND_ERROR)) {
            throw new ISE(e, JDBC_DRIVER_JAR_FILES_MISSING_ERROR);
        } else {
            throw e;
        }
    }
    dbQueryStart = System.currentTimeMillis();
    LOG.debug("Updating %s", entryId);
    final String newVersion;
    if (lastDBUpdate != null) {
        newVersion = lastDBUpdate.toString();
    } else {
        newVersion = StringUtils.format("%d", dbQueryStart);
    }
    final CacheScheduler.VersionedCache versionedCache = scheduler.createVersionedCache(entryId, newVersion);
    final long startNs = System.nanoTime();
    try (Handle handle = getHandle(entryId, namespace);
        ResultIterator<Pair<String, String>> pairs = getLookupPairs(handle, namespace)) {
        final Map<String, String> cache = versionedCache.getCache();
        final MapPopulator.PopulateResult populateResult = MapPopulator.populateAndWarnAtByteLimit(pairs, cache, (long) (MAX_MEMORY * namespace.getMaxHeapPercentage() / 100.0), null == entryId ? null : entryId.toString());
        final long duration = System.nanoTime() - startNs;
        LOG.info("Finished loading %,d values (%d bytes) for [%s] in %,d ns", populateResult.getEntries(), populateResult.getBytes(), entryId, duration);
        return versionedCache;
    } catch (UnableToObtainConnectionException e) {
        if (e.getMessage().contains(NO_SUITABLE_DRIVER_FOUND_ERROR)) {
            throw new ISE(e, JDBC_DRIVER_JAR_FILES_MISSING_ERROR);
        } else {
            throw e;
        }
    } catch (Throwable t) {
        try {
            versionedCache.close();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        throw t;
    }
}
Also used : UnableToObtainConnectionException(org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException) Handle(org.skife.jdbi.v2.Handle) UnableToObtainConnectionException(org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException) ISE(org.apache.druid.java.util.common.ISE) CacheScheduler(org.apache.druid.server.lookup.namespace.cache.CacheScheduler) Pair(org.apache.druid.java.util.common.Pair) MapPopulator(org.apache.druid.data.input.MapPopulator) Nullable(javax.annotation.Nullable)

Aggregations

Handle (org.skife.jdbi.v2.Handle)2 UnableToObtainConnectionException (org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException)2 Duration (io.airlift.units.Duration)1 SQLException (java.sql.SQLException)1 SQLTransientException (java.sql.SQLTransientException)1 Nullable (javax.annotation.Nullable)1 MapPopulator (org.apache.druid.data.input.MapPopulator)1 ISE (org.apache.druid.java.util.common.ISE)1 Pair (org.apache.druid.java.util.common.Pair)1 CacheScheduler (org.apache.druid.server.lookup.namespace.cache.CacheScheduler)1