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

io.agroal.pool.DataSource Maven / Gradle / Ivy

// Copyright (C) 2017 Red Hat, Inc. and individual contributors as indicated by the @author tags.
// You may not use this file except in compliance with the Apache License, Version 2.0.

package io.agroal.pool;

import io.agroal.api.AgroalDataSource;
import io.agroal.api.AgroalDataSourceListener;
import io.agroal.api.AgroalDataSourceMetrics;
import io.agroal.api.configuration.AgroalDataSourceConfiguration;
import io.agroal.api.configuration.AgroalDataSourceConfiguration.MetricsEnabledListener;
import io.agroal.pool.MetricsRepository.EmptyMetricsRepository;
import io.agroal.pool.util.StampedCopyOnWriteArrayList;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author Luis Barreiro
 */
public class DataSource implements AgroalDataSource, MetricsEnabledListener {

    private static final long serialVersionUID = 6485903416474487024L;

    private final AgroalDataSourceConfiguration configuration;
    private final List listenerList;
    private final ConnectionPool connectionPool;
    private MetricsRepository dataSourceMetrics;

    public DataSource(AgroalDataSourceConfiguration configuration, AgroalDataSourceListener... listeners) {
        this.configuration = configuration;
        this.configuration.registerMetricsEnabledListener( this );

        listenerList = new StampedCopyOnWriteArrayList<>( AgroalDataSourceListener.class );
        listenerList.addAll( Arrays.asList( listeners ) );
        connectionPool = new ConnectionPool( configuration.connectionPoolConfiguration(), this );
        onMetricsEnabled( configuration.metricsEnabled() );

        connectionPool.init();
    }

    public List listenerList() {
        return listenerList;
    }

    public MetricsRepository metricsRepository() {
        return dataSourceMetrics;
    }

    @Override
    public void onMetricsEnabled(boolean metricsEnabled) {
        dataSourceMetrics = metricsEnabled ? new DefaultMetricsRepository( connectionPool ) : new EmptyMetricsRepository();
    }

    // --- AgroalDataSource methods //

    @Override
    public AgroalDataSourceConfiguration getConfiguration() {
        return configuration;
    }

    @Override
    public AgroalDataSourceMetrics getMetrics() {
        return dataSourceMetrics;
    }

    public void addListener(AgroalDataSourceListener listener) {
        listenerList.add( listener );
    }

    @Override
    public void close() {
        connectionPool.close();
    }

    // --- DataSource methods //

    @Override
    public Connection getConnection() throws SQLException {
        return connectionPool.getConnection();
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        throw new SQLException( "username/password invalid on a pooled data source" );
    }

    // --- Wrapper methods //

    @Override
    public  T unwrap(Class target) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class target) throws SQLException {
        return false;
    }

    // --- CommonDataSource methods //

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
        // no-op
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        // no-op
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException( "Not Supported" );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy