Search in sources :

Example 56 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project otter by alibaba.

the class DataSourceCreator method createDataSource.

private DataSource createDataSource(String url, String userName, String password, String driverClassName, DataMediaType dataMediaType, String encoding) {
    BasicDataSource dbcpDs = new BasicDataSource();
    // 初始化连接池时创建的连接数
    dbcpDs.setInitialSize(initialSize);
    // 连接池允许的最大并发连接数,值为非正数时表示不限制
    dbcpDs.setMaxActive(maxActive);
    // 连接池中的最大空闲连接数,超过时,多余的空闲连接将会被释放,值为负数时表示不限制
    dbcpDs.setMaxIdle(maxIdle);
    // 连接池中的最小空闲连接数,低于此数值时将会创建所欠缺的连接,值为0时表示不创建
    dbcpDs.setMinIdle(minIdle);
    // 以毫秒表示的当连接池中没有可用连接时等待可用连接返回的时间,超时则抛出异常,值为-1时表示无限等待
    dbcpDs.setMaxWait(maxWait);
    // 是否清除已经超过removeAbandonedTimeout设置的无效连接
    dbcpDs.setRemoveAbandoned(true);
    // 当清除无效链接时是否在日志中记录清除信息的标志
    dbcpDs.setLogAbandoned(true);
    // 以秒表示清除无效链接的时限
    dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    // 确保连接池中没有已破损的连接
    dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    // 指定连接被调用时是否经过校验
    dbcpDs.setTestOnBorrow(false);
    // 指定连接返回到池中时是否经过校验
    dbcpDs.setTestOnReturn(false);
    // 指定连接进入空闲状态时是否经过空闲对象驱逐进程的校验
    dbcpDs.setTestWhileIdle(true);
    // 以毫秒表示空闲对象驱逐进程由运行状态进入休眠状态的时长,值为非正数时表示不运行任何空闲对象驱逐进程
    dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    // 以毫秒表示连接被空闲对象驱逐进程驱逐前在池中保持空闲状态的最小时间
    dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    // 动态的参数
    dbcpDs.setDriverClassName(driverClassName);
    dbcpDs.setUrl(url);
    dbcpDs.setUsername(userName);
    dbcpDs.setPassword(password);
    if (dataMediaType.isOracle()) {
        dbcpDs.addConnectionProperty("restrictGetTables", "true");
    // dbcpDs.setValidationQuery("select 1 from dual");
    } else if (dataMediaType.isMysql()) {
        // open the batch mode for mysql since 5.1.8
        dbcpDs.addConnectionProperty("useServerPrepStmts", "false");
        dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true");
        // 将0000-00-00的时间类型返回null
        dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");
        // 直接返回字符串,不做year转换date处理
        dbcpDs.addConnectionProperty("yearIsDateType", "false");
        // 返回时间类型的字符串,不做时区处理
        dbcpDs.addConnectionProperty("noDatetimeStringSync", "true");
        if (StringUtils.isNotEmpty(encoding)) {
            if (StringUtils.equalsIgnoreCase(encoding, "utf8mb4")) {
                dbcpDs.addConnectionProperty("characterEncoding", "utf8");
                dbcpDs.setConnectionInitSqls(Arrays.asList("set names utf8mb4"));
            } else {
                dbcpDs.addConnectionProperty("characterEncoding", encoding);
            }
        }
    // dbcpDs.setValidationQuery("select 1");
    } else {
        logger.error("ERROR ## Unknow database type");
    }
    return dbcpDs;
}
Also used : BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Example 57 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project druid by alibaba.

the class Case2 method test_1.

public void test_1() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);
    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();
}
Also used : BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Example 58 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project druid by alibaba.

the class Oracle_Case3 method test_1.

public void test_1() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setMaxActive(maxActive);
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    for (int i = 0; i < loopCount; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}
Also used : BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Example 59 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project druid by alibaba.

the class Case0 method f_test_1.

public void f_test_1() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);
    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();
}
Also used : BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Example 60 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project druid by alibaba.

the class Case1 method test_dbcp.

public void test_dbcp() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);
    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(false);
    for (int i = 0; i < loopCount; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}
Also used : BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Aggregations

BasicDataSource (org.apache.commons.dbcp.BasicDataSource)141 Connection (java.sql.Connection)25 Test (org.junit.Test)13 SQLException (java.sql.SQLException)12 Properties (java.util.Properties)12 DataSource (javax.sql.DataSource)10 Platform (org.apache.ddlutils.Platform)8 Database (org.apache.ddlutils.model.Database)8 DdlGenerator (siena.jdbc.ddl.DdlGenerator)8 Statement (java.sql.Statement)7 Bean (org.springframework.context.annotation.Bean)6 ResultSet (java.sql.ResultSet)5 Before (org.junit.Before)5 JdbcPersistenceManager (siena.jdbc.JdbcPersistenceManager)5 Config (com.typesafe.config.Config)4 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Test (org.testng.annotations.Test)3 PostgresqlPersistenceManager (siena.jdbc.PostgresqlPersistenceManager)3