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

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

Introduction

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

Prototype

BasicDataSource

Source Link

Usage

From source file:com.miserablemind.butter.bootstrap.RootContext.java

/**
 * Data Source Bean that is configured for JDBC connection using {@link ConfigJDBC}.
 *
 * @return {@link BasicDataSource} bean with a name {@code dataSource}.
 *///from   ww  w . ja va2s .co  m
@Bean(name = "dataSource")
public BasicDataSource dataSource() {

    ConfigJDBC configJDBC = this.configSystem.getConfigJDBC();

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUsername(configJDBC.getJdbcUsername());
    dataSource.setPassword(configJDBC.getJdbcPassword());
    dataSource.setUrl(configJDBC.getJdbcUrl());
    dataSource.setDriverClassName(configJDBC.getJdbcDriverClassName());

    return dataSource;
}

From source file:com.alibaba.druid.benckmark.pool.Case2.java

public void test_1() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);/*from   www.j a  va  2  s  .  co  m*/
    dataSource.setMinIdle(minPoolSize);
    dataSource.setMaxIdle(maxPoolSize);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery("SELECT 1");
    dataSource.setTestOnBorrow(testOnBorrow);

    for (int i = 0; i < executeCount; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}

From source file:gobblin.util.jdbc.DataSourceProvider.java

public DataSourceProvider() {
    this.basicDataSource = new BasicDataSource();
}

From source file:com.dangdang.ddframe.rdb.transaction.soft.AsyncJobMain.java

private static DataSource createTransactionLogDataSource() {
    BasicDataSource result = new BasicDataSource();
    result.setDriverClassName("com.mysql.jdbc.Driver");
    result.setUrl("jdbc:mysql://localhost:3306/trans_log");
    result.setUsername("root");
    result.setPassword("");
    return result;
}

From source file:com.pinterest.pinlater.backends.mysql.MySQLDataSources.java

private static DataSource createDataSource(String host, int port, String user, String passwd, int poolSize,
        int maxWaitMillis, int socketTimeoutMillis) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource/* w ww . j  a  v a2s .com*/
            .setUrl(String.format(
                    "jdbc:mysql://%s:%d?" + "connectTimeout=5000&" + "socketTimeout=%d&"
                            + "enableQueryTimeouts=false&" + "cachePrepStmts=true&" + "characterEncoding=UTF-8",
                    host, port, socketTimeoutMillis));
    dataSource.setUsername(user);
    dataSource.setPassword(passwd);
    dataSource.setDefaultAutoCommit(true);
    dataSource.setInitialSize(poolSize);
    dataSource.setMaxActive(poolSize);
    dataSource.setMaxIdle(poolSize);
    // deal with idle connection eviction
    dataSource.setValidationQuery("SELECT 1 FROM DUAL");
    dataSource.setTestOnBorrow(false);
    dataSource.setTestOnReturn(false);
    dataSource.setTestWhileIdle(true);
    dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
    dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
    dataSource.setNumTestsPerEvictionRun(poolSize);
    // max wait in milliseconds for a connection.
    dataSource.setMaxWait(maxWaitMillis);
    // 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 mysql connection when creating DataSource, " + "host: %s, port: %d", host,
                port), e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }
    return dataSource;
}

From source file:com.alibaba.druid.benckmark.pool.Case0.java

public void f_test_1() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);/*from  w w w. j a  v a2  s.  c  o m*/
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    for (int i = 0; i < LOOP_COUNT; ++i) {
        p0(dataSource, "dbcp");
    }
    System.out.println();
}

From source file:me.emmy.db.MySQLLayer.java

public MySQLLayer(BasePlugin plugin, FileConfiguration config) {
    super();/*from w w  w. j  a va2s. com*/
    this.plugin = plugin;
    source = new BasicDataSource();
    source.setDriverClassName("com.mysql.jdbc.Driver");
    schema = config.getString("database");
    host = config.getString("host");
    port = config.getString("port");
    String url = String.format("jdbc:mysql://%1$s:%2$s/%3$s", host, port, schema);
    source.setUrl(url);

    String username = config.getString("username");
    source.setUsername(username);
    source.setPassword(config.getString("password"));
    plugin.log("Connecting to %s with user %s", url, username);
    jdbc = new JdbcTemplate(source);

}

From source file:com.plexobject.testplayer.dao.hibernate.GenericDaoHibernate.java

protected static DataSource _getDataSource() {
    BasicDataSource source = new BasicDataSource();
    source.setDriverClassName("org.hsqldb.jdbcDriver");
    source.setUrl("jdbc:hsqldb:file:testplayer.db");
    //source.setUrl("jdbc:hsqldb:hsql://localhost/testplayer");
    source.setUsername("sa");
    source.setPassword("");
    source.setMaxActive(15);//from w w w.j  av  a2  s .  co m
    source.setMaxIdle(4);
    return source;
}

From source file:com.dangdang.ddframe.rdb.sharding.config.yaml.YamlintegratedTest.java

@Test
public void testDynamic() {
    Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
    Map<String, DataSource> dsMap = new HashMap<>();
    dsMap.put("ds", new BasicDataSource());
    ShardingRuleConfig config = (ShardingRuleConfig) yaml
            .load(YamlintegratedTest.class.getResourceAsStream("/config/config-dynamic.yaml"));
    ShardingRule shardingRule = new ShardingRuleBuilder("config-dynamic.yaml", dsMap, config).build();
    int i = 0;/*from w  w w.  j  a v a2  s.c  o  m*/
    for (TableRule each : shardingRule.getTableRules()) {
        i++;
        assertThat(each.getActualTables().size(), is(2));
        assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db0")));
        assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db1")));
        switch (i) {
        case 1:
            assertThat(each.getLogicTable(), is("config"));
            break;
        case 2:
            assertThat(each.getLogicTable(), is("t_order"));
            break;
        case 3:
            assertThat(each.getLogicTable(), is("t_order_item"));
            break;
        default:
            fail();
        }
    }
}

From source file:br.gov.frameworkdemoiselle.internal.proxy.BasicDataSourceProxy.java

private BasicDataSource getDelegate() {
    if (this.delegate == null) {
        BasicDataSource dataSource = new BasicDataSource();

        try {/*from w w  w  . jav  a2  s .c  om*/
            String driver = config.getDriverClass().get(dataSourceName);
            String url = config.getUrl().get(dataSourceName);
            String username = config.getUsername().get(dataSourceName);
            String password = config.getPassword().get(dataSourceName);

            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);

        } catch (ClassCastException cause) {
            throw new DemoiselleException(bundle.getString("load-duplicated-configuration-failed"), cause);
        }

        delegate = dataSource;
    }

    return this.delegate;
}