view geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/connectionpool/ArcSDEConnectionPool.java @ 1127:ebeb56428409

Added license headers and license file. geo-backend/trunk@1261 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:52:22 +0000
parents e5e30090c37c
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.geobackend.sde.connectionpool;

import java.sql.Connection;
import java.util.Properties;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.log4j.Logger;

import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPool;
import de.intevation.gnv.geobackend.base.connectionpool.exception.ConnectionException;

/**
 *This is the ArcSDE specific implementation of the Interface ConnectionPool
 *
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 */
public class ArcSDEConnectionPool implements ConnectionPool {

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger
            .getLogger(ArcSDEPoolableObjectFactory.class);

    /**
     * The Pool which stores the Connections to the ArcSDE Backend
     */
    private GenericObjectPool pool = null;

    /**
     * @see de.intevation.gnv.geobackend.base.connectionpool.ConnectionPool#closeConnection(java.sql.Connection)
     */
    public void closeConnection(Connection connection)
                                                      throws ConnectionException {
        try {
            this.pool.returnObject(connection);
        } catch (Exception e) {
            log.error(e, e);
            throw new ConnectionException(e);
        }

    }

    /**
     * @see de.intevation.gnv.geobackend.base.connectionpool.ConnectionPool#getConnection(java.lang.String)
     */
    public synchronized Connection getConnection(String connectionID)
                                                                     throws ConnectionException {
        try {
            Object object = this.pool.borrowObject();

            if (object instanceof Connection) {
                return (Connection) object;
            } else {
                throw new ConnectionException(
                        "Created Object is not an java.sql.Connection");
            }

        } catch (Exception e) {
            log.error(e, e);
            throw new ConnectionException(e);
        }
    }

    /**
     * @see de.intevation.gnv.geobackend.base.connectionpool.ConnectionPool#initialize(java.util.Properties)
     */
    public void initialize(Properties properties) {
        log.info("ArcSDEConnectionPool.initialize has been called");
        log.info("ConnectionPool will be initialized");

        PoolableObjectFactory poolableObjectFactory = new ArcSDEPoolableObjectFactory(
                properties);

        int maxActive = Integer.parseInt(properties.getProperty("maxActive",
                "10"));
        long maxWait = Long.parseLong(properties.getProperty("maxWait",
                "" + GenericObjectPool.DEFAULT_MAX_WAIT));
        int maxIdle = Integer.parseInt(properties.getProperty("maxIdle",
                "" + GenericObjectPool.DEFAULT_MAX_IDLE));
        int minIdle = Integer.parseInt(properties.getProperty("minIdle",
                "" + GenericObjectPool.DEFAULT_MIN_IDLE));
        boolean testOnBorrow = Boolean.parseBoolean(properties.getProperty(
                "testOnBorrow", "" + GenericObjectPool.DEFAULT_TEST_ON_BORROW));
        boolean testOnReturn = Boolean.parseBoolean(properties.getProperty(
                "testOnReturn", "" + GenericObjectPool.DEFAULT_TEST_ON_RETURN));
        long timeBetweenEvictionRunsMillis = Long
                .parseLong(properties
                        .getProperty(
                                "timeBetweenEvictionRunsMillis",
                                ""
                                        + GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS));
        int numTestsPerEvictionRun = Integer.parseInt(properties.getProperty(
                "numTestsPerEvictionRun",
                "" + GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN));
        long minEvictableIdleTimeMillis = Long
                .parseLong(properties
                        .getProperty(
                                "minEvictableIdleTimeMillis",
                                ""
                                        + GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
        boolean testWhileIdle = Boolean.parseBoolean(properties
                .getProperty("testWhileIdle",
                        "" + GenericObjectPool.DEFAULT_TEST_WHILE_IDLE));

        log.info("Maximum Number of active Connections: " + maxActive);
        log.info("Maximum Number of waiting: " + maxWait);
        log.info("Maximum Number of idle: " + maxIdle);
        log.info("Minimum Number of idle: " + minIdle);
        log.info("TestOnBorrow: " + testOnBorrow);
        log.info("TestOnReturn: " + testOnReturn);
        log.info("TimeBetweenEvictionRunsMillis: " + timeBetweenEvictionRunsMillis);
        log.info("NumTestsPerEvictionRun: " + numTestsPerEvictionRun);
        log.info("MinEvictableIdleTimeMillis: " + minEvictableIdleTimeMillis);
        log.info("TestWhileIdle: " + testWhileIdle);

        this.pool = new GenericObjectPool(poolableObjectFactory, maxActive,
                                            GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, maxWait,
                                            maxIdle, minIdle, testOnBorrow, testOnReturn,
                                            timeBetweenEvictionRunsMillis, numTestsPerEvictionRun,
                                            minEvictableIdleTimeMillis, testWhileIdle);
	}
}

http://dive4elements.wald.intevation.org