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.openddal.test.BaseTestCase.java

private BasicDataSource newDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driverClassName);
    ds.setUrl(url);/*from  ww  w  .j  ava2  s . c  o m*/
    ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    return ds;
}

From source file:com.redrisegames.reigninwildWeb.ui.SampleWebUiApplication.java

@Bean
public SessionFactory sessionFactory() {
    BasicDataSource dataSource = new BasicDataSource();

    return new LocalSessionFactoryBuilder(getDataSource()).addAnnotatedClasses(SampleWebUiApplication.class)
            .buildSessionFactory();/*from w ww  .java  2  s  . com*/
}

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

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

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);/*  w ww.j a v  a2 s  .co  m*/
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(oracleDriverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setTestOnBorrow(testWhileIdle);
    dataSource.setTestOnBorrow(testOnReturn);
    dataSource.setRemoveAbandoned(removeAbandoned);
    dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    dataSource.setValidationQuery(validateQuery);

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

From source file:com.btmatthews.maven.plugins.inmemdb.db.derby.DerbyDatabase.java

/**
 * Get the data source that describes the connection to the in-memory Apache
 * Derby database./* w  w w  . jav a 2s.  c om*/
 *
 * @param attributes Additional attributes for the data source connection string.
 * @return Returns {@code dataSource} which was initialised by the
 *         constructor.
 */
@Override
public DataSource getDataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl(getUrl(getAttributes()));
    dataSource.setUsername(getUsername());
    if (StringUtils.isNotEmpty(getPassword())) {
        dataSource.setPassword(getPassword());
    }
    dataSource.setDriverClassName(DRIVER_CLASS);
    return dataSource;
}

From source file:com.googlesource.gerrit.plugins.ci.server.schema.CiDataSourceProvider.java

private DataSource open(Context context, CiDataSourceType dst) {
    //ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = config.getString("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();/*  w  w w  . ja  va  2  s  . com*/
    }

    String url = config.getString("dbUrl");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = config.getString("username");
    String password = config.getString("password");
    String interceptor = config.getString("dataSourceInterceptorClass");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = config.getBoolean("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(config.getInt("poollimit", DEFAULT_POOL_LIMIT));
        ds.setMinIdle(config.getInt("poolminidle", 4));
        ds.setMaxIdle(config.getInt("poolmaxidle", 4));
        String valueString = config.getString("poolmaxwait");
        if (Strings.isNullOrEmpty(valueString)) {
            ds.setMaxWait(MILLISECONDS.convert(30, SECONDS));
        } else {
            ds.setMaxWait(ConfigUtil.getTimeUnit(valueString, MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        }
        ds.setInitialSize(ds.getMinIdle());
        exportPoolMetrics(ds);
        return intercept(interceptor, ds);
    } else {
        // Don't use the connection pool.
        //
        try {
            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 intercept(interceptor, new SimpleDataSource(p));
        } catch (SQLException se) {
            throw new ProvisionException("Database unavailable", se);
        }
    }
}

From source file:com.googlesource.gerrit.plugins.verifystatus.server.schema.CiDataSourceProvider.java

private DataSource open(Context context, CiDataSourceType dst) {
    // ConfigSection dbs = new ConfigSection(cfg, "database");
    String driver = config.getString("driver");
    if (Strings.isNullOrEmpty(driver)) {
        driver = dst.getDriver();/*from   w  ww . ja va 2 s  .c o  m*/
    }

    String url = config.getString("dbUrl");
    if (Strings.isNullOrEmpty(url)) {
        url = dst.getUrl();
    }

    String username = config.getString("username");
    String password = config.getString("password");
    String interceptor = config.getString("dataSourceInterceptorClass");

    boolean usePool;
    if (context == Context.SINGLE_USER) {
        usePool = false;
    } else {
        usePool = config.getBoolean("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(config.getInt("poollimit", DEFAULT_POOL_LIMIT));
        ds.setMinIdle(config.getInt("poolminidle", 4));
        ds.setMaxIdle(config.getInt("poolmaxidle", 4));
        String valueString = config.getString("poolmaxwait");
        if (Strings.isNullOrEmpty(valueString)) {
            ds.setMaxWait(MILLISECONDS.convert(30, SECONDS));
        } else {
            ds.setMaxWait(ConfigUtil.getTimeUnit(valueString, MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
        }
        ds.setInitialSize(ds.getMinIdle());
        exportPoolMetrics(ds);
        return intercept(interceptor, ds);
    }
    // Don't use the connection pool.
    try {
        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 intercept(interceptor, new SimpleDataSource(p));
    } catch (SQLException se) {
        throw new ProvisionException("Database unavailable", se);
    }
}

From source file:fr.cnes.sitools.datasource.jdbc.business.SitoolsDataSourceFactory.java

/**
 * Setup a dataSource for "users". Usage is for all users for consultation functions.
 * // w  w w .j  a  va2 s  .co  m
 * @param dataSource
 *          the DataSource to update
 * @return SitoolsDataSource the new DataSource
 */
public SitoolsDataSource setupDataSourceForUsers(JDBCDataSource dataSource) {
    String key = dataSource.getId();
    SitoolsDataSource foundDatasource = dataSources.get(key);
    if (foundDatasource == null) {

        BasicDataSource ds = new BasicDataSource();
        // OSGi
        ds.setDriverClassLoader(getClass().getClassLoader());
        ds.setDriverClassName(dataSource.getDriverClass());
        ds.setUsername(dataSource.getUserLogin());
        ds.setPassword(dataSource.getUserPassword());
        ds.setUrl(dataSource.getUrl());
        ds.setMaxActive(dataSource.getMaxActive());
        ds.setInitialSize(dataSource.getInitialSize());
        ds.setDefaultReadOnly(true);
        if ((dataSource.getSchemaOnConnection() != null) && !dataSource.getSchemaOnConnection().equals("")) {
            ds.setDefaultCatalog(dataSource.getSchemaOnConnection());
        }
        foundDatasource = new SitoolsDataSource(dataSource, ds, dataSource.getSchemaOnConnection());
        dataSources.put(key, foundDatasource);
    }
    return foundDatasource;
}

From source file:net.hydromatic.optiq.model.ModelHandler.java

private DataSource dataSource(JsonJdbcSchema jsonJdbcSchema) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl(jsonJdbcSchema.jdbcUrl);
    dataSource.setUsername(jsonJdbcSchema.jdbcUser);
    dataSource.setPassword(jsonJdbcSchema.jdbcPassword);
    return dataSource;
}

From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

@Test
public void testBasicDataSource() throws Exception {

    String jdbcDriverClassPath = globalConfiguration.getJdbcDriverClassPath();
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { new URL(jdbcDriverClassPath) },
            Thread.currentThread().getContextClassLoader());

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(globalConfiguration.getJdbcDriverClassName());
    basicDataSource.setUrl(globalConfiguration.getJdbcConnectionURI());
    basicDataSource.setUsername(globalConfiguration.getJdbcUsername());
    basicDataSource.setPassword(globalConfiguration.getJdbcPassword());

    // Does not work in 1.4, fixed in 1.4.1
    basicDataSource.setDriverClassLoader(urlClassLoader);

    try {/*from ww  w  . ja  v a2 s. c  om*/
        Connection connection = basicDataSource.getConnection();
        assertNotNull(connection);
        fail("Wuhu! They have finally fixed the bug in DBCP; ignoring the classloader. Consider changing the code!");
    } catch (SQLException e) {
        // As expected when using DBCP 1.4
    }
}

From source file:eu.celarcloud.jcatascopia.web.queryMaster.database.MySQL.DBInterfaceWithConnPool.java

public void dbConnect() {
    try {//  ww w .j av a2s.  co  m
        dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://" + host + "/" + database);
        dataSource.setUsername(user);
        dataSource.setPassword(pass);
        dataSource.setMaxActive(CONN_NUM);
        //dataSource.setMinIdle(CONN_NUM/2);
        dataSource.setMinIdle(CONN_NUM);
        dataSource.setInitialSize(CONN_NUM);

        System.out.println("MySQL DBInterface>> creating a connection...");

        //connected, but does database exist?
        conn = dataSource.getConnection();
        resultSet = conn.getMetaData().getCatalogs();
        boolean found = false;
        while (resultSet.next()) {
            String databaseName = resultSet.getString(1);
            if (databaseName.equals(database)) {
                System.out.println("MySQL DBInterface>> Connected to database...");
                found = true;
            }
        }
        if (!found)
            System.out.println("MySQL DBInterface>> database doesn't exist...");
        this.resultSet.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}