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

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

Introduction

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

Prototype

public Connection getConnection() throws SQLException 

Source Link

Document

Create (if necessary) and return a connection to the database.

Usage

From source file:org.wso2.carbon.identity.openidconnect.util.TestUtils.java

public static void initiateH2Base() throws SQLException {

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUsername("username");
    dataSource.setPassword("password");
    dataSource.setUrl("jdbc:h2:mem:test" + DB_NAME);
    Connection connection = dataSource.getConnection();
    connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + getFilePath(H2_SCRIPT_NAME) + "'");

    dataSourceMap.put(DB_NAME, dataSource);
}

From source file:org.wso2.throttle.core.Throttler.java

/**
 * Copies physical ThrottleTable to this instance's in-memory ThrottleTable.
 *//*ww  w.j  a va 2  s .  c o  m*/
private void populateThrottleTable() {
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    basicDataSource.setUrl("jdbc:mysql://localhost/org_wso2_throttle_DataSource");
    basicDataSource.setUsername("root");
    basicDataSource.setPassword("root");

    Connection connection = null;
    try {
        connection = basicDataSource.getConnection();
        DatabaseMetaData dbm = connection.getMetaData();
        // check if "ThrottleTable" table is there
        ResultSet tables = dbm.getTables(null, null, RDBMS_THROTTLE_TABLE_NAME, null);
        if (tables.next()) { // Table exists
            PreparedStatement stmt = connection.prepareStatement("SELECT * FROM " + RDBMS_THROTTLE_TABLE_NAME);
            ResultSet resultSet = stmt.executeQuery();
            while (resultSet.next()) {
                String key = resultSet.getString(RDBMS_THROTTLE_TABLE_COLUMN_KEY);
                Boolean isThrottled = resultSet.getBoolean(RDBMS_THROTTLE_TABLE_COLUMN_ISTHROTTLED);
                try {
                    getGlobalThrottleStreamInputHandler().send(new Object[] { key, isThrottled });
                } catch (InterruptedException e) {
                    log.error("Error occurred while sending an event.", e);
                }
            }
        } else { // Table does not exist
            log.warn("RDBMS ThrottleTable does not exist. Make sure global throttler server is started.");
        }
    } catch (SQLException e) {
        log.error("Error occurred while copying throttle data from global throttler server.", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing database connection.", e);
            }
        }
    }
}

From source file:voldemort.performance.MysqlGrowth.java

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        System.err.println("USAGE: java MySQLGrowth total_size increment threads");
        System.exit(1);// w  ww .ja  v  a 2 s .  c o m
    }
    final int totalSize = Integer.parseInt(args[0]);
    final int increment = Integer.parseInt(args[1]);
    final int threads = Integer.parseInt(args[2]);

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("root");
    ds.setPassword("");
    ds.setUrl("jdbc:mysql://127.0.0.1:3306/test");
    final Connection conn = ds.getConnection();
    conn.createStatement().execute("truncate table test_table");

    final Random rand = new Random();
    int iterations = totalSize / increment;
    long[] readTimes = new long[iterations];
    long[] writeTimes = new long[iterations];
    ExecutorService service = Executors.newFixedThreadPool(threads);
    for (int i = 0; i < iterations; i++) {
        System.out.println("Starting iteration " + i);
        List<Future<Object>> results = new ArrayList<Future<Object>>(increment);
        long startTime = System.currentTimeMillis();
        final int fi = i;
        for (int j = 0; j < increment; j++) {
            final int fj = j;
            results.add(service.submit(new Callable<Object>() {

                public Object call() throws Exception {
                    upsert(conn, Integer.toString(fi * increment + fj), Integer.toString(fi * increment + fj));
                    return null;
                }
            }));
        }
        for (int j = 0; j < increment; j++)
            results.get(j).get();
        writeTimes[i] = System.currentTimeMillis() - startTime;
        System.out.println("write: " + (writeTimes[i] / (double) increment));
        results.clear();

        startTime = System.currentTimeMillis();
        for (int j = 0; j < increment; j++) {
            results.add(service.submit(new Callable<Object>() {

                public Object call() throws Exception {
                    return select(conn, Integer.toString(rand.nextInt((fi + 1) * increment)));
                }
            }));
        }
        for (int j = 0; j < increment; j++)
            results.get(j).get();
        readTimes[i] = (System.currentTimeMillis() - startTime);
        System.out.println("read: " + (readTimes[i] / (double) increment));
    }
    conn.close();

    System.out.println();
    System.out.println("iteration read write:");
    for (int i = 0; i < iterations; i++) {
        System.out.print(i);
        System.out.print(" " + readTimes[i] / (double) increment);
        System.out.println(" " + writeTimes[i] / (double) increment);
    }

    System.exit(0);
}