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) 

Source Link

Document

Create a new GenericObjectPool using defaults from GenericObjectPoolConfig .

Usage

From source file:org.hawk.orientdb.OrientDatabase.java

public void run(String iURL, File parentfolder, IConsole c) throws Exception {
    this.storageFolder = parentfolder;
    this.tempFolder = new File(storageFolder, "temp");
    this.console = c;

    OGlobalConfiguration.OBJECT_SAVE_ONLY_DIRTY.setValue(true);
    OGlobalConfiguration.SBTREE_MAX_KEY_SIZE.setValue(102_400);

    // Add a Guava-based Orient cache as default unless user specified something else
    @SuppressWarnings("unchecked")
    OConfigurableStatefulFactory<String, ORecordCache> factory = (OConfigurableStatefulFactory<String, ORecordCache>) Orient
            .instance().getLocalRecordCache();
    factory.register(ORecordCacheGuava.class.getName(), ORecordCacheGuava.class);
    if (System.getProperty(OGlobalConfiguration.CACHE_LOCAL_IMPL.getKey()) == null) {
        OGlobalConfiguration.CACHE_LOCAL_IMPL.setValue(ORecordCacheGuava.class.getName());
    }//from   w ww  .jav  a  2  s . com

    console.println("Starting database " + iURL);
    this.dbURL = iURL;

    pool = new GenericObjectPool<>(new OrientConnectionFactory());
    pool.setMinIdle(0);
    pool.setMaxIdle(2);
    pool.setMaxTotal(getPoolSize());
    pool.setMinEvictableIdleTimeMillis(20_000);
    pool.setBlockWhenExhausted(true);

    metamodelIndex = getOrCreateNodeIndex(METAMODEL_IDX_NAME);
    fileIndex = getOrCreateNodeIndex(FILE_IDX_NAME);

    // By default, we're on transactional mode
    exitBatchMode();
}

From source file:org.jboss.narayana.quickstart.spring.config.DatabaseConfig.java

@Bean
public DataSource dataSource() {
    DataSourceXAConnectionFactory dataSourceXAConnectionFactory = new DataSourceXAConnectionFactory(tm,
            h2DataSource());/*  w  ww .  j  a  v a2  s  .c  o m*/
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
            dataSourceXAConnectionFactory, null);
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    return new ManagedDataSource<>(connectionPool, dataSourceXAConnectionFactory.getTransactionRegistry());
}

From source file:org.jfastcgi.client.PooledConnectionFactory.java

public PooledConnectionFactory(final PooledObjectFactory<ISocket> pooledObjectFactory) {
    pool = new GenericObjectPool<ISocket>(pooledObjectFactory);
}

From source file:org.jivesoftware.openfire.websocket.XmppWebSocket.java

private synchronized void initializePool() {
    if (readerPool == null) {
        readerPool = new GenericObjectPool<XMPPPacketReader>(new XMPPPPacketReaderFactory());
        readerPool.setMaxTotal(-1);/*from www .j  a v  a2  s .  c o  m*/
        readerPool.setBlockWhenExhausted(false);
        readerPool.setTestOnReturn(true);
        readerPool.setTimeBetweenEvictionRunsMillis(JiveConstants.MINUTE);
    }
}

From source file:org.kie.workbench.common.screens.datasource.management.backend.core.dbcp.DBCPDataSourceProvider.java

@Override
public DataSourceDeploymentInfo deploy(DataSourceDef dataSourceDef) throws Exception {

    DriverDef driverDef = null;/*from w  w w .j  a  va  2  s  .c om*/
    for (DriverDef _driverDef : driverProvider.getDeployments()) {
        if (_driverDef.getUuid().equals(dataSourceDef.getDriverUuid())) {
            driverDef = _driverDef;
            break;
        }
    }

    if (driverDef == null) {
        throw new Exception("Required driver: " + dataSourceDef.getDriverUuid() + " is not deployed");
    }

    final URI uri = artifactResolver.resolve(driverDef.getGroupId(), driverDef.getArtifactId(),
            driverDef.getVersion());
    if (uri == null) {
        throw new Exception("Unable to get driver library artifact for driver: " + driverDef);
    }

    final Properties properties = new Properties();
    properties.setProperty("user", dataSourceDef.getUser());
    properties.setProperty("password", dataSourceDef.getPassword());
    final URLConnectionFactory urlConnectionFactory = buildConnectionFactory(uri, driverDef.getDriverClass(),
            dataSourceDef.getConnectionURL(), properties);

    //Connection Factory that the pool will use for creating connections.
    ConnectionFactory connectionFactory = new DBCPConnectionFactory(urlConnectionFactory);

    //Poolable connection factory
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    //The pool to be used by the ConnectionFactory
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    //Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //Finally create DataSource
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    DataSourceDeploymentInfo deploymentInfo = new DataSourceDeploymentInfo(dataSourceDef.getUuid(), true,
            dataSourceDef.getUuid(), false);

    deploymentRegistry.put(deploymentInfo.getDeploymentId(), new DBCPDataSource(dataSource));
    deploymentInfos.put(deploymentInfo.getDeploymentId(), deploymentInfo);
    deployedDataSources.put(deploymentInfo.getDeploymentId(), dataSourceDef);

    return deploymentInfo;
}

From source file:org.moneta.config.ConnectionPoolFactory.java

public static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType) {
    Validate.notNull(dataSourceType, "Null MonetaDataSource not allowed.");

    Validate.notEmpty(dataSourceType.getDataSourceName(), "Null or blank name not allowed");
    Validate.notEmpty(dataSourceType.getConnectionUrl(), "Null or blank url not allowed");
    Validate.notNull(dataSourceType.getDriver(), "Null driver not allowed");

    try {/*from ww w.j a va 2 s  .  c o  m*/
        dataSourceType.getDriver().newInstance();
    } catch (Exception e) {
        throw new MonetaException("Data source JDBC driver can't be instantiated", e)
                .addContextValue("JDBC Driver class", dataSourceType.getDriver().getName());
    }

    Properties connectionProps = new Properties();
    connectionProps.putAll(dataSourceType.getJdbcConnectionProperties());
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dataSourceType.getConnectionUrl(),
            connectionProps);

    ObjectName poolName = null;
    try {
        poolName = new ObjectName("org.moneta", "connectionPool", dataSourceType.getDataSourceName());
    } catch (MalformedObjectNameException e) {
        throw new MonetaException("Data source name not valid", e).addContextValue("Data source name",
                dataSourceType.getDataSourceName());
    }
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);

    Set<String> unusedConnectionPoolPropSet = new HashSet<String>();
    Map<String, String> propMap = new HashMap<String, String>(dataSourceType.getConnectionPoolProperties());

    PropertyDescriptor pDesc = null;
    for (String propName : propMap.keySet()) {
        pDesc = assignProperty(dataSourceType.getDataSourceName(), poolableConnectionFactory, propName,
                propMap.get(propName));
        if (pDesc == null) {
            unusedConnectionPoolPropSet.add(propName);
        }
    }

    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(pool);

    for (String propName : new HashSet<String>(unusedConnectionPoolPropSet)) {
        pDesc = assignProperty(dataSourceType.getDataSourceName(), pool, propName, propMap.get(propName));
        if (pDesc != null) {
            unusedConnectionPoolPropSet.remove(propName);
        }
    }

    if (unusedConnectionPoolPropSet.size() > 0) {
        MonetaException me = new MonetaException("Invalid connection pool properties detected");
        for (String propName : unusedConnectionPoolPropSet) {
            me.addContextValue("pool property name", propName);
        }

        throw me;
    }
    return pool;
}

From source file:org.moneta.healthcheck.DbcpConnectionPoolHealthCheckTest.java

@Before
public void setUp() throws Exception {
    org.hsqldb.jdbc.JDBCDriver.class.newInstance();
    Properties connectionProps = new Properties();
    connectionProps.put("user", "SA");
    connectionProps.put("password", "");
    connectionProps.put("create", "true");

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:hsqldb:mem:my-sample",
            connectionProps);/*from   w w  w  .  ja v  a2  s.c  o  m*/

    ObjectName poolName = poolName = new ObjectName("org.moneta", "connectionPool", "TestPool");
    poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, poolName);
    poolableConnectionFactory.setDefaultCatalog("PUBLIC");
    poolableConnectionFactory.setValidationQuery(VALIDATION_SQL);

    connectionPool = new GenericObjectPool<PoolableConnection>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    connectionPool.setMaxTotal(2);
    connectionPool.setMaxWaitMillis(400);

    healthCheck = new DbcpConnectionPoolHealthCheck(connectionPool, "mine");
}

From source file:org.ranksys.compression.codecs.lemire.FastPFORVBCODEC.java

@Override
public int[] co(int[] in, int offset, int len) {
    IntegerCODEC pfor;/* www  .j  a va  2 s  .  co  m*/
    try {
        if (pool == null) {
            synchronized (this) {
                if (pool == null) {
                    pool = new GenericObjectPool<>(new FastPFORFactory());
                }
            }
        }
        pfor = pool.borrowObject();
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, null, ex);
        return null;
    }
    int[] out = new int[len + 1024];
    IntWrapper inputoffset = new IntWrapper(offset);
    IntWrapper outputoffset = new IntWrapper(1);
    pfor.compress(in, inputoffset, len, out, outputoffset);
    out[0] = outputoffset.get() - 1;
    out = Arrays.copyOf(out, outputoffset.get());
    pool.returnObject(pfor);
    add(len * Integer.BYTES, outputoffset.intValue() * Integer.BYTES);

    return out;
}

From source file:org.ranksys.compression.codecs.lemire.FastPFORVBCODEC.java

@Override
public int dec(int[] in, int[] out, int outOffset, int len) {
    IntegerCODEC pfor;//from  w w  w  . j  a  v  a2 s . co m
    try {
        if (pool == null) {
            synchronized (this) {
                if (pool == null) {
                    pool = new GenericObjectPool<>(new FastPFORFactory());
                }
            }
        }
        pfor = pool.borrowObject();
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, null, ex);
        return -1;
    }
    int nInts = in[0];
    IntWrapper inputoffset = new IntWrapper(1);
    IntWrapper outputoffset = new IntWrapper(outOffset);
    pfor.uncompress(in, inputoffset, nInts, out, outputoffset);
    pool.returnObject(pfor);

    return inputoffset.get();
}

From source file:org.springframework.boot.actuate.metrics.ambari.pool.MetricObjectPool.java

public MetricObjectPool(int timelineMetricsPoolSize, int timelineMeticPoolSize) {
    this.timelineMetricPool = new GenericObjectPool<TimelineMetric>(new TimelineMetricFactory());
    this.timelineMetricPool.setMaxTotal(timelineMeticPoolSize);
    this.timelineMetricPool.setMaxWaitMillis(FIVE_SECONDS);

    this.timelineMetricsPool = new GenericObjectPool<TimelineMetrics>(new TimelineMetricsFactory());
    this.timelineMetricsPool.setMaxTotal(timelineMetricsPoolSize);
    this.timelineMetricsPool.setMaxWaitMillis(FIVE_SECONDS);
}