Example usage for org.apache.commons.pool2.impl GenericObjectPool GenericObjectPool

List of usage examples for org.apache.commons.pool2.impl GenericObjectPool GenericObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericObjectPool GenericObjectPool.

Prototype

public GenericObjectPool(PooledObjectFactory<T> factory, GenericObjectPoolConfig config) 

Source Link

Document

Create a new GenericObjectPool using a specific configuration.

Usage

From source file:org.ops4j.pax.jdbc.pool.dbcp2.impl.ds.PooledDataSourceFactory.java

protected DataSource createDataSourceInternal(Properties props, Map<String, String> poolProps)
        throws SQLException {
    DataSource ds = dsFactory.createDataSource(props);
    DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory(ds);
    PoolableConnectionFactory pcf = new PoolableConnectionFactory(connFactory, null);
    GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
    BeanConfig.configure(conf, poolProps);
    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf, conf);
    return new CloseablePoolingDataSource<PoolableConnection>(pool);
}

From source file:org.ops4j.pax.jdbc.pool.dbcp2.impl.ds.XAPooledDataSourceFactory.java

@Override
protected DataSource createDataSourceInternal(Properties props, Map<String, String> poolProps)
        throws SQLException {
    XADataSource ds = dsFactory.createXADataSource(props);
    DataSourceXAConnectionFactory connFactory = new DataSourceXAConnectionFactory(tm, ds);
    PoolableManagedConnectionFactory pcf = new PoolableManagedConnectionFactory(connFactory, null);
    GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
    BeanConfig.configure(conf, poolProps);
    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf, conf);
    TransactionRegistry transactionRegistry = connFactory.getTransactionRegistry();
    return new CloseableManagedDataSource<PoolableConnection>(pool, transactionRegistry);
}

From source file:org.ops4j.pax.jdbc.pool.narayana.impl.DbcpXAPooledDataSourceFactory.java

@Override
public DataSource create(DataSourceFactory dsf, Properties props) throws SQLException {
    try {/*from w w  w  .ja va  2s  . com*/
        final XADataSource ds = dsf.createXADataSource(getNonPoolProps(props));
        DataSourceXAConnectionFactory connFactory = new DataSourceXAConnectionFactory(tm, (XADataSource) ds);
        PoolableManagedConnectionFactory pcf = new PoolableManagedConnectionFactory(connFactory, null);
        GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
        BeanConfig.configure(conf, getPoolProps(props));
        BeanConfig.configure(pcf, getPrefixed(props, FACTORY_PREFIX));
        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf, conf);
        pcf.setPool(pool);
        TransactionRegistry transactionRegistry = connFactory.getTransactionRegistry();
        final ServiceRegistration<XAResourceRecovery> registration = bundleContext
                .registerService(XAResourceRecovery.class, new XAResourceRecovery() {
                    @Override
                    public XAResource[] getXAResources() {
                        try {
                            return new XAResource[] { new Wrapper(ds.getXAConnection()) };
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }, null);
        ManagedDataSource<PoolableConnection> mds = new ManagedDataSource<PoolableConnection>(pool,
                transactionRegistry) {
            @Override
            public void close() throws Exception {
                registration.unregister();
                super.close();
            }
        };
        return mds;
    } catch (Throwable e) {
        LOG.error("Error creating pooled datasource" + e.getMessage(), e);
        if (e instanceof SQLException) {
            throw (SQLException) e;
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

From source file:org.ops4j.pax.jdbc.pool.narayana.impl.ds.DbcpXAPooledDataSourceFactory.java

@Override
public DataSource createDataSource(Properties props) throws SQLException {
    try {/* www  .  java2  s. c om*/
        final XADataSource ds = dsFactory.createXADataSource(getNonPoolProps(props));
        DataSourceXAConnectionFactory connFactory = new DataSourceXAConnectionFactory(tm, (XADataSource) ds);
        PoolableManagedConnectionFactory pcf = new PoolableManagedConnectionFactory(connFactory, null);
        GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
        BeanConfig.configure(conf, getPoolProps(props));
        BeanConfig.configure(pcf, getPrefixed(props, FACTORY_PREFIX));
        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf, conf);
        pcf.setPool(pool);
        TransactionRegistry transactionRegistry = connFactory.getTransactionRegistry();
        final ServiceRegistration<XAResourceRecovery> registration = bundleContext
                .registerService(XAResourceRecovery.class, new XAResourceRecovery() {
                    @Override
                    public XAResource[] getXAResources() {
                        try {
                            return new XAResource[] { new Wrapper(ds.getXAConnection()) };
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }, null);
        ManagedDataSource mds = new ManagedDataSource<PoolableConnection>(pool, transactionRegistry) {
            @Override
            public void close() throws Exception {
                registration.unregister();
                super.close();
            }
        };
        return mds;
    } catch (Throwable e) {
        LOG.error("Error creating pooled datasource" + e.getMessage(), e);
        if (e instanceof SQLException) {
            throw (SQLException) e;
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

From source file:org.springframework.aop.target.CommonsPool2TargetSource.java

/**
 * Subclasses can override this if they want to return a specific Commons pool.
 * They should apply any configuration properties to the pool here.
 * <p>Default is a GenericObjectPool instance with the given pool size.
 * @return an empty Commons {@code ObjectPool}.
 * @see GenericObjectPool/*w  ww  .j av a2 s  . com*/
 * @see #setMaxSize
 */
protected ObjectPool createObjectPool() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(getMaxSize());
    config.setMaxIdle(getMaxIdle());
    config.setMinIdle(getMinIdle());
    config.setMaxWaitMillis(getMaxWait());
    config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
    config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
    config.setBlockWhenExhausted(isBlockWhenExhausted());
    return new GenericObjectPool(this, config);
}

From source file:org.springframework.data.redis.connection.jredis.JredisPool.java

/**
 * Uses the {@link Config} defaults for configuring the connection pool
 * /*  w  w  w .  j  av a 2s  .  c  om*/
 * @param connectionSpec The {@link ConnectionSpec} for connecting to Redis
 */
public JredisPool(ConnectionSpec connectionSpec) {
    this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec),
            new GenericObjectPoolConfig());
}

From source file:org.springframework.data.redis.connection.jredis.JredisPool.java

/**
 * @param connectionSpec The {@link ConnectionSpec} for connecting to Redis
 * @param poolConfig The pool {@link Config}
 *//*from ww w.  j  av a2  s  .c  o  m*/
public JredisPool(ConnectionSpec connectionSpec, GenericObjectPoolConfig poolConfig) {
    this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec), poolConfig);
}

From source file:org.springframework.data.redis.connection.jredis.JredisPool.java

/**
 * @param hostName The Redis host/*w w  w  .  j a  va2s. co m*/
 * @param port The Redis port
 * @param dbIndex The index of the database all connections should use
 * @param password The password used for authenticating with the Redis server or null if no password required
 * @param timeout The socket timeout or 0 to use the default socket timeout
 * @param poolConfig The pool {@link Config}
 */
public JredisPool(String hostName, int port, int dbIndex, String password, int timeout,
        GenericObjectPoolConfig poolConfig) {
    ConnectionSpec connectionSpec = DefaultConnectionSpec.newSpec(hostName, port, dbIndex, null);
    connectionSpec.setConnectionFlag(Connection.Flag.RELIABLE, false);
    if (StringUtils.hasLength(password)) {
        connectionSpec.setCredentials(password);
    }
    if (timeout > 0) {
        connectionSpec.setSocketProperty(Property.SO_TIMEOUT, timeout);
    }
    this.internalPool = new GenericObjectPool<JRedis>(new JredisFactory(connectionSpec), poolConfig);
}

From source file:org.springframework.data.redis.connection.lettuce.DefaultLettucePool.java

@SuppressWarnings({ "rawtypes" })
public void afterPropertiesSet() {

    if (clientResources != null) {
        this.client = RedisClient.create(clientResources, getRedisURI());
    } else {//  ww w  .j a  va2s. co  m
        this.client = RedisClient.create(getRedisURI());
    }

    client.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS);
    this.internalPool = new GenericObjectPool<RedisAsyncConnection>(new LettuceFactory(client, dbIndex),
            poolConfig);
}

From source file:org.wso2.carbon.transport.jms.factory.JMSClientConnectionFactory.java

/**
 * Initialize the session pool with provided configuration.
 *//*from   w  ww .j av a  2  s  . com*/
private void initSessionPool() {
    SessionPoolFactory sessionPoolFactory = new SessionPoolFactory(this);

    //create pool configurations
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(maxNumberOfConnections * maxSessionsPerConnection);
    //todo: set the ideal limit and make the idle sessions timedout
    config.setMaxIdle(maxNumberOfConnections * maxSessionsPerConnection);
    config.setBlockWhenExhausted(true);
    config.setMaxWaitMillis(poolWaitTimeout);

    //initialize the pool
    sessionPool = new GenericObjectPool<SessionWrapper>(sessionPoolFactory, config);
}