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.google.gerrit.server.schema.DataSourceProvider.java

private DataSource open(final SitePaths site, final Config cfg, final Context context,
        final DataSourceType dst) {
    ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = dbs.optional("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();//from w  w  w.j  av  a 2s. c  o m
    }

    String url = dbs.optional("url");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = dbs.optional("username");
    String password = dbs.optional("password");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = cfg.getBoolean("database", "connectionpool", dst.usePool());
    }

    if (usePool) {
        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        if (username != null && !username.isEmpty()) {
            ds.setUsername(username);
        }
        if (password != null && !password.isEmpty()) {
            ds.setPassword(password);
        }
        ds.setMaxActive(cfg.getInt("database", "poollimit", 8));
        ds.setMinIdle(cfg.getInt("database", "poolminidle", 4));
        ds.setMaxIdle(cfg.getInt("database", "poolmaxidle", 4));
        ds.setMaxWait(ConfigUtil.getTimeUnit(cfg, "database", null, "poolmaxwait",
                MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        ds.setInitialSize(ds.getMinIdle());
        return ds;

    } else {
        // Don't use the connection pool.
        //
        try {
            final Properties p = new Properties();
            p.setProperty("driver", driver);
            p.setProperty("url", url);
            if (username != null) {
                p.setProperty("user", username);
            }
            if (password != null) {
                p.setProperty("password", password);
            }
            return new SimpleDataSource(p);
        } catch (SQLException se) {
            throw new ProvisionException("Database unavailable", se);
        }
    }
}

From source file:net.certifi.audittablegen.GenericDMR.java

/**
 * Generate a DataSource from Properties 
 * @param props//from  w  ww .j av  a  2 s .  c o m
 * @return BasicDataSource as DataSource
 */
static DataSource getRunTimeDataSource(Properties props) {

    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(props.getProperty("driver", ""));
    dataSource.setUsername(props.getProperty("username"));
    dataSource.setPassword(props.getProperty("password"));
    dataSource.setUrl(props.getProperty("url"));
    dataSource.setMaxActive(10);
    dataSource.setMaxIdle(5);
    dataSource.setInitialSize(5);

    //dataSource.setValidationQuery("SELECT 1");

    return dataSource;
}

From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java

@Bean
public DataSource slaveDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getRequiredProperty("gotour.slave.jdbc.driverClassName"));
    dataSource.setUrl(env.getRequiredProperty("gotour.slave.jdbc.url"));
    dataSource.setUsername(env.getRequiredProperty("gotour.slave.jdbc.username"));
    dataSource.setPassword(env.getRequiredProperty("gotour.slave.jdbc.password"));
    dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class));
    dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class));
    dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class));
    dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class));
    dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class));
    dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class));
    dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery"));
    dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class));
    dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class));
    dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class));
    dataSource.setTimeBetweenEvictionRunsMillis(
            env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class));
    dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class));
    dataSource.setMinEvictableIdleTimeMillis(
            env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class));
    return dataSource;
}

From source file:com.otterca.persistence.dao.H2Configuration.java

/**
 * Get dataSource for H2 database./*from  w ww . j  av  a  2  s .c  o  m*/
 * 
 * @return
 */
@Bean
public DataSource getDataSource() {
    if (dataSource == null) {
        synchronized (this) {
            if (dataSource == null) {
                BasicDataSource ds = new BasicDataSource();
                ds.setDriverClassName(bundle.getString("driverClassName"));
                ds.setUrl(bundle.getString("url"));
                ds.setUsername(bundle.getString("username"));
                BasicTextEncryptor encryptor = new BasicTextEncryptor();
                encryptor.setPassword(masterkey.getString("master.password"));
                ds.setPassword(encryptor.decrypt(bundle.getString("password")));
                ds.setValidationQuery(bundle.getString("validationQuery"));
                dataSource = ds;
            }
        }
    }
    return dataSource;
}

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

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

    dataSource.setMaxActive(maxActive);// ww  w. j a  va2 s . c  om
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setPoolPreparedStatements(preparedStatementCache);
    dataSource.setMaxOpenPreparedStatements(preparedStatementCacheSize);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setConnectionProperties(properties);

    //        printAV_INFO(dataSource);

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

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

protected static DataSource getDataSource() {
    BasicDataSource source = new BasicDataSource();
    source.setDriverClassName("com.mysql.jdbc.Driver");
    source.setUrl("jdbc:mysql://localhost/testplayer");
    source.setUsername("root");
    source.setPassword("root");
    source.setMaxActive(15);/*w  w w. ja  v a  2  s  . c o m*/
    source.setMaxIdle(4);
    return source;
}

From source file:com.jk.framework.dao.connection.JKPoolingDataSource.java

private void init() {

    this.datasource = new BasicDataSource();
    this.datasource.setDriverClassName(super.getDriverName());
    this.datasource.setUrl(getDatabaseUrl());
    this.datasource.setUsername(getUsername());
    this.datasource.setPassword(getPassword());
    this.datasource.setInitialSize(getInitialPoolSize());
    // datasource.setMaxIdle(Integer.parseInt(getProperty("db-max-idle",
    // "5")));/*from   w  ww. jav  a  2 s  .c o  m*/
    // datasource.setPoolPreparedStatements(Boolean.parseBoolean(getProperty("db-cache-ps","true")));
    this.datasource.setMaxActive(getMaxPoolSize());
    logger.info("Init database connection-pool(", getInitialPoolSize(), "-", getMaxPoolSize(), ")...");

    logger.debug(this.datasource.toString());
    // datasource.setMaxWait(1000);
}

From source file:net.hydromatic.optiq.impl.splunk.SplunkDriver.java

@Override
public Connection connect(String url, Properties info) throws SQLException {
    Connection connection = super.connect(url, info);
    OptiqConnection optiqConnection = (OptiqConnection) connection;
    SplunkConnection splunkConnection;/* w  w w  .j  a  v a 2 s  .  c om*/
    try {
        String url1 = info.getProperty("url");
        if (url1 == null) {
            throw new IllegalArgumentException("Must specify 'url' property");
        }
        URL url2 = new URL(url1);
        String user = info.getProperty("user");
        if (user == null) {
            throw new IllegalArgumentException("Must specify 'user' property");
        }
        String password = info.getProperty("password");
        if (password == null) {
            throw new IllegalArgumentException("Must specify 'password' property");
        }
        splunkConnection = new SplunkConnection(url2, user, password);
    } catch (Exception e) {
        throw new SQLException("Cannot connect", e);
    }
    final MutableSchema rootSchema = optiqConnection.getRootSchema();
    final String schemaName = "splunk";
    final SplunkSchema schema = new SplunkSchema(optiqConnection, rootSchema, schemaName, splunkConnection,
            optiqConnection.getTypeFactory(), rootSchema.getSubSchemaExpression(schemaName, Schema.class));
    rootSchema.addSchema(schemaName, schema);

    // Include a schema called "mysql" in every splunk connection.
    // This is a hack for demo purposes. TODO: Add a config file mechanism.
    if (true) {
        final String mysqlSchemaName = "mysql";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new SQLException(e);
        }
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:mysql://localhost");
        dataSource.setUsername("foodmart");
        dataSource.setPassword("foodmart");

        JdbcSchema.create(optiqConnection.getRootSchema(), dataSource, "foodmart", "", mysqlSchemaName);
    }

    return connection;
}

From source file:com.dangdang.ddframe.rdb.integrate.AbstractDBUnitTest.java

private DataSource createDataSource(final String dataSetFile) {
    if (DATA_SOURCES.containsKey(dataSetFile)) {
        return DATA_SOURCES.get(dataSetFile);
    }//from   w  ww  .  j a va2  s  .  com
    BasicDataSource result = new BasicDataSource();
    result.setDriverClassName(dbEnv.getDriverClassName());
    result.setUrl(dbEnv.getURL(getFileName(dataSetFile)));
    result.setUsername(dbEnv.getUsername());
    result.setPassword(dbEnv.getPassword());
    result.setMaxActive(1000);
    DATA_SOURCES.put(dataSetFile, result);
    return result;
}

From source file:blueprint.sdk.experimental.florist.db.ConnectionListener.java

public void contextInitialized(final ServletContextEvent arg0) {
    Properties poolProp = new Properties();
    try {/*from   ww  w . java  2s.c  o m*/
        Context initContext = new InitialContext();

        // load pool properties file (from class path)
        poolProp.load(ConnectionListener.class.getResourceAsStream("/jdbc_pools.properties"));
        StringTokenizer stk = new StringTokenizer(poolProp.getProperty("PROP_LIST"));

        // process all properties files list in pool properties (from class
        // path)
        while (stk.hasMoreTokens()) {
            try {
                String propName = stk.nextToken();
                LOGGER.info(this, "loading jdbc properties - " + propName);

                Properties prop = new Properties();
                prop.load(ConnectionListener.class.getResourceAsStream(propName));

                DataSource dsr;
                if (prop.containsKey("JNDI_NAME")) {
                    // lookup DataSource from JNDI
                    // FIXME JNDI support is not tested yet. SPI or Factory
                    // is needed here.
                    LOGGER.warn(this, "JNDI DataSource support needs more hands on!");
                    dsr = (DataSource) initContext.lookup(prop.getProperty("JNDI_NAME"));
                } else {
                    // create new BasicDataSource
                    BasicDataSource bds = new BasicDataSource();
                    bds.setMaxActive(Integer.parseInt(prop.getProperty("MAX_ACTIVE")));
                    bds.setMaxIdle(Integer.parseInt(prop.getProperty("MAX_IDLE")));
                    bds.setMaxWait(Integer.parseInt(prop.getProperty("MAX_WAIT")));
                    bds.setInitialSize(Integer.parseInt(prop.getProperty("INITIAL")));
                    bds.setDriverClassName(prop.getProperty("CLASS_NAME"));
                    bds.setUrl(prop.getProperty("URL"));
                    bds.setUsername(prop.getProperty("USER"));
                    bds.setPassword(prop.getProperty("PASSWORD"));
                    bds.setValidationQuery(prop.getProperty("VALIDATION"));
                    dsr = bds;
                }
                boundDsrs.put(prop.getProperty("POOL_NAME"), dsr);
                initContext.bind(prop.getProperty("POOL_NAME"), dsr);
            } catch (RuntimeException e) {
                LOGGER.trace(e);
            }
        }
    } catch (IOException | NamingException e) {
        LOGGER.trace(e);
    }
}