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:com.qcadoo.model.Utils.java

public static DataSource createDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUrl("jdbc:hsqldb:mem:mes");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    return dataSource;
}

From source file:com.gs.obevo.db.impl.core.jdbc.JdbcDataSourceFactory.java

private static DataSource createFromJdbcUrl(Class<? extends Driver> driverClass, String url,
        Credential credential, int numThreads, ImmutableList<String> initSqls,
        Properties extraConnectionProperties) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClass.getName());
    dataSource.setUrl(url);
    dataSource.setUsername(credential.getUsername());
    dataSource.setPassword(credential.getPassword());

    // connection pool settings
    dataSource.setInitialSize(numThreads);
    dataSource.setMaxActive(numThreads);
    // keep the connections open if possible; only close them via the removeAbandonedTimeout feature
    dataSource.setMaxIdle(numThreads);//from w ww.  j  av  a 2s  .  co  m
    dataSource.setMinIdle(0);
    dataSource.setRemoveAbandonedTimeout(300);

    dataSource.setConnectionInitSqls(initSqls.castToList());
    if (extraConnectionProperties != null) {
        for (String key : extraConnectionProperties.stringPropertyNames()) {
            dataSource.addConnectionProperty(key, extraConnectionProperties.getProperty(key));
        }
    }

    return dataSource;
}

From source file:com.taobao.tddl.jdbc.group.testutil.DataSourceFactory.java

public static DataSource getLocalMySQLDataSource(int num) {
    if (num > 3)
        num = 1;/* www  .j  av  a 2s  .  c  o m*/
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("root");
    ds.setPassword("zhh");
    ds.setUrl("jdbc:mysql://localhost/group_test_" + num);
    return ds;

}

From source file:com.taobao.tddl.jdbc.group.testutil.DataSourceFactory.java

public static DataSource getMySQLDataSource(int num) {
    if (num > 3)
        num = 1;/*from   w  w w.  j a v a  2s  . co m*/
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("tddl");
    ds.setPassword("tddl");
    ds.setUrl("jdbc:mysql://127.0.0.1:3306/group_test_" + num);
    return ds;

}

From source file:com.surveypanel.form.TestHelper.java

public static DataSource getDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("root");
    ds.setPassword("halflife");
    ds.setUrl(
            "jdbc:mysql://localhost:3306/surveypanel?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8");
    return ds;/*from  w  ww.j  av a 2  s.  c  o m*/
}

From source file:dbcp.BasicDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("test");
    ds.setPassword("test");
    ds.setUrl(connectURI);
    return ds;//from  w  w w  .j a  v a 2  s.  c  om
}

From source file:br.com.linuxgames.model.dao.core.LinuxGamesDataSource.java

private static DataSource setupDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("boaglio_linuxgam");
    ds.setPassword("boaglio_linuxgam");
    ds.setUrl("jdbc:mysql://localhost/linuxgames");
    return ds;/*from   w  w w. j  av  a2  s.co m*/
}

From source file:com.stratelia.silverpeas.silverStatisticsPeas.control.PerfVolumeTest.java

@BeforeClass
public static void setupDataSource() throws NamingException {
    SimpleMemoryContextFactory.setUpAsInitialContext();
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setUsername("postgres");
    ds.setPassword("postgres");
    ds.setUrl("jdbc:postgresql://localhost:5432/extranet");
    InitialContext ic = new InitialContext();
    ic.bind("java:/datasources/silverpeas-jdbc", ds);
    ic.bind("java:/datasources/DocumentStoreDS", ds);
}

From source file:javax.arang.DB.dbcp.BasicDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("root");
    ds.setPassword("dkfkdsid");
    ds.setUrl(connectURI);
    return ds;//  w w w  . j a va  2s . c o  m
}

From source file:com.pinterest.deployservice.db.DatabaseUtil.java

/**
 * Create a MySQL datasource./*from   w  ww .  java 2 s .co  m*/
 *
 * @param url             the url of the DB.
 * @param user            the user name to connect to MySQL as.
 * @param passwd          the password for the corresponding MySQL user.
 * @param poolSize        the connection pool size string, in the format of
 *                        initialSize:maxActive:maxIdle:minIdle.
 * @param maxWaitInMillis the max wait time in milliseconds to get a connection from the pool.
 * @return a BasicDataSource for the target MySQL instance.
 */
public static BasicDataSource createDataSource(String driverClassName, String url, String user, String passwd,
        String poolSize, int maxWaitInMillis) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);
    dataSource.setUsername(user);
    dataSource.setPassword(passwd);
    dataSource.setDefaultAutoCommit(true);
    dataSource.setDefaultReadOnly(false);

    // poolSize parsing, the poolsize string passed in the following format
    // initialSize:maxActive:maxIdle:minIdle
    String[] sizeStrs = poolSize.split(":");
    dataSource.setInitialSize(Integer.parseInt(sizeStrs[0]));
    dataSource.setMaxActive(Integer.parseInt(sizeStrs[1]));
    dataSource.setMaxIdle(Integer.parseInt(sizeStrs[2]));
    dataSource.setMinIdle(Integer.parseInt(sizeStrs[3]));

    dataSource.setValidationQuery("SELECT 1");
    dataSource.setTestOnBorrow(true);
    dataSource.setTestOnReturn(false);
    dataSource.setTestWhileIdle(true);
    dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
    dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
    // dataSource.setNumTestsPerEvictionRun(3);
    // max wait in milliseconds for a connection.
    dataSource.setMaxWait(maxWaitInMillis);

    // force connection pool initialization.
    Connection conn = null;
    try {
        // Here not getting the connection from ThreadLocal no need to worry about that.
        conn = dataSource.getConnection();
    } catch (SQLException e) {
        LOG.error(String.format("Failed to get a db connection when creating DataSource, url = %s", url), e);
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return dataSource;
}