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:io.apiman.gateway.engine.jdbc.JdbcMetricsTest.java

/**
 * Creates an in-memory datasource.//from ww  w. j  ava  2 s.co  m
 * @throws SQLException
 */
private static BasicDataSource createInMemoryDatasource() throws Exception {
    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.setAutoCommit(true);
    initDB(connection);
    connection.close();
    return ds;
}

From source file:io.apiman.gateway.engine.policies.BasicAuthJDBCTest.java

/**
 * Creates an in-memory datasource.//ww w .  ja  v  a 2  s  .  c om
 * @throws SQLException
 */
private static BasicDataSource createInMemoryDatasource() throws SQLException {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa"); //$NON-NLS-1$
    ds.setPassword(""); //$NON-NLS-1$
    ds.setUrl("jdbc:h2:mem:BasicAuthJDBCTest;DB_CLOSE_DELAY=-1"); //$NON-NLS-1$
    Connection connection = ds.getConnection();
    connection.prepareStatement(
            "CREATE TABLE users ( username varchar(255) NOT NULL, password varchar(255) NOT NULL, PRIMARY KEY (username))")
            .executeUpdate();
    connection.prepareStatement(
            "INSERT INTO users (username, password) VALUES ('bwayne', 'ae2efd698aefdf366736a4eda1bc5241f9fbfec7')")
            .executeUpdate();
    connection.prepareStatement(
            "INSERT INTO users (username, password) VALUES ('ckent', 'ea59f7ca52a2087c99374caba0ff29be1b2dcdbf')")
            .executeUpdate();
    connection.prepareStatement(
            "INSERT INTO users (username, password) VALUES ('ballen', 'ea59f7ca52a2087c99374caba0ff29be1b2dcdbf')")
            .executeUpdate();
    connection
            .prepareStatement(
                    "CREATE TABLE roles (rolename varchar(255) NOT NULL, username varchar(255) NOT NULL)")
            .executeUpdate();
    connection.prepareStatement("INSERT INTO roles (rolename, username) VALUES ('user', 'bwayne')")
            .executeUpdate();
    connection.prepareStatement("INSERT INTO roles (rolename, username) VALUES ('admin', 'bwayne')")
            .executeUpdate();
    connection.prepareStatement("INSERT INTO roles (rolename, username) VALUES ('ckent', 'user')")
            .executeUpdate();
    connection.prepareStatement("INSERT INTO roles (rolename, username) VALUES ('ballen', 'user')")
            .executeUpdate();
    connection.close();
    return ds;
}

From source file:com.funambol.json.coredb.dao.DBManager.java

public void configureDatasource(String user, String password, String url, String driver) {
    ds = new BasicDataSource();
    ds.setDriverClassName(driver == null ? "" : driver);
    ds.setUsername(user == null ? "" : user);
    ds.setPassword(password == null ? "" : password);
    ds.setUrl(url == null ? "" : url);
    ds.setDefaultAutoCommit(false);/* w  ww.  j  a  v  a2 s  . co  m*/

}

From source file:cz.muni.fi.pv168.CarManagerTest.java

private static DataSource prepareDataSource() throws SQLException {
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl("jdbc:derby:memory:CarRentalDB;create=true");
    return ds;//from w w w . ja  va 2 s.  c om
}

From source file:com.wso2.raspberrypi.Util.java

private static BasicDataSource getBasicDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://rss1.stratoslive.wso2.com/rpi_azeez_org");
    ds.setUsername("rpi_FHdBOhR3");
    ds.setPassword("wso2");
    ds.setValidationQuery("SELECT 1");
    return ds;/*from   w ww .  ja  v  a 2 s .com*/
}

From source file:com.uber.hoodie.hive.client.HoodieHiveClient.java

private DataSource getDatasource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driverName);/*from   w  w  w  . j a  v  a  2s  .c  o m*/
    ds.setUrl(getHiveJdbcUrlWithDefaultDBName());
    ds.setUsername(configuration.getHiveUsername());
    ds.setPassword(configuration.getHivePassword());
    return ds;
}

From source file:edu.si.services.sidora.rest.batch.BatchServiceTest.java

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry reg = super.createRegistry();

    if (USE_MYSQL_DB) {
        BasicDataSource testMySQLdb = new BasicDataSource();
        testMySQLdb.setDriverClassName("com.mysql.jdbc.Driver");
        testMySQLdb.setUrl(/*from   ww w.  j  a v a 2 s  . co m*/
                "jdbc:mysql://" + props.getProperty("mysql.host") + ":" + props.getProperty("mysql.port") + "/"
                        + props.getProperty("mysql.database") + "?zeroDateTimeBehavior=convertToNull");
        testMySQLdb.setUsername(props.getProperty("mysql.username").toString());
        testMySQLdb.setPassword(props.getProperty("mysql.password").toString());
        reg.bind("dataSource", testMySQLdb);
    } else {
        reg.bind("dataSource", db);
    }

    reg.bind("dataSource", db);

    reg.bind("jsonProvider", org.apache.cxf.jaxrs.provider.json.JSONProvider.class);
    reg.bind("jaxbProvider", org.apache.cxf.jaxrs.provider.JAXBElementProvider.class);

    return reg;
}

From source file:io.druid.db.DbConnector.java

private DataSource getDatasource() {
    DbConnectorConfig connectorConfig = config.get();

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUsername(connectorConfig.getUser());
    dataSource.setPassword(connectorConfig.getPassword());
    String uri = connectorConfig.getConnectURI();
    isPostgreSQL = uri.startsWith("jdbc:postgresql");
    dataSource.setUrl(uri);/*  w  w w .  j  a  v  a 2 s .co m*/

    if (connectorConfig.isUseValidationQuery()) {
        dataSource.setValidationQuery(connectorConfig.getValidationQuery());
        dataSource.setTestOnBorrow(true);
    }

    return dataSource;
}

From source file:com.atypon.wayf.guice.WayfGuiceModule.java

@Provides
@Singleton/*from   ww w. j a  va  2  s.co m*/
public NamedParameterJdbcTemplate getJdbcTemplate(@Named("jdbc.driver") String driver,
        @Named("jdbc.username") String username, @Named("jdbc.password") String password,
        @Named("jdbc.url") String url, @Named("jdbc.maxActive") Integer maxActive,
        @Named("jdbc.maxIdle") Integer maxIdle, @Named("jdbc.initialSize") Integer initialSize,
        @Named("jdbc.validationQuery") String validationQuery) {
    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(driver);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setUrl(url);
    dataSource.setMaxActive(maxActive);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setInitialSize(initialSize);
    dataSource.setValidationQuery(validationQuery);

    return new NamedParameterJdbcTemplate(dataSource);
}

From source file:com.jolbox.benchmark.BenchmarkTests.java

/**
 * //  ww  w.  j a  v a2  s .c  om
 *
 * @return time taken
 * @throws SQLException
 */
private long singleDBCP() throws SQLException {
    // Start DBCP

    BasicDataSource cpds = new BasicDataSource();
    cpds.setDriverClassName("com.jolbox.bonecp.MockJDBCDriver");
    cpds.setUrl(url);
    cpds.setUsername(username);
    cpds.setPassword(password);
    cpds.setMaxIdle(-1);
    cpds.setMinIdle(-1);
    cpds.setMaxOpenPreparedStatements(max_statement);
    cpds.setInitialSize(pool_size);
    cpds.setMaxActive(pool_size);
    cpds.getConnection(); // call to initialize possible lazy structures etc 

    long start = System.currentTimeMillis();
    for (int i = 0; i < MAX_CONNECTIONS; i++) {
        Connection conn = cpds.getConnection();
        conn.close();
    }
    long end = (System.currentTimeMillis() - start);
    //      System.out.println("DBCP Single thread benchmark: "+end);

    cpds.close();
    return end;

}