Example usage for org.apache.commons.dbcp.managed BasicManagedDataSource setInitialSize

List of usage examples for org.apache.commons.dbcp.managed BasicManagedDataSource setInitialSize

Introduction

In this page you can find the example usage for org.apache.commons.dbcp.managed BasicManagedDataSource setInitialSize.

Prototype

public synchronized void setInitialSize(int initialSize) 

Source Link

Document

Sets the initial size of the connection pool.

Note: this method currently has no effect once the pool has been initialized.

Usage

From source file:com.eurodyn.qlack2.util.datasource.generic.Configurator.java

public void refresh() {
    try {//  w w  w  .java2  s . c om
        Object registeredDs;

        // Configure the database driver.
        Object dbDriver = Class.forName(getDriverClass()).newInstance();
        // To configure the driver in a generic way, we use the
        // driverParametersMapping.
        String[] params = getDriverParametersMapping().split(",");
        for (String param : params) {
            param = param.trim();
            String paramKey = param.trim().split("-")[0];
            String paramValue = param.trim().split("-")[1];
            String property = BeanUtils.getProperty(this, paramValue);
            BeanUtils.setProperty(dbDriver, paramKey, property);
        }

        if (datasourceType.equals("javax.sql.XADataSource")) {
            BasicManagedDataSource managedDs = new BasicManagedDataSource();
            managedDs.setTransactionManager(transactionManager);
            managedDs.setXaDataSourceInstance((XADataSource) dbDriver);
            managedDs.setInitialSize(initialSize);
            managedDs.setMaxActive(maxActive);
            managedDs.setMaxIdle(maxIdle);
            managedDs.setMinIdle(minIdle);
            managedDs.setMaxWait(maxWait);
            managedDs.setValidationQuery(validationQuery);
            managedDs.setTestOnBorrow(testOnBorrow);
            managedDs.setRemoveAbandoned(removeAbandoned);
            managedDs.setRemoveAbandonedTimeout(removeAbandonedTimeout);
            registeredDs = managedDs;
        } else {
            registeredDs = dbDriver;
        }

        // If the service is already registered it should be unregistered first.
        for (ServiceRegistration<?> registration : serviceRegistrations) {
            registration.unregister();
        }

        // Expose datasources using the driver configured above.
        // This bundle accepts multiple jndi names as a comma-separated list so we
        // expose as many services as the provided jndi names.
        String[] jndiNames = jndiName.split(",");
        for (String name : jndiNames) {
            Dictionary<String, String> registrationProperties = new Hashtable<String, String>();
            registrationProperties.put("osgi.jndi.service.name", name);
            ServiceRegistration<?> registration = context.registerService(DataSource.class.getName(),
                    registeredDs, registrationProperties);
            serviceRegistrations.add(registration);
            LOGGER.log(Level.CONFIG, "Registered Datasource for {0} under {1}.",
                    new String[] { getDriverClass(), name });
        }
    } catch (ClassNotFoundException e) {
        LOGGER.log(Level.SEVERE, MessageFormat.format("Could not find database driver {0}.", getDriverClass()),
                e);
    } catch (InvocationTargetException | InstantiationException | IllegalAccessException
            | NoSuchMethodException e) {
        LOGGER.log(Level.SEVERE,
                MessageFormat.format("Could not instantiate database driver {0}.", getDriverClass()), e);
    }
}

From source file:org.nuxeo.runtime.datasource.BasicManagedDataSourceFactory.java

/**
 * Creates and configures a {@link BasicManagedDataSource} instance based on
 * the given properties./*  w  w  w  .j av a2  s .c  om*/
 *
 * @param properties the datasource configuration properties
 * @throws Exception if an error occurs creating the data source
 */
public static DataSource createDataSource(Properties properties) throws Exception {
    BasicManagedDataSource dataSource = new BasicManagedDataSource();

    String value = properties.getProperty(PROP_DEFAULTAUTOCOMMIT);
    if (value != null) {
        dataSource.setDefaultAutoCommit(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_DEFAULTREADONLY);
    if (value != null) {
        dataSource.setDefaultReadOnly(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_DEFAULTTRANSACTIONISOLATION);
    if (value != null) {
        int level = UNKNOWN_TRANSACTIONISOLATION;
        if ("NONE".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_NONE;
        } else if ("READ_COMMITTED".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_READ_COMMITTED;
        } else if ("READ_UNCOMMITTED".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_READ_UNCOMMITTED;
        } else if ("REPEATABLE_READ".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_REPEATABLE_READ;
        } else if ("SERIALIZABLE".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_SERIALIZABLE;
        } else {
            try {
                level = Integer.parseInt(value);
            } catch (NumberFormatException e) {
                System.err.println("Could not parse defaultTransactionIsolation: " + value);
                System.err.println("WARNING: defaultTransactionIsolation not set");
                System.err.println("using default value of database driver");
                level = UNKNOWN_TRANSACTIONISOLATION;
            }
        }
        dataSource.setDefaultTransactionIsolation(level);
    }

    value = properties.getProperty(PROP_DEFAULTCATALOG);
    if (value != null) {
        dataSource.setDefaultCatalog(value);
    }

    value = properties.getProperty(PROP_DRIVERCLASSNAME);
    if (value != null) {
        dataSource.setDriverClassName(value);
    }

    value = properties.getProperty(PROP_MAXACTIVE);
    if (value != null) {
        dataSource.setMaxActive(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXIDLE);
    if (value != null) {
        dataSource.setMaxIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MINIDLE);
    if (value != null) {
        dataSource.setMinIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_INITIALSIZE);
    if (value != null) {
        dataSource.setInitialSize(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXWAIT);
    if (value != null) {
        dataSource.setMaxWait(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_TESTONBORROW);
    if (value != null) {
        dataSource.setTestOnBorrow(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TESTONRETURN);
    if (value != null) {
        dataSource.setTestOnReturn(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TIMEBETWEENEVICTIONRUNSMILLIS);
    if (value != null) {
        dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_NUMTESTSPEREVICTIONRUN);
    if (value != null) {
        dataSource.setNumTestsPerEvictionRun(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MINEVICTABLEIDLETIMEMILLIS);
    if (value != null) {
        dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_TESTWHILEIDLE);
    if (value != null) {
        dataSource.setTestWhileIdle(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_PASSWORD);
    if (value != null) {
        dataSource.setPassword(value);
    }

    value = properties.getProperty(PROP_URL);
    if (value != null) {
        dataSource.setUrl(value);
    }

    value = properties.getProperty(PROP_USERNAME);
    if (value != null) {
        dataSource.setUsername(value);
    }

    value = properties.getProperty(PROP_VALIDATIONQUERY);
    if (value != null) {
        dataSource.setValidationQuery(value);
    }

    value = properties.getProperty(PROP_VALIDATIONQUERY_TIMEOUT);
    if (value != null) {
        dataSource.setValidationQueryTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED);
    if (value != null) {
        dataSource.setAccessToUnderlyingConnectionAllowed(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_REMOVEABANDONED);
    if (value != null) {
        dataSource.setRemoveAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_REMOVEABANDONEDTIMEOUT);
    if (value != null) {
        dataSource.setRemoveAbandonedTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_LOGABANDONED);
    if (value != null) {
        dataSource.setLogAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_POOLPREPAREDSTATEMENTS);
    if (value != null) {
        dataSource.setPoolPreparedStatements(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_MAXOPENPREPAREDSTATEMENTS);
    if (value != null) {
        dataSource.setMaxOpenPreparedStatements(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_INITCONNECTIONSQLS);
    if (value != null) {
        StringTokenizer tokenizer = new StringTokenizer(value, ";");
        dataSource.setConnectionInitSqls(Collections.list(tokenizer));
    }

    value = properties.getProperty(PROP_CONNECTIONPROPERTIES);
    if (value != null) {
        Properties p = getProperties(value);
        Enumeration<?> e = p.propertyNames();
        while (e.hasMoreElements()) {
            String propertyName = (String) e.nextElement();
            dataSource.addConnectionProperty(propertyName, p.getProperty(propertyName));
        }
    }

    // Managed: initialize XADataSource

    value = properties.getProperty(PROP_XADATASOURCE);
    if (value != null) {
        Class<?> xaDataSourceClass;
        try {
            xaDataSourceClass = Class.forName(value);
        } catch (Throwable t) {
            throw (SQLException) new SQLException("Cannot load XA data source class '" + value + "'")
                    .initCause(t);
        }
        XADataSource xaDataSource;
        try {
            xaDataSource = (XADataSource) xaDataSourceClass.newInstance();
        } catch (Throwable t) {
            throw (SQLException) new SQLException("Cannot create XA data source of class '" + value + "'")
                    .initCause(t);
        }
        dataSource.setXaDataSourceInstance(xaDataSource);
    }

    // DBCP-215
    // Trick to make sure that initialSize connections are created
    if (dataSource.getInitialSize() > 0) {
        dataSource.getLogWriter();
    }

    // Return the configured DataSource instance
    return dataSource;
}

From source file:org.obiba.opal.core.runtime.jdbc.DataSourceFactoryBean.java

@Override
public DataSource getObject() {
    log.debug("Configure DataSource for {}", url);
    BasicManagedDataSource dataSource = new BasicManagedDataSource();
    dataSource.setTransactionManager(jtaTransactionManager);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(url);//from  ww w  . j  a va  2s  .co  m
    setConnectionProperties(dataSource);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setInitialSize(MIN_POOL_SIZE);
    dataSource.setMaxActive(MAX_POOL_SIZE);
    dataSource.setMaxIdle(MAX_IDLE);
    dataSource.setTestOnBorrow(true);
    dataSource.setTestWhileIdle(false);
    dataSource.setTestOnReturn(false);
    dataSource.setDefaultAutoCommit(false);
    dataSource.setValidationQuery(guessValidationQuery());
    return dataSource;
}