Example usage for org.apache.commons.dbcp.datasources PerUserPoolDataSource PerUserPoolDataSource

List of usage examples for org.apache.commons.dbcp.datasources PerUserPoolDataSource PerUserPoolDataSource

Introduction

In this page you can find the example usage for org.apache.commons.dbcp.datasources PerUserPoolDataSource PerUserPoolDataSource.

Prototype

public PerUserPoolDataSource() 

Source Link

Document

Default no-arg constructor for Serialization

Usage

From source file:org.apache.synapse.commons.datasource.factory.DataSourceFactory.java

/**
 * Factory method to create a DataSource based on provided information
 * which is encapsulated in the DataSourceInformation object.
 *
 * @param dataSourceInformation Information about DataSource
 * @return DataSource Instance if one can be created ,
 *         otherwise null or exception if provided details are not valid or enough to create
 *         a DataSource//from w w  w .j  a  v  a2 s .  co  m
 */
public static DataSource createDataSource(DataSourceInformation dataSourceInformation) {

    String dsType = dataSourceInformation.getType();
    String driver = dataSourceInformation.getDriver();

    if (driver == null || "".equals(driver)) {
        handleException("Database driver class name cannot be found.");
    }

    String url = dataSourceInformation.getUrl();

    if (url == null || "".equals(url)) {
        handleException("Database connection URL cannot be found.");
    }

    String user = dataSourceInformation.getSecretInformation().getUser();
    String password = dataSourceInformation.getSecretInformation().getResolvedSecret();

    int defaultTransactionIsolation = dataSourceInformation.getDefaultTransactionIsolation();

    if (DataSourceInformation.BASIC_DATA_SOURCE.equals(dsType)) {

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(driver);
        basicDataSource.setUrl(url);

        if (user != null && !"".equals(user)) {
            basicDataSource.setUsername(user);
        }

        if (password != null && !"".equals(password)) {
            basicDataSource.setPassword(password);
        }

        basicDataSource.setMaxActive(dataSourceInformation.getMaxActive());
        basicDataSource.setMaxIdle(dataSourceInformation.getMaxIdle());
        basicDataSource.setMaxWait(dataSourceInformation.getMaxWait());
        basicDataSource.setMinIdle(dataSourceInformation.getMinIdle());
        basicDataSource.setDefaultAutoCommit(dataSourceInformation.isDefaultAutoCommit());
        basicDataSource.setDefaultReadOnly(dataSourceInformation.isDefaultReadOnly());
        basicDataSource.setTestOnBorrow(dataSourceInformation.isTestOnBorrow());
        basicDataSource.setTestOnReturn(dataSourceInformation.isTestOnReturn());
        basicDataSource.setTestWhileIdle(dataSourceInformation.isTestWhileIdle());
        basicDataSource.setMinEvictableIdleTimeMillis(dataSourceInformation.getMinEvictableIdleTimeMillis());
        basicDataSource
                .setTimeBetweenEvictionRunsMillis(dataSourceInformation.getTimeBetweenEvictionRunsMillis());
        basicDataSource.setNumTestsPerEvictionRun(dataSourceInformation.getNumTestsPerEvictionRun());
        basicDataSource.setMaxOpenPreparedStatements(dataSourceInformation.getMaxOpenPreparedStatements());
        basicDataSource.setAccessToUnderlyingConnectionAllowed(
                dataSourceInformation.isAccessToUnderlyingConnectionAllowed());
        basicDataSource.setInitialSize(dataSourceInformation.getInitialSize());
        basicDataSource.setPoolPreparedStatements(dataSourceInformation.isPoolPreparedStatements());

        if (defaultTransactionIsolation != -1) {
            basicDataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
        }

        String defaultCatalog = dataSourceInformation.getDefaultCatalog();
        if (defaultCatalog != null && !"".equals(defaultCatalog)) {
            basicDataSource.setDefaultCatalog(defaultCatalog);
        }

        String validationQuery = dataSourceInformation.getValidationQuery();

        if (validationQuery != null && !"".equals(validationQuery)) {
            basicDataSource.setValidationQuery(validationQuery);
        }

        return basicDataSource;

    } else if (DataSourceInformation.PER_USER_POOL_DATA_SOURCE.equals(dsType)) {

        DriverAdapterCPDS adapterCPDS = new DriverAdapterCPDS();

        try {
            adapterCPDS.setDriver(driver);
        } catch (ClassNotFoundException e) {
            handleException("Error setting driver : " + driver + " in DriverAdapterCPDS", e);
        }

        adapterCPDS.setUrl(url);

        if (user != null && !"".equals(user)) {
            adapterCPDS.setUser(user);
        }

        if (password != null && !"".equals(password)) {
            adapterCPDS.setPassword(password);
        }

        adapterCPDS.setPoolPreparedStatements(dataSourceInformation.isPoolPreparedStatements());
        adapterCPDS.setMaxIdle(dataSourceInformation.getMaxIdle());

        PerUserPoolDataSource perUserPoolDataSource = new PerUserPoolDataSource();
        perUserPoolDataSource.setConnectionPoolDataSource(adapterCPDS);

        perUserPoolDataSource.setDefaultMaxActive(dataSourceInformation.getMaxActive());
        perUserPoolDataSource.setDefaultMaxIdle(dataSourceInformation.getMaxIdle());
        perUserPoolDataSource.setDefaultMaxWait((int) dataSourceInformation.getMaxWait());
        perUserPoolDataSource.setDefaultAutoCommit(dataSourceInformation.isDefaultAutoCommit());
        perUserPoolDataSource.setDefaultReadOnly(dataSourceInformation.isDefaultReadOnly());
        perUserPoolDataSource.setTestOnBorrow(dataSourceInformation.isTestOnBorrow());
        perUserPoolDataSource.setTestOnReturn(dataSourceInformation.isTestOnReturn());
        perUserPoolDataSource.setTestWhileIdle(dataSourceInformation.isTestWhileIdle());
        perUserPoolDataSource
                .setMinEvictableIdleTimeMillis((int) dataSourceInformation.getMinEvictableIdleTimeMillis());
        perUserPoolDataSource.setTimeBetweenEvictionRunsMillis(
                (int) dataSourceInformation.getTimeBetweenEvictionRunsMillis());
        perUserPoolDataSource.setNumTestsPerEvictionRun(dataSourceInformation.getNumTestsPerEvictionRun());

        if (defaultTransactionIsolation != -1) {
            perUserPoolDataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
        }

        String validationQuery = dataSourceInformation.getValidationQuery();

        if (validationQuery != null && !"".equals(validationQuery)) {
            perUserPoolDataSource.setValidationQuery(validationQuery);
        }

        return perUserPoolDataSource;

    } else {
        handleException("Unsupported DataSource : " + dsType);
    }
    return null;
}

From source file:org.apache.torque.dsfactory.PerUserPoolDataSourceFactory.java

/**
 * Initializes the Jdbc2PoolDataSource.//from ww  w.java 2 s .  c  o  m
 *
 * @param configuration where to read the settings from
 * @throws TorqueException if a property set fails
 * @return a configured <code>Jdbc2PoolDataSource</code>
 */
private PerUserPoolDataSource initJdbc2Pool(Configuration configuration) throws TorqueException {
    log.debug("Starting initJdbc2Pool");
    PerUserPoolDataSource dataSource = new PerUserPoolDataSource();
    Configuration c = Torque.getConfiguration();

    if (c == null || c.isEmpty()) {
        log.warn("Global Configuration not set," + " no Default pool data source configured!");
    } else {
        Configuration conf = c.subset(DEFAULT_POOL_KEY);
        applyConfiguration(conf, dataSource);
    }

    Configuration conf = configuration.subset(POOL_KEY);
    applyConfiguration(conf, dataSource);
    return dataSource;
}

From source file:org.wsm.database.tools.util.BaseLoadTestConnectionManager.java

private static PerUserPoolDataSource prepareForConnectionPooling(ConnectionPoolDataSource cpds) {
    log.debug("Preparing ConnectionPoolDataSource with the default properties ");
    PerUserPoolDataSource pupds = new PerUserPoolDataSource();
    pupds.setConnectionPoolDataSource(cpds);
    pupds.setDefaultMaxActive(0);//w w  w .  j a  v a  2  s .  co m
    pupds.setDefaultMaxIdle(8);
    return pupds;
}

From source file:org.wsm.database.tools.util.ConnectionManager.java

private static void determineDataSource(DBConnectionProperties props)
        throws ConnectionPoolDataSourceCreationException {
    File f = new File(DbUtilConstants.DBUTIL_HOME);
    File f2 = new File(f, "dataSourceLogs.txt");
    PrintWriter pw = null;//from  w  ww. j  a  v  a  2  s  . co  m
    try {
        if (!f.exists()) {
            if (f.canWrite()) {
                f2.createNewFile();
            }
        }
        if (f2.exists())
            pw = new PrintWriter(new FileOutputStream(f2));
    } catch (IOException e) {
        e.printStackTrace();
    }
    ConnectionPoolDataSource cpds = null;

    if (DbUtilConstants.DATABASE_TYPE_ORACLE.equals(props.getDatabaseType())) {
        try {
            OracleConnectionPoolDataSource tempOcpds = new OracleConnectionPoolDataSource();
            tempOcpds.setUser(userName);
            tempOcpds.setPassword(password);
            tempOcpds.setURL(connUrl);
            cpds = tempOcpds;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    } else if (DbUtilConstants.DATABASE_TYPE_MYSQL.equals(props.getDatabaseType())) {
        MysqlConnectionPoolDataSource tempCpds = new MysqlConnectionPoolDataSource();
        tempCpds.setUser(userName);
        tempCpds.setPassword(password);
        tempCpds.setURL(connUrl);
        cpds = tempCpds;
    }
    if (pw != null) {
        try {
            if (cpds != null) {
                cpds.setLogWriter(pw);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (getPupds() != null) {
        getPupds().close();
    }
    setPupds(null);
    if (getPupds() == null)
        setPupds(new PerUserPoolDataSource());
    if (cpds != null)
        getPupds().setConnectionPoolDataSource(cpds);
    else
        throw new ConnectionPoolDataSourceCreationException("Unable to create Data Source");

}