com.tera.common.database.factory.DatabaseFactoryStatistics.java Source code

Java tutorial

Introduction

Here is the source code for com.tera.common.database.factory.DatabaseFactoryStatistics.java

Source

/**
 * This file is part of tera-api.
 *
 * tera-api is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
    
 * tera-api is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
    
 * You should have received a copy of the GNU General Public License
 * along with tera-api.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.tera.common.database.factory;

import org.apache.commons.pool.ObjectPool;

import com.tera.common.database.DatabaseFactory;

/**
 * @author ATracer
 */
public class DatabaseFactoryStatistics {

    private int currentActiveConnections = -1;

    private int currentIdleConnections = -1;

    /**
     * Instance can be get via static method
     */
    private DatabaseFactoryStatistics() {
    }

    /**
     * @return the currentActiveConnections
     */
    public int getCurrentActiveConnections() {
        return currentActiveConnections;
    }

    /**
     * @return the currentIdleConnections
     */
    public int getCurrentIdleConnections() {
        return currentIdleConnections;
    }

    /**
     * Factory method that will use supplied database factory for statistics
     * 
     * @param dbFactory
     * @return
     */
    public static DatabaseFactoryStatistics getStats(DatabaseFactory dbFactory) {
        DatabaseFactoryStatistics stats = new DatabaseFactoryStatistics();
        if (dbFactory instanceof AbstractDatabaseFactory) {
            AbstractDatabaseFactory adbFactory = (AbstractDatabaseFactory) dbFactory;
            ObjectPool pool = adbFactory.getPool();
            stats.currentActiveConnections = pool.getNumActive();
            stats.currentIdleConnections = pool.getNumIdle();
        }
        return stats;
    }

    @Override
    public String toString() {
        return "DatabaseFactoryStatistics [currentActiveConnections=" + currentActiveConnections
                + ", currentIdleConnections=" + currentIdleConnections + "]";
    }

}