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

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

Introduction

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

Prototype

public synchronized void setUrl(String url) 

Source Link

Document

Sets the #url .

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

Usage

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   w  w w. java2s .com
 * 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 ww .j av a 2  s  .c om
 * @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.geoserver.spatialite.SpatiaLiteDataStoreFactory.java

@Override
public BasicDataSource createDataSource(Map params) throws IOException {
    //create a datasource
    BasicDataSource dataSource = new BasicDataSource();

    // driver// ww  w  .j  av a2  s.c o  m
    dataSource.setDriverClassName(getDriverClassName());

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

    addConnectionProperties(dataSource);
    initializeDataSource(dataSource);

    return dataSource;
}

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

/**
 * Creates the datasource for the coverage store.
 *///from ww  w  .j  a  va2  s  . c o 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  .j ava  2  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 av a2 s .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.
 *//*from  w ww  . jav  a  2 s .  c  o 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);
}

From source file:org.geotoolkit.db.h2.H2StoreTest.java

/**
 * TODO reuse JDBC feature store test classes.
 * /*from  ww w.  j ava  2s . co m*/
 * @throws DataStoreException
 * @throws FactoryException 
 */
@Test
public void createAndReadTest() throws DataStoreException, FactoryException {

    final CoordinateReferenceSystem crs = CRS.decode("EPSG:4326", true);

    final BasicDataSource ds = new BasicDataSource();
    ds.setUrl("jdbc:h2:mem:test");
    ds.setUsername("user");
    ds.setPassword("pwd");

    final ParameterValueGroup params = H2FeatureStoreFactory.PARAMETERS_DESCRIPTOR.createValue();
    Parameters.getOrCreate(H2FeatureStoreFactory.USER, params).setValue("user");
    Parameters.getOrCreate(H2FeatureStoreFactory.PASSWORD, params).setValue("pwd");
    Parameters.getOrCreate(H2FeatureStoreFactory.PORT, params).setValue(5555);
    Parameters.getOrCreate(H2FeatureStoreFactory.DATABASE, params).setValue("sirs");
    Parameters.getOrCreate(H2FeatureStoreFactory.HOST, params).setValue("localhost");
    Parameters.getOrCreate(H2FeatureStoreFactory.SIMPLETYPE, params).setValue(Boolean.FALSE);
    Parameters.getOrCreate(H2FeatureStoreFactory.DATASOURCE, params).setValue(ds);

    final FeatureStore store = new H2FeatureStoreFactory().create(params);

    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("route");
    AttributeDescriptor add = ftb.add("id", String.class);
    add.getUserData().put(HintsPending.PROPERTY_IS_IDENTIFIER, Boolean.TRUE);
    ftb.add("geom", LineString.class, crs);
    final FeatureType featureType = ftb.buildFeatureType();

    store.createFeatureType(ftb.getName(), featureType);

    for (Name n : store.getNames()) {
        final FeatureType ft = store.getFeatureType(n);
        System.out.println(store.getFeatureType(n));

        final GeometryFactory GF = new GeometryFactory();
        final LineString geom = GF
                .createLineString(new Coordinate[] { new Coordinate(10, 20), new Coordinate(30, 40) });

        final Feature f = FeatureUtilities.defaultFeature(ft, "id-0");
        f.setPropertyValue("id", "test");
        f.setPropertyValue("geom", geom);

        store.addFeatures(n, Collections.singleton(f));

        final FeatureCollection res = store.createSession(true).getFeatureCollection(QueryBuilder.all(n));
        for (Feature rf : res) {
            System.out.println(rf);
        }

    }

}

From source file:org.geotools.data.h2.H2DataStoreFactory.java

protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException {
    String database = (String) DATABASE.lookUp(params);
    String host = (String) HOST.lookUp(params);
    BasicDataSource dataSource = new BasicDataSource();

    if (host != null && !host.equals("")) {
        Integer port = (Integer) PORT.lookUp(params);
        if (port != null && !port.equals("")) {
            dataSource.setUrl("jdbc:h2:tcp://" + host + ":" + port + "/" + database);
        } else {/*from  w w  w .j  av a  2 s. c  o m*/
            dataSource.setUrl("jdbc:h2:tcp://" + host + "/" + database);
        }
    } else if (baseDirectory == null) {
        //use current working directory
        dataSource.setUrl("jdbc:h2:" + database);
    } else {
        //use directory specified if the patch is relative
        String location;
        if (!new File(database).isAbsolute()) {
            location = new File(baseDirectory, database).getAbsolutePath();
        } else {
            location = database;
        }

        dataSource.setUrl("jdbc:h2:file:" + location);
    }

    String username = (String) USER.lookUp(params);
    if (username != null) {
        dataSource.setUsername(username);
    }
    String password = (String) PASSWD.lookUp(params);
    if (password != null) {
        dataSource.setPassword(password);
    }

    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setPoolPreparedStatements(false);

    return new DBCPDataSource(dataSource);
}

From source file:org.geotools.data.jdbc.ds.UnWrapperTest.java

public void testDBCPUnwrapper() throws SQLException, IOException {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:test_mem");
    ds.setAccessToUnderlyingConnectionAllowed(true);

    Connection conn = ds.getConnection();
    UnWrapper uw = DataSourceFinder.getUnWrapper(conn);
    assertNotNull(uw);// w  w w.  j a  v  a 2  s.  c o  m
    assertTrue(uw.canUnwrap(conn));
    Connection unwrapped = uw.unwrap(conn);
    assertNotNull(unwrapped);
    assertTrue(unwrapped instanceof org.h2.jdbc.JdbcConnection);

    Statement st = conn.createStatement();
    uw = DataSourceFinder.getUnWrapper(st);
    assertNotNull(uw);
    assertTrue(uw.canUnwrap(st));
    Statement uwst = uw.unwrap(st);
    assertNotNull(uwst);
    assertTrue(uwst instanceof org.h2.jdbc.JdbcStatement);
    st.close();

    PreparedStatement ps = conn.prepareStatement("select curtime()");
    uw = DataSourceFinder.getUnWrapper(ps);
    assertNotNull(uw);
    assertTrue(uw.canUnwrap(ps));
    PreparedStatement uwps = (PreparedStatement) uw.unwrap(ps);
    assertNotNull(uwps);
    assertTrue(uwps instanceof org.h2.jdbc.JdbcPreparedStatement);
    ps.close();

    conn.close();
    ds.close();
}