Package io.dropwizard.db

Source Code of io.dropwizard.db.ManagedPooledDataSource

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;
    }

    // JDK6 has JDBC 4.0 which doesn't have this -- don't add @Override
    @SuppressWarnings("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"),
                                new Gauge<Integer>() {
                                    @Override
                                    public Integer getValue() {
                                        return connectionPool.getActive();
                                    }
                                });

        metricRegistry.register(name(getClass(), connectionPool.getName(), "idle"),
                                new Gauge<Integer>() {

                                    @Override
                                    public Integer getValue() {
                                        return connectionPool.getIdle();
                                    }
                                });
    }

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

Related Classes of io.dropwizard.db.ManagedPooledDataSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.