All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.db.ManagedPooledDataSource Maven / Gradle / Ivy

The newest version!
package io.dropwizard.db;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import org.apache.tomcat.jdbc.pool.ConnectionPool;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
import org.apache.tomcat.jdbc.pool.PoolConfiguration;

import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import static com.codahale.metrics.MetricRegistry.name;

/**
 * A {@link ManagedDataSource} which is backed by a Tomcat pooled {@link javax.sql.DataSource}.
 */
public class ManagedPooledDataSource extends DataSourceProxy implements ManagedDataSource {
    private final MetricRegistry metricRegistry;

    /**
     * Create a new data source with the given connection pool configuration.
     *
     * @param config the connection pool configuration
     */
    public ManagedPooledDataSource(PoolConfiguration config, MetricRegistry metricRegistry) {
        super(config);
        this.metricRegistry = metricRegistry;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException("Doesn't use java.util.logging");
    }

    @Override
    public void start() throws Exception {
        final ConnectionPool connectionPool = createPool();
        metricRegistry.register(name(getClass(), connectionPool.getName(), "active"),
            (Gauge) connectionPool::getActive);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "idle"),
            (Gauge) connectionPool::getIdle);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "waiting"),
            (Gauge) connectionPool::getWaitCount);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "size"),
            (Gauge) connectionPool::getSize);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "created"),
            (Gauge) connectionPool::getCreatedCount);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "borrowed"),
            (Gauge) connectionPool::getBorrowedCount);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "reconnected"),
            (Gauge) connectionPool::getReconnectedCount);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "released"),
            (Gauge) connectionPool::getReleasedCount);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "releasedIdle"),
            (Gauge) connectionPool::getReleasedIdleCount);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "returned"),
            (Gauge) connectionPool::getReturnedCount);

        metricRegistry.register(name(getClass(), connectionPool.getName(), "removeAbandoned"),
            (Gauge) connectionPool::getRemoveAbandonedCount);
    }

    @Override
    public void stop() throws Exception {
        close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy