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.openrdf.sail.rdbms.postgresql.PgSqlStore.java

@Override
public void initialize() throws SailException {
    try {/*from   w  w  w . j a  va 2  s  . co m*/
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        throw new RdbmsException(e.toString(), e);
    }
    StringBuilder url = new StringBuilder();
    url.append("jdbc:postgresql:");
    if (serverName != null) {
        url.append("//").append(serverName);
        if (portNumber > 0) {
            url.append(":").append(portNumber);
        }
        url.append("/");
    }
    url.append(databaseName);
    Iterator<Entry<String, String>> iter;
    iter = getProperties().entrySet().iterator();
    if (iter.hasNext()) {
        url.append("?");
    }
    while (iter.hasNext()) {
        Entry<String, String> e = iter.next();
        url.append(enc(e.getKey()));
        url.append("=");
        url.append(enc(e.getValue()));
        if (iter.hasNext()) {
            url.append("&");
        }
    }
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(url.toString());
    if (user != null) {
        ds.setUsername(user);
    } else {
        ds.setUsername(System.getProperty("user.name"));
    }
    if (password != null) {
        ds.setPassword(password);
    }
    PgSqlConnectionFactory factory = new PgSqlConnectionFactory();
    factory.setSail(this);
    factory.setDataSource(ds);
    setBasicDataSource(ds);
    setConnectionFactory(factory);
    super.initialize();
}

From source file:org.openrdf.sail.rdbms.RdbmsStore.java

private DataSource lookupDataSource(String url, String user, String password) throws NamingException {
    if (url.startsWith("jdbc:")) {
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(url);
        ds.setUsername(user);/*  w w  w.j  av a 2  s.co  m*/
        ds.setPassword(password);
        setBasicDataSource(ds);
        return ds;
    }
    return (DataSource) new InitialContext().lookup(url);
}

From source file:org.orbisgis.geoserver.h2gis.datastore.H2GISDataStoreFactory.java

@Override
protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException {
    String database = (String) DATABASE.lookUp(params);
    String host = (String) HOST.lookUp(params);
    Boolean mvcc = (Boolean) MVCC.lookUp(params);
    Boolean mvstore = (Boolean) MVSTORE.lookUp(params);
    BasicDataSource dataSource = new BasicDataSource();

    if (host != null && !host.equals("")) {
        Integer port = (Integer) PORT.lookUp(params);
        if (port != null) {
            dataSource.setUrl("jdbc:h2:tcp://" + host + ":" + port + "/" + database);
        } else {/*from ww  w  . j av  a  2  s .  com*/
            dataSource.setUrl("jdbc:h2:tcp://" + host + "/" + database);
        }
    } else if (baseDirectory == null) {
        //use current working directory
        dataSource.setUrl("jdbc:h2:" + database + ";AUTO_SERVER=TRUE" + (mvcc != null ? (";MVCC=" + mvcc) : "")
                + (mvstore != null ? (";MVSTORE=" + mvstore) : ""));
    } 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 + ";AUTO_SERVER=TRUE"
                + (mvcc != null ? (";MVCC=" + mvcc) : "") + (mvstore != null ? (";MVSTORE=" + mvstore) : ""));
    }

    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);

    // if we got here the database has been created, now verify it has the H2GIS extension
    // and eventually try to create them
    JDBCDataStore closer = new JDBCDataStore();
    Connection cx = null;
    try {
        cx = dataSource.getConnection();
        //Add the spatial function
        if (!JDBCUtilities.tableExists(cx, "PUBLIC.GEOMETRY_COLUMNS")) {
            CreateSpatialExtension.initSpatialExtension(cx);
        }
    } catch (SQLException e) {
        throw new IOException("Failed to create the target database", e);
    } finally {
        closer.closeSafe(cx);
    }

    return new DBCPDataSource(dataSource);
}

From source file:org.overlord.dtgov.devsvr.bpm.DTGovBpmDevServer.java

/**
 * Creates an in-memory datasource./*from w  ww  .  j  a v a  2 s .  co  m*/
 * @throws SQLException
 */
private static DataSource createInMemoryDatasource() throws SQLException {
    System.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    Connection connection = ds.getConnection();
    connection.close();
    return ds;
}

From source file:org.owasp.dependencytrack.config.DatabaseConfiguration.java

@Bean
public DataSource dataSource() {
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(driverClassName);
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);
    basicDataSource.setUrl(url);
    basicDataSource.setMaxActive(maxActive);
    return basicDataSource;
}

From source file:org.owasp.dependencytrack.config.JunitDatabaseConfiguration.java

@Bean
public DataSource dataSource() {
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName("org.h2.Driver");
    basicDataSource.setUsername("sa");
    basicDataSource.setPassword("");
    basicDataSource.setUrl("jdbc:h2:mem:dtrack");
    basicDataSource.setMaxActive(100);// w  w  w  . j a  v a 2 s . c  om
    return basicDataSource;
}

From source file:org.paxml.tag.sql.SqlDataSourceTag.java

private DataSource getCacheOrCreate() {
    String key = getCacheKey();//  www  .  j a va  2 s .c  o m
    DataSource ds = CACHE.get(key);
    if (ds == null) {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(getDriver());
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDefaultAutoCommit(true);
        DataSource existing = CACHE.putIfAbsent(key, dataSource);
        if (existing != null) {
            ds = existing;
        } else {
            ds = dataSource;
        }
    }
    return ds;
}

From source file:org.paxml.util.DBUtils.java

public static DataSource getPooledDataSource(String driverClass, String username, String password, String url) {
    String key = driverClass + "::" + url;
    BasicDataSource ds = pooledDataSources.get(key);
    if (ds == null) {
        ds = new BasicDataSource();
        ds.setDriverClassName(driverClass);
        ds.setUsername(username);//ww  w. jav  a 2s  . c om
        ds.setPassword(password);
        ds.setUrl(url);
        BasicDataSource existingDs = pooledDataSources.putIfAbsent(key, ds);
        if (existingDs != null) {
            try {
                ds.close();
            } catch (SQLException e) {
                // do nothing
            }
            ds = existingDs;
        }

    }

    return ds;
}

From source file:org.pentaho.platform.engine.services.connection.datasource.dbcp.NonPooledDatasourceService.java

private DataSource convert(IDatasource datasource) {
    BasicDataSource basicDatasource = new BasicDataSource();
    basicDatasource.setDriverClassName(datasource.getDriverClass());
    basicDatasource.setMaxActive(datasource.getMaxActConn());
    basicDatasource.setMaxIdle(datasource.getIdleConn());
    basicDatasource.setMaxWait(datasource.getWait());
    basicDatasource.setUrl(datasource.getUrl());
    basicDatasource.setUsername(datasource.getUserName());
    basicDatasource.setPassword(datasource.getPassword());
    basicDatasource.setValidationQuery(datasource.getQuery());
    return basicDatasource;
}

From source file:org.pinus4j.cluster.impl.AppDBClusterImpl.java

@Override
public void buildDataSource(DBInfo dbConnInfo) throws LoadConfigException {
    AppDBInfo appDbConnInfo = (AppDBInfo) dbConnInfo;

    LOG.info(dbConnInfo.toString());/*from w w  w .  jav a 2s  . c o m*/

    try {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(enumDb.getDriverClass());
        ds.setUsername(appDbConnInfo.getUsername());
        ds.setPassword(appDbConnInfo.getPassword());
        ds.setUrl(appDbConnInfo.getUrl());

        // ?
        Map<String, Object> dbConnPoolInfo = appDbConnInfo.getConnPoolInfo();
        ds.setValidationQuery("SELECT 1");
        ds.setMaxActive((Integer) dbConnPoolInfo.get(Const.PROP_MAXACTIVE));
        ds.setMinIdle((Integer) dbConnPoolInfo.get(Const.PROP_MINIDLE));
        ds.setMaxIdle((Integer) dbConnPoolInfo.get(Const.PROP_MAXIDLE));
        ds.setInitialSize((Integer) dbConnPoolInfo.get(Const.PROP_INITIALSIZE));
        ds.setRemoveAbandoned((Boolean) dbConnPoolInfo.get(Const.PROP_REMOVEABANDONED));
        ds.setRemoveAbandonedTimeout((Integer) dbConnPoolInfo.get(Const.PROP_REMOVEABANDONEDTIMEOUT));
        ds.setMaxWait((Integer) dbConnPoolInfo.get(Const.PROP_MAXWAIT));
        ds.setTimeBetweenEvictionRunsMillis(
                (Integer) dbConnPoolInfo.get(Const.PROP_TIMEBETWEENEVICTIONRUNSMILLIS));
        ds.setNumTestsPerEvictionRun((Integer) dbConnPoolInfo.get(Const.PROP_NUMTESTSPEREVICTIONRUN));
        ds.setMinEvictableIdleTimeMillis((Integer) dbConnPoolInfo.get(Const.PROP_MINEVICTABLEIDLETIMEMILLIS));

        dbConnInfo.setDatasource(ds);
    } catch (Exception e) {
        throw new LoadConfigException(e);
    }
}