Example usage for org.apache.commons.pool.impl StackKeyedObjectPoolFactory StackKeyedObjectPoolFactory

List of usage examples for org.apache.commons.pool.impl StackKeyedObjectPoolFactory StackKeyedObjectPoolFactory

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl StackKeyedObjectPoolFactory StackKeyedObjectPoolFactory.

Prototype

public StackKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int max) 

Source Link

Document

Create a new StackKeyedObjectPoolFactory.

Usage

From source file:com.glaf.core.jdbc.connection.DBCPConnectionProvider.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public void configure(Properties props) throws RuntimeException {
    String jdbcDriverClass = props.getProperty(DBConfiguration.JDBC_DRIVER);
    String jdbcUrl = props.getProperty(DBConfiguration.JDBC_URL);
    Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(props);

    log.info("DBCP using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl);
    log.info("Connection properties: " + PropertiesHelper.maskOut(connectionProps, "password"));

    autocommit = PropertiesHelper.getBoolean(DBConfiguration.JDBC_AUTOCOMMIT, props);
    log.info("autocommit mode: " + autocommit);

    if (jdbcDriverClass == null) {
        log.warn("No JDBC Driver class was specified by property " + DBConfiguration.JDBC_DRIVER);
    } else {//w  w w .j  av a  2s  .c  o  m
        try {
            Class.forName(jdbcDriverClass);
        } catch (ClassNotFoundException cnfe) {
            try {
                ClassUtils.classForName(jdbcDriverClass);
            } catch (Exception e) {
                String msg = "JDBC Driver class not found: " + jdbcDriverClass;
                log.error(msg, e);
                throw new RuntimeException(msg, e);
            }
        }
    }

    String dbUser = props.getProperty(DBConfiguration.JDBC_USER);
    String dbPassword = props.getProperty(DBConfiguration.JDBC_PASSWORD);

    if (dbUser == null) {
        dbUser = ""; // Some RDBMS (e.g Postgresql) don't like null
                     // usernames
    }

    if (dbPassword == null) {
        dbPassword = ""; // Some RDBMS (e.g Postgresql) don't like null
                         // passwords
    }

    // Create the actual pool of connections
    ObjectPool connectionPool = new GenericObjectPool(null);

    // Apply any properties
    if (props.getProperty(ConnectionConstants.PROP_MAXIDLE) != null) {
        int value = PropertiesHelper.getInt(ConnectionConstants.PROP_MAXIDLE, props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxIdle(value);
        }
    }
    if (props.getProperty(ConnectionConstants.PROP_MINIDLE) != null) {
        int value = PropertiesHelper.getInt(ConnectionConstants.PROP_MINIDLE, props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMinIdle(value);
        }
    }
    if (props.getProperty(ConnectionConstants.PROP_MAXACTIVE) != null) {
        int value = PropertiesHelper.getInt(ConnectionConstants.PROP_MAXACTIVE, props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxActive(value);
        }
    }
    if (props.getProperty(ConnectionConstants.PROP_MAXWAIT) != null) {
        int value = PropertiesHelper.getInt(ConnectionConstants.PROP_MAXWAIT, props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxWait(value);
        }
    }
    // how often should the evictor run (if ever, default is -1 = off)
    if (props.getProperty(ConnectionConstants.PROP_TIMEBETWEENEVICTIONRUNSMILLIS) != null) {
        int value = PropertiesHelper.getInt(ConnectionConstants.PROP_TIMEBETWEENEVICTIONRUNSMILLIS, props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setTimeBetweenEvictionRunsMillis(value);

            // in each eviction run, ecict at least a fourth of "maxIdle"
            // connections
            int maxIdle = ((GenericObjectPool) connectionPool).getMaxIdle();
            int numTestsPerEvictionRun = (int) Math.ceil(((double) maxIdle / 4));
            ((GenericObjectPool) connectionPool).setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        }
    }
    // how long may a connection sit idle in the pool before it may be
    // evicted
    if (props.getProperty(ConnectionConstants.PROP_MINEVICTABLEIDLETIMEMILLIS) != null) {
        int value = PropertiesHelper.getInt(ConnectionConstants.PROP_MINEVICTABLEIDLETIMEMILLIS, props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMinEvictableIdleTimeMillis(value);
        }
    }

    // Create a factory to be used by the pool to create the connections
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, dbUser, dbPassword);

    // Create a factory for caching the PreparedStatements
    KeyedObjectPoolFactory kpf = null;
    if (props.getProperty(ConnectionConstants.PROP_MAXSTATEMENTS) != null) {
        int value = PropertiesHelper.getInt(ConnectionConstants.PROP_MAXSTATEMENTS, props, 0);
        if (value > 0) {
            kpf = new StackKeyedObjectPoolFactory(null, value);
        }
    }

    // Wrap the connections and statements with pooled variants
    try {
        String testSQL = null;
        if (props.getProperty(ConnectionConstants.PROP_VALIDATIONQUERY) != null) {
            testSQL = PropertiesHelper.getString(ConnectionConstants.PROP_VALIDATIONQUERY, props, null);
        }
        new PoolableConnectionFactory(connectionFactory, connectionPool, kpf, testSQL, false, false);
        if (testSQL != null) {
            ((GenericObjectPool) connectionPool).setTestOnBorrow(true);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        log.error("could not instantiate DBCP connection pool", ex);
        throw new ConnectionPoolException("DBCP", jdbcDriverClass, jdbcUrl, ex);
    }

    ds = new PoolingDataSource(connectionPool);

    Connection conn = null;
    try {
        conn = ds.getConnection();
        if (conn == null) {
            throw new RuntimeException("DBCP connection pool can't get jdbc connection");
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        JdbcUtils.close(conn);
    }

    String i = props.getProperty(DBConfiguration.JDBC_ISOLATION);
    if (i == null) {
        isolation = null;
    } else {
        isolation = new Integer(i);
        log.info("JDBC isolation level: " + DBConfiguration.isolationLevelToString(isolation.intValue()));
    }

}

From source file:com.glaf.jbpm.connection.DbcpConnectionProvider.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public void configure(Properties props) throws RuntimeException {
    String jdbcDriverClass = props.getProperty(Environment.DRIVER);
    String jdbcUrl = props.getProperty(Environment.URL);
    Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(props);

    log.info("DBCP using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl);
    log.info("Connection properties: " + PropertiesHelper.maskOut(connectionProps, "password"));

    autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props);
    log.info("autocommit mode: " + autocommit);

    if (jdbcDriverClass == null) {
        log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);
    } else {/*w w  w . j ava  2  s  .  co m*/
        try {
            Class.forName(jdbcDriverClass);
        } catch (ClassNotFoundException cnfe) {
            try {
                ClassUtils.classForName(jdbcDriverClass);
            } catch (Exception e) {
                String msg = "JDBC Driver class not found: " + jdbcDriverClass;
                log.error(msg, e);
                throw new RuntimeException(msg, e);
            }
        }
    }

    String dbUser = props.getProperty(Environment.USER);
    String dbPassword = props.getProperty(Environment.PASS);

    if (dbUser == null) {
        dbUser = "";
    }

    if (dbPassword == null) {
        dbPassword = "";
    }

    Properties properties = new Properties();

    for (Iterator<Object> ii = props.keySet().iterator(); ii.hasNext();) {
        String key = (String) ii.next();
        if (key.startsWith("hibernate.dbcp.")) {
            String newKey = key.substring(15);
            properties.put(newKey, props.get(key));
        }
    }

    // Create the actual pool of connections
    ObjectPool connectionPool = new GenericObjectPool(null);

    if (props.getProperty("hibernate.connection.maxIdle") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.maxIdle", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxIdle(value);
        }
    }

    if (props.getProperty("hibernate.connection.minIdle") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.minIdle", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMinIdle(value);
        }
    }

    if (props.getProperty("hibernate.connection.maxActive") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.maxActive", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxActive(value);
        }
    }

    if (props.getProperty("hibernate.connection.maxWait") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.maxWait", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMaxWait(value);
        }
    }

    // how often should the evictor run (if ever, default is -1 = off)
    if (props.getProperty("hibernate.connection.timeBetweenEvictionRunsMillis") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.timeBetweenEvictionRunsMillis", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setTimeBetweenEvictionRunsMillis(value);

            // in each eviction run, ecict at least a fourth of "maxIdle"
            // connections
            int maxIdle = ((GenericObjectPool) connectionPool).getMaxIdle();
            int numTestsPerEvictionRun = (int) Math.ceil(((double) maxIdle / 4));
            ((GenericObjectPool) connectionPool).setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        }
    }
    // how long may a connection sit idle in the pool before it may be
    // evicted
    if (props.getProperty("hibernate.connection.minEvictableIdleTimeMillis") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.minEvictableIdleTimeMillis", props, 0);
        if (value > 0) {
            ((GenericObjectPool) connectionPool).setMinEvictableIdleTimeMillis(value);
        }
    }

    // Create a factory to be used by the pool to create the connections
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, dbUser, dbPassword);

    // Create a factory for caching the PreparedStatements
    KeyedObjectPoolFactory kpf = null;

    if (props.getProperty("hibernate.connection.maxStatements") != null) {
        int value = PropertiesHelper.getInt("hibernate.connection.maxStatements", props, 0);
        if (value > 0) {
            kpf = new StackKeyedObjectPoolFactory(null, value);
        }
    } else {
        kpf = new StackKeyedObjectPoolFactory(null, 200);
    }

    // Wrap the connections and statements with pooled variants
    try {
        String testSQL = null;
        if (props.getProperty("hibernate.connection.testSQL") != null) {
            testSQL = PropertiesHelper.getString("hibernate.connection.testSQL", props, null);
        }
        new PoolableConnectionFactory(connectionFactory, connectionPool, kpf, testSQL, false, false);
        if (testSQL != null) {
            ((GenericObjectPool) connectionPool).setTestOnBorrow(true);
        }
    } catch (Exception e) {
        throw new ConnectionPoolException("DBCP", jdbcDriverClass, jdbcUrl, e);
    }

    ds = new PoolingDataSource(connectionPool);

    Connection conn = null;
    try {
        conn = ds.getConnection();
        if (conn == null) {
            throw new RuntimeException("DBCP connection pool can't get jdbc connection");
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    } finally {
        JdbcUtils.close(conn);
    }

    String i = props.getProperty(Environment.ISOLATION);
    if (i == null) {
        isolation = null;
    } else {
        isolation = new Integer(i);
        log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation.intValue()));
    }

}