Example usage for org.apache.commons.dbcp BasicDataSource setPassword

List of usage examples for org.apache.commons.dbcp BasicDataSource setPassword

Introduction

In this page you can find the example usage for org.apache.commons.dbcp BasicDataSource setPassword.

Prototype

public synchronized void setPassword(String password) 

Source Link

Document

Sets the #password .

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

Usage

From source file:org.ff4j.test.utils.JdbcTestHelper.java

/**
 * Initialize DataSource with a pool of connections to HQL database.
 *
 * @return/*from   ww  w . j  a v  a  2s . c o  m*/
 *      current data source
 */
public static DataSource createInMemoryHQLDataSource() {
    // Init DataSource
    BasicDataSource dbcpDataSource = new BasicDataSource();
    dbcpDataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dbcpDataSource.setUsername("sa");
    dbcpDataSource.setPassword("");
    dbcpDataSource.setUrl("jdbc:hsqldb:mem:.");
    dbcpDataSource.setMaxActive(3);
    dbcpDataSource.setMaxIdle(2);
    dbcpDataSource.setInitialSize(2);
    dbcpDataSource.setValidationQuery("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS;");
    dbcpDataSource.setPoolPreparedStatements(true);
    return dbcpDataSource;
}

From source file:org.freewheelschedule.freewheel.config.ServerConfig.java

@Bean
public BasicDataSource freewheelDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(hibernateUrl);//from w w w.  j  ava2  s .  c  om
    dataSource.setUsername(hibernateUser);
    dataSource.setPassword(hibernatePassword);
    return dataSource;
}

From source file:org.geosde.core.jdbc.data.datasource.DataSourceUtil.java

/**
 * Builds up a default DBCP DataSource that easy to use connection factories
 * can use to setup a connection pool.//from w w  w. j  a  v a  2 s  . c  o  m
 * 
 * @param url
 *            the jdbc url
 * @param driverName
 *            the jdbc driver full qualified class name
 * @param username
 * @param password
 * @param maxActive maximum number of concurrent connections in the pool
 * @param minIdle minimum number of concurrent connections in the pool
 * @param validationQuery
 *            the validation query to be used for connection liveliness on
 *            borrow, or null, if no check is to be performed
 * @param cachePreparedStatements
 *            wheter to cache prepared statements or not
 * @return
 * @throws DataSourceException
 */
public static ManageableDataSource buildDefaultDataSource(String url, String driverName, String username,
        String password, int maxActive, int minIdle, String validationQuery, boolean cachePreparedStatements,
        int removeAbandonedTimeout) throws DataSourceException {
    // basics
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverName);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    // pool size
    dataSource.setMaxActive(maxActive);
    dataSource.setMinIdle(minIdle);

    // pool eviction settings
    dataSource.setMinEvictableIdleTimeMillis(1000 * 20);
    dataSource.setTimeBetweenEvictionRunsMillis(1000 * 10);

    // connection validation
    if (validationQuery != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(validationQuery);
    }

    // prepared statement cache
    if (cachePreparedStatements) {
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxOpenPreparedStatements(10);
    }

    // remove abandoned connections (I know it's deprecated, but we do want
    // something shaving off lost connections. Let's give them 5 minutes of 
    // continuous usage
    if (removeAbandonedTimeout > 0) {
        dataSource.setRemoveAbandoned(true);
        dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        dataSource.setLogAbandoned(true);
    }

    Connection conn = null;
    try {
        conn = dataSource.getConnection();
    } catch (Exception e) {
        throw new DataSourceException("Connection test failed ", e);
    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
            }
    }

    return new DBCPDataSource(dataSource);
}

From source file:org.geosde.core.jdbc.data.datasource.DBCPDataSourceFactory.java

public DataSource createNewDataSource(Map params) throws IOException {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName((String) DRIVERCLASS.lookUp(params));
    dataSource.setUrl((String) JDBC_URL.lookUp(params));
    dataSource.setUsername((String) USERNAME.lookUp(params));
    dataSource.setPassword((String) PASSWORD.lookUp(params));
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setMaxActive(((Integer) MAXACTIVE.lookUp(params)).intValue());
    dataSource.setMaxIdle(((Integer) MAXIDLE.lookUp(params)).intValue());

    // check the data source is properly setup by trying to gather a connection out of it
    Connection conn = null;/* w  ww .  j  av a2 s.  co  m*/
    try {
        conn = dataSource.getConnection();
    } catch (SQLException e) {
        throw new DataSourceException("Connection pool improperly set up: " + e.getMessage(), e);
    } finally {
        // close the connection at once
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
            }
    }

    return dataSource;
}

From source file:org.geosde.core.jdbc.JDBCDataStoreFactory.java

/**
 * DataSource access allowing SQL use: intended to allow client code to query available schemas.
 * <p>//from  ww w  .j a v a2  s.  co m
 * This DataSource is the clients responsibility to close() when they are finished using it.
 * </p> 
 * @param params Map of connection parameter.
 * @return DataSource for SQL use
 * @throws IOException
 */
public BasicDataSource createDataSource(Map params) throws IOException {
    //create a datasource
    BasicDataSource dataSource = new BasicDataSource();

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username (on embedded dbs it can be optional)
    String user = (String) USER.lookUp(params);
    if (user != null) {
        dataSource.setUsername(user);
    }

    // password
    String passwd = (String) PASSWD.lookUp(params);
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    Integer maxWait = (Integer) MAXWAIT.lookUp(params);
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    Integer minConn = (Integer) MINCONN.lookUp(params);
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    Integer maxConn = (Integer) MAXCONN.lookUp(params);
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    Boolean validate = (Boolean) VALIDATECONN.lookUp(params);
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    Boolean testWhileIdle = (Boolean) TEST_WHILE_IDLE.lookUp(params);
    if (testWhileIdle != null) {
        dataSource.setTestWhileIdle(testWhileIdle);
    }

    Integer timeBetweenEvictorRuns = (Integer) TIME_BETWEEN_EVICTOR_RUNS.lookUp(params);
    if (timeBetweenEvictorRuns != null && timeBetweenEvictorRuns > 0) {
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictorRuns * 1000l);
    }

    Integer minEvictableTime = (Integer) MIN_EVICTABLE_TIME.lookUp(params);
    if (minEvictableTime != null) {
        dataSource.setMinEvictableIdleTimeMillis(minEvictableTime * 1000l);
    }

    Integer evictorTestsPerRun = (Integer) EVICTOR_TESTS_PER_RUN.lookUp(params);
    if (evictorTestsPerRun != null) {
        dataSource.setNumTestsPerEvictionRun(evictorTestsPerRun);
    }

    // some datastores might need this
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    return dataSource;
}

From source file:org.geoserver.security.jdbc.AbstractJDBCService.java

/**
 * initialize a {@link DataSource} form a
 * {@link JdbcSecurityServiceConfig} object
 * //from w w  w .  j  a va 2s  . co  m
 * @param config
 * @throws IOException
 */
public void initializeDSFromConfig(SecurityNamedServiceConfig namedConfig) throws IOException {
    JDBCSecurityServiceConfig config = (JDBCSecurityServiceConfig) namedConfig;
    if (config.isJndi()) {
        String jndiName = config.getJndiName();
        try {
            Context initialContext = new InitialContext();
            datasource = (DataSource) initialContext.lookup(jndiName);
        } catch (NamingException e) {
            throw new IOException(e);
        }
    } else {
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName(config.getDriverClassName());
        bds.setUrl(config.getConnectURL());
        bds.setUsername(config.getUserName());
        bds.setPassword(config.getPassword());
        bds.setDefaultAutoCommit(false);
        bds.setDefaultTransactionIsolation(DEFAULT_ISOLATION_LEVEL);
        bds.setMaxActive(10);
        datasource = bds;
    }
}

From source file:org.geotoolkit.coverage.postgresql.PGCoverageStoreFactory.java

/**
 * Creates the datasource for the coverage store.
 *///from ww  w  .ja v  a  2  s .co  m
private DataSource createDataSource(final ParameterValueGroup params) throws IOException {
    //create a datasource
    final BasicDataSource dataSource = new BasicDataSource();

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username
    final String user = (String) params.parameter(USER.getName().toString()).getValue();
    dataSource.setUsername(user);

    // password
    final String passwd = (String) params.parameter(PASSWORD.getName().toString()).getValue();
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    final Integer maxWait = (Integer) params.parameter(MAXWAIT.getName().toString()).getValue();
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    final Integer minConn = (Integer) params.parameter(MINCONN.getName().toString()).getValue();
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    final Integer maxConn = (Integer) params.parameter(MAXCONN.getName().toString()).getValue();
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    final Boolean validate = (Boolean) params.parameter(VALIDATECONN.getName().toString()).getValue();
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    // might need this
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    return new DBCPDataSource(dataSource);
}

From source file:org.geotoolkit.data.om.SOSDatabaseFeatureStoreFactory.java

@Override
public FeatureStore open(final ParameterValueGroup params) throws DataStoreException {
    checkCanProcessWithError(params);//from w ww  .ja  v  a2  s.c  o  m
    try {
        //create a datasource
        final BasicDataSource dataSource = new BasicDataSource();

        // some default data source behaviour
        dataSource.setPoolPreparedStatements(true);

        // driver
        final String driver = getDriverClassName(params);
        dataSource.setDriverClassName(driver);
        final boolean isPostgres = driver.startsWith("org.postgresql");

        // url
        dataSource.setUrl(getJDBCUrl(params));

        // username
        final String user = (String) params.parameter(USER.getName().toString()).getValue();
        dataSource.setUsername(user);

        // password
        final String passwd = (String) params.parameter(PASSWD.getName().toString()).getValue();
        if (passwd != null) {
            dataSource.setPassword(passwd);
        }

        // some datastores might need this
        dataSource.setAccessToUnderlyingConnectionAllowed(true);

        final ManageableDataSource source = new DBCPDataSource(dataSource);
        return new SOSDatabaseFeatureStore(params, source, isPostgres);
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
}

From source file:org.geotoolkit.data.sml.SMLFeatureStoreFactory.java

@Override
public FeatureStore open(final ParameterValueGroup params) throws DataStoreException {
    checkCanProcessWithError(params);/*from   w  w  w.j ava2s .  c om*/
    try {
        //create a datasource
        final BasicDataSource dataSource = new BasicDataSource();

        // some default data source behaviour
        dataSource.setPoolPreparedStatements(true);

        // driver
        dataSource.setDriverClassName(getDriverClassName(params));

        // url
        dataSource.setUrl(getJDBCUrl(params));

        // username
        final String user = (String) params.parameter(USER.getName().toString()).getValue();
        dataSource.setUsername(user);

        // password
        final String passwd = (String) params.parameter(PASSWD.getName().toString()).getValue();
        if (passwd != null) {
            dataSource.setPassword(passwd);
        }

        // some datastores might need this
        dataSource.setAccessToUnderlyingConnectionAllowed(true);

        final ManageableDataSource source = new DBCPDataSource(dataSource);
        return new SMLFeatureStore(params, source);
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
}

From source file:org.geotoolkit.db.AbstractJDBCFeatureStoreFactory.java

/**
 * Create a datasource using given parameters.
 *//*ww  w  .  j a v  a  2 s .  co  m*/
protected DataSource createDataSource(final ParameterValueGroup params) throws IOException {
    //create a datasource
    final BasicDataSource dataSource = new BasicDataSource();

    // some default data source behaviour
    dataSource.setPoolPreparedStatements(false);

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username
    final String user = (String) params.parameter(USER.getName().toString()).getValue();
    dataSource.setUsername(user);

    // password
    final String passwd = (String) params.parameter(PASSWORD.getName().toString()).getValue();
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    final Integer maxWait = (Integer) params.parameter(MAXWAIT.getName().toString()).getValue();
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    final Integer minConn = (Integer) params.parameter(MINCONN.getName().toString()).getValue();
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    final Integer maxConn = (Integer) params.parameter(MAXCONN.getName().toString()).getValue();
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    final Boolean validate = (Boolean) params.parameter(VALIDATECONN.getName().toString()).getValue();
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    // allow manipulating connections for possible tuning.
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    return new DBCPDataSource(dataSource);
}