Java Code Examples for org.apache.commons.dbcp.BasicDataSource#setMinIdle()

The following examples show how to use org.apache.commons.dbcp.BasicDataSource#setMinIdle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcPushDownConnectionManager.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private JdbcPushDownConnectionManager(KylinConfig config, String id) throws ClassNotFoundException {
    dataSource = new BasicDataSource();

    Class.forName(config.getJdbcDriverClass(id));
    dataSource.setDriverClassName(config.getJdbcDriverClass(id));
    dataSource.setUrl(config.getJdbcUrl(id));
    dataSource.setUsername(config.getJdbcUsername(id));
    dataSource.setPassword(config.getJdbcPassword(id));
    dataSource.setMaxActive(config.getPoolMaxTotal(id));
    dataSource.setMaxIdle(config.getPoolMaxIdle(id));
    dataSource.setMinIdle(config.getPoolMinIdle(id));

    // Default settings
    dataSource.setTestOnBorrow(true);
    dataSource.setValidationQuery("select 1");
    dataSource.setRemoveAbandoned(true);
    dataSource.setRemoveAbandonedTimeout(300);
}
 
Example 2
Source File: AbstractJdbcAdaptor.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor method.
 * @param config Basic configuration of JDBC source, such as driver name, URL, username, password.
 * @throws Exception If datasource cannot be connected.
 */
protected AbstractJdbcAdaptor(AdaptorConfig config) throws ClassNotFoundException {
    this.config = config;
    this.dataSourceDef = DataSourceDefProvider.getInstance().getById(config.datasourceId);

    dataSource = new BasicDataSource();

    Class.forName(config.driver);
    dataSource.setDriverClassName(config.driver);
    dataSource.setUrl(config.url);
    dataSource.setUsername(config.username);
    dataSource.setPassword(config.password);
    dataSource.setMaxActive(config.poolMaxTotal);
    dataSource.setMaxIdle(config.poolMaxIdle);
    dataSource.setMinIdle(config.poolMinIdle);

    // Default settings
    dataSource.setTestOnBorrow(true);
    dataSource.setValidationQuery(getSourceValidationSql());
    dataSource.setRemoveAbandoned(true);
    dataSource.setRemoveAbandonedTimeout(300);

    DataSourceDefProvider provider = DataSourceDefProvider.getInstance();
    DataSourceDef jdbcDs = provider.getById(getDataSourceId());
    configurer = new DefaultConfigurer(this, jdbcDs);
}
 
Example 3
Source File: DataSourceContainer.java    From DBus with Apache License 2.0 6 votes vote down vote up
public boolean register(JdbcVo conf) {
    boolean isOk = true;
    try {
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName(conf.getDriverClass());
        bds.setUrl(conf.getUrl());
        bds.setUsername(conf.getUserName());
        bds.setPassword(conf.getPassword());
        bds.setInitialSize(conf.getInitialSize());
        bds.setMaxActive(conf.getMaxActive());
        bds.setMaxIdle(conf.getMaxIdle());
        bds.setMinIdle(conf.getMinIdle());
        cmap.put(conf.getKey(), bds);
    } catch (Exception e) {
        logger.error("[db container init key " + conf.getKey() + " datasource error!]", e);
        isOk = false;
    }
    return isOk;
}
 
Example 4
Source File: JdbcDataSourceFactory.java    From obevo with Apache License 2.0 6 votes vote down vote up
private static DataSource createFromJdbcUrl(Class<? extends Driver> driverClass, String url,
        Credential credential, int numThreads, ImmutableList<String> initSqls, Properties extraConnectionProperties) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClass.getName());
    // TODO validate non-null host name, notably for postgresl jdbc url
    dataSource.setUrl(url);
    dataSource.setUsername(credential.getUsername());
    dataSource.setPassword(credential.getPassword());

    // connection pool settings
    dataSource.setInitialSize(numThreads);
    dataSource.setMaxActive(numThreads);
    // keep the connections open if possible; only close them via the removeAbandonedTimeout feature
    dataSource.setMaxIdle(numThreads);
    dataSource.setMinIdle(0);
    dataSource.setRemoveAbandonedTimeout(300);

    dataSource.setConnectionInitSqls(initSqls.castToList());
    if (extraConnectionProperties != null) {
        for (String key : extraConnectionProperties.stringPropertyNames()) {
            dataSource.addConnectionProperty(key, extraConnectionProperties.getProperty(key));
        }
    }

    return dataSource;
}
 
Example 5
Source File: JdbcPushDownConnectionManager.java    From kylin with Apache License 2.0 6 votes vote down vote up
private JdbcPushDownConnectionManager(KylinConfig config, String id) throws ClassNotFoundException {
    dataSource = new BasicDataSource();

    Class.forName(config.getJdbcDriverClass(id));
    dataSource.setDriverClassName(config.getJdbcDriverClass(id));
    dataSource.setUrl(config.getJdbcUrl(id));
    dataSource.setUsername(config.getJdbcUsername(id));
    dataSource.setPassword(config.getJdbcPassword(id));
    dataSource.setMaxActive(config.getPoolMaxTotal(id));
    dataSource.setMaxIdle(config.getPoolMaxIdle(id));
    dataSource.setMinIdle(config.getPoolMinIdle(id));

    // Default settings
    dataSource.setTestOnBorrow(true);
    dataSource.setValidationQuery("select 1");
    dataSource.setRemoveAbandoned(true);
    dataSource.setRemoveAbandonedTimeout(300);
}
 
Example 6
Source File: AbstractJdbcAdaptor.java    From kylin with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor method.
 * @param config Basic configuration of JDBC source, such as driver name, URL, username, password.
 * @throws Exception If datasource cannot be connected.
 */
protected AbstractJdbcAdaptor(AdaptorConfig config) throws ClassNotFoundException {
    this.config = config;
    this.dataSourceDef = DataSourceDefProvider.getInstance().getById(config.datasourceId);

    dataSource = new BasicDataSource();

    Class.forName(config.driver);
    dataSource.setDriverClassName(config.driver);
    dataSource.setUrl(config.url);
    dataSource.setUsername(config.username);
    dataSource.setPassword(config.password);
    dataSource.setMaxActive(config.poolMaxTotal);
    dataSource.setMaxIdle(config.poolMaxIdle);
    dataSource.setMinIdle(config.poolMinIdle);

    // Default settings
    dataSource.setTestOnBorrow(true);
    dataSource.setValidationQuery(getSourceValidationSql());
    dataSource.setRemoveAbandoned(true);
    dataSource.setRemoveAbandonedTimeout(300);

    DataSourceDefProvider provider = DataSourceDefProvider.getInstance();
    DataSourceDef jdbcDs = provider.getById(getDataSourceId());
    configurer = new DefaultConfigurer(this, jdbcDs);
}
 
Example 7
Source File: JDBCPersistenceProvider.java    From oxd with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    try {
        JDBCConfiguration jdbcConfiguration = asJDBCConfiguration(configurationService.getConfiguration());
        validate(jdbcConfiguration);

        dataSource = new BasicDataSource();
        dataSource.setDriverClassName(jdbcConfiguration.getDriver());
        dataSource.setUrl(jdbcConfiguration.getJdbcUrl());
        dataSource.setUsername(jdbcConfiguration.getUsername());
        dataSource.setPassword(jdbcConfiguration.getPassword());

        dataSource.setMinIdle(5);
        dataSource.setMaxIdle(10);
        dataSource.setMaxOpenPreparedStatements(100);
    } catch (Exception e) {
        LOG.error("Error in creating jdbc connection.", e);
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: CompareWithPopularPool.java    From clearpool with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testDbcp() throws Exception {
  final BasicDataSource dataSource = new BasicDataSource();
  dataSource.setInitialSize(this.corePoolSize);
  dataSource.setMaxActive(this.maxPoolSize);
  dataSource.setMinIdle(this.corePoolSize);
  dataSource.setMaxIdle(this.maxPoolSize);
  dataSource.setPoolPreparedStatements(true);
  dataSource.setDriverClassName(this.driverClassName);
  dataSource.setUrl(this.url);
  dataSource.setPoolPreparedStatements(true);
  dataSource.setUsername(this.username);
  dataSource.setPassword(this.password);
  dataSource.setValidationQuery("SELECT 1");
  dataSource.setTestOnBorrow(false);
  for (int i = 0; i < this.loop; ++i) {
    ThreadProcessUtils.process(dataSource, "dbcp", this.count, threadCount, physicalCon);
  }
  System.out.println();
}
 
Example 9
Source File: DBCPDataSource.java    From maven-framework-project with MIT License 6 votes vote down vote up
private DBCPDataSource() throws IOException, SQLException,
		PropertyVetoException {

	ds = new BasicDataSource();
	ds.setDriverClassName("org.h2.Driver");
	ds.setUsername("sa");
	ds.setPassword("");
	ds.setUrl("jdbc:h2:./target/test;AUTO_SERVER=TRUE");

	// the settings below are optional -- dbcp can work with defaults
	ds.setMinIdle(5);
	ds.setMaxIdle(20);
	ds.setMaxOpenPreparedStatements(180);

	this.setDataSource(ds);

}
 
Example 10
Source File: DSSMybatisConfig.java    From DataSphereStudio with Apache License 2.0 5 votes vote down vote up
@Bean(name = "dataSource", destroyMethod = "close")
@Primary
public DataSource dataSource() {
    BasicDataSource datasource = new BasicDataSource();
    String dbUrl = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_URL.getValue();
    String username = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_USERNAME.getValue();
    String password = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_PASSWORD.getValue();
    String driverClassName = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_DRIVER_CLASS_NAME.getValue();
    int initialSize = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_INITIALSIZE.getValue();
    int minIdle = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_MINIDLE.getValue();
    int maxActive = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_MAXACTIVE.getValue();
    int maxWait = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_MAXWAIT.getValue();
    int timeBetweenEvictionRunsMillis = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_TBERM.getValue();
    int minEvictableIdleTimeMillis = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_MEITM.getValue();
    String validationQuery = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_VALIDATIONQUERY.getValue();
    boolean testWhileIdle = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_TESTWHILEIDLE.getValue();
    boolean testOnBorrow = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_TESTONBORROW.getValue();
    boolean testOnReturn = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_TESTONRETURN.getValue();
    boolean poolPreparedStatements = DSSServerConf.BDP_SERVER_MYBATIS_DATASOURCE_POOLPREPAREDSTATEMENTS.getValue();
    datasource.setUrl(dbUrl);
    datasource.setUsername(username);
    datasource.setPassword(password);
    datasource.setDriverClassName(driverClassName);
    datasource.setInitialSize(initialSize);
    datasource.setMinIdle(minIdle);
    datasource.setMaxActive(maxActive);
    datasource.setMaxWait(maxWait);
    datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    datasource.setValidationQuery(validationQuery);
    datasource.setTestWhileIdle(testWhileIdle);
    datasource.setTestOnBorrow(testOnBorrow);
    datasource.setTestOnReturn(testOnReturn);
    datasource.setPoolPreparedStatements(poolPreparedStatements);
    logger.info("Database connection address information(数据库连接地址信息)=" + dbUrl);
    return datasource;
}
 
Example 11
Source File: DefaultDataSourceFactory.java    From PeonyFramwork with Apache License 2.0 5 votes vote down vote up
@Override
    public void setAdvancedConfig(BasicDataSource ds) {
        /**
         * <!-- 配置初始化大小、最小、最大 -->
         <property name="initialSize" value="1" />
         <property name="minIdle" value="1" />
         <property name="maxActive" value="100" />

         <!-- 配置获取连接等待超时的时间 -->
         <property name="maxWait" value="10000" />

         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
         <property name="timeBetweenEvictionRunsMillis" value="600000" />

         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
         <property name="minEvictableIdleTimeMillis" value="600000" />

         <property name="testWhileIdle" value="true" />

         <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
         <property name="testOnBorrow" value="false" />
         <property name="testOnReturn" value="false" />

         <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
         <property name="validationQuery" value="select 1 " />
         */
        ds.setInitialSize(1);
        ds.setMinIdle(1);
        ds.setMaxActive(400);
        ds.setMaxWait(10000);
        ds.setTimeBetweenEvictionRunsMillis(600000);
        ds.setMinEvictableIdleTimeMillis(600000);
        ds.setTestWhileIdle(true);
        ds.setTestOnBorrow(true);
        ds.setTestOnReturn(false);
        ds.setValidationQuery("select 1");
        ds.setConnectionInitSqls(Arrays.asList("set names utf8mb4")); // 支持表情符
        // 解决 java.sql.SQLException: Already closed. 的问题(连接池会自动关闭长时间没有使用的连接)
//        ds.setValidationQuery("select 1 from dual");
    }
 
Example 12
Source File: MySQLDBConnection.java    From Gatekeeper with Apache License 2.0 5 votes vote down vote up
private JdbcTemplate connect(String url, String gkUserPassword) throws SQLException {
    String dbUrl = "jdbc:mysql://" + url;

    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName("org.mariadb.jdbc.Driver");
    dataSource.setUrl(dbUrl+"?"+ssl);
    dataSource.setUsername(gkUserName);
    dataSource.setPassword(gkUserPassword);
    dataSource.setMinIdle(0);
    dataSource.setMaxIdle(0);

    return new JdbcTemplate(dataSource);
}
 
Example 13
Source File: DBCPDataSourceCreater.java    From tangyuan2 with GNU General Public License v3.0 4 votes vote down vote up
private BasicDataSource createDataSource(Map<String, String> properties, String urlPattern) {
	BasicDataSource dsPool = new BasicDataSource();

	dsPool.setDriverClassName(DSPropertyUtil.getPropertyStringValue("driver", properties, null, null, true));
	dsPool.setUsername(DSPropertyUtil.getPropertyStringValue("username", properties, null, null, true));
	dsPool.setPassword(DSPropertyUtil.getPropertyStringValue("password", properties, null, null, true));

	String url = DSPropertyUtil.getPropertyStringValue("url", properties, null, null, true);
	if (null != urlPattern) {
		url = DSPropertyUtil.replace(url, "{}", urlPattern);
	}
	dsPool.setUrl(url);
	dsPool.setPoolPreparedStatements(DSPropertyUtil.getPropertyBooleanValue("poolingStatements", properties, null, true, true)); // 开启池的prepared
																																	// 池功能
	dsPool.setRemoveAbandoned(DSPropertyUtil.getPropertyBooleanValue("removeAbandoned", properties, null, true, true));
	dsPool.setRemoveAbandonedTimeout(DSPropertyUtil.getPropertyIntegerValue("removeAbandonedTimeout", properties, null, 1000, true));
	dsPool.setLogAbandoned(DSPropertyUtil.getPropertyBooleanValue("logAbandoned", properties, null, true, true));

	dsPool.setInitialSize(DSPropertyUtil.getPropertyIntegerValue("initialSize", properties, null, 2, true));
	dsPool.setMaxActive(DSPropertyUtil.getPropertyIntegerValue("maxActive", properties, null, 8, true));
	dsPool.setMaxIdle(DSPropertyUtil.getPropertyIntegerValue("maxIdle", properties, null, 8, true));
	dsPool.setMinIdle(DSPropertyUtil.getPropertyIntegerValue("minIdle", properties, null, 0, true));
	dsPool.setMaxWait(DSPropertyUtil.getPropertyIntegerValue("maxWait", properties, null, 10000, true));

	dsPool.setTimeBetweenEvictionRunsMillis(DSPropertyUtil.getPropertyIntegerValue("timeBetweenEvictionRunsMillis", properties, null, -1, true));

	dsPool.setTestOnBorrow(DSPropertyUtil.getPropertyBooleanValue("testOnBorrow", properties, null, false, true));
	dsPool.setTestOnReturn(DSPropertyUtil.getPropertyBooleanValue("testOnReturn", properties, null, false, true));
	dsPool.setTestWhileIdle(DSPropertyUtil.getPropertyBooleanValue("testWhileIdle", properties, null, false, true));

	String validationQuery = DSPropertyUtil.getPropertyStringValue("validationQuery", properties, null, null, false);
	if (null != validationQuery) {
		dsPool.setValidationQuery(validationQuery);
	}
	int timeout = DSPropertyUtil.getPropertyIntegerValue("", properties, null, -1, false);
	if (timeout > -1) {
		dsPool.setValidationQueryTimeout(timeout);
	}

	dsPool.setNumTestsPerEvictionRun(DSPropertyUtil.getPropertyIntegerValue("numTestsPerEvictionRun", properties, null, 10, true));
	dsPool.setMinEvictableIdleTimeMillis(DSPropertyUtil.getPropertyIntegerValue("minEvictableIdleTimeMillis", properties, null, 60000, true));

	// mysql:select 1
	// oracle:select 1 from dual
	// sqlserver:select 1
	// jtds:select 1

	boolean openTest = DSPropertyUtil.getPropertyBooleanValue("openTest", properties, null, false, false);
	if (openTest) {
		try {
			Connection conn = dsPool.getConnection();
			conn.close();
			log.info("test open database success.");
		} catch (Exception e) {
			throw new DataSourceException("test open database error: " + e.getMessage(), e);
		}
	}
	return dsPool;
}
 
Example 14
Source File: DbcpDataSourcePool.java    From Zebra with Apache License 2.0 4 votes vote down vote up
@Override
public DataSource build(DataSourceConfig config, boolean withDefaultValue) {
	BasicDataSource dbcpDataSource = new BasicDataSource();

	dbcpDataSource.setUrl(config.getJdbcUrl());
	dbcpDataSource.setUsername(config.getUsername());
	dbcpDataSource.setPassword(config.getPassword());
	dbcpDataSource.setDriverClassName(StringUtils.isNotBlank(config.getDriverClass()) ? config.getDriverClass() : JdbcDriverClassHelper.getDriverClassNameByJdbcUrl(config.getJdbcUrl()));

	if (withDefaultValue) {
		dbcpDataSource.setInitialSize(getIntProperty(config, "initialPoolSize", 5));
		dbcpDataSource.setMaxActive(getIntProperty(config, "maxPoolSize", 30));
		dbcpDataSource.setMinIdle(getIntProperty(config, "minPoolSize", 5));
		dbcpDataSource.setMaxIdle(getIntProperty(config, "maxPoolSize", 20));
		dbcpDataSource.setMaxWait(getIntProperty(config, "checkoutTimeout", 1000));
		dbcpDataSource.setValidationQuery(getStringProperty(config, "preferredTestQuery", "SELECT 1"));
		dbcpDataSource.setMinEvictableIdleTimeMillis(getIntProperty(config, "minEvictableIdleTimeMillis", 1800000));// 30min
		dbcpDataSource.setTimeBetweenEvictionRunsMillis(getIntProperty(config, "timeBetweenEvictionRunsMillis", 30000)); // 30s
		dbcpDataSource.setRemoveAbandonedTimeout(getIntProperty(config, "removeAbandonedTimeout", 300)); // 30s
		dbcpDataSource.setNumTestsPerEvictionRun(getIntProperty(config, "numTestsPerEvictionRun", 6)); // 30s
		dbcpDataSource.setValidationQueryTimeout(getIntProperty(config, "validationQueryTimeout", 0));
		if (StringUtils.isNotBlank(getStringProperty(config, "connectionInitSql", null))) {
			List<String> initSqls = new ArrayList<String>();
			initSqls.add(getStringProperty(config, "connectionInitSql", null));
			dbcpDataSource.setConnectionInitSqls(initSqls);
		}

		dbcpDataSource.setTestWhileIdle(true);
		dbcpDataSource.setTestOnBorrow(false);
		dbcpDataSource.setTestOnReturn(false);
		dbcpDataSource.setRemoveAbandoned(true);

	} else {
		try {
			PropertiesInit<BasicDataSource> propertiesInit = new PropertiesInit<BasicDataSource>(dbcpDataSource);
			propertiesInit.initPoolProperties(config);
		} catch (Exception e) {
			throw new ZebraConfigException(String.format("dbcp2 dataSource [%s] created error : ", config.getId()),
					e);
		}
	}

	this.pool = dbcpDataSource;
	LOGGER.info(String.format("New dataSource [%s] created.", config.getId()));

	return this.pool;
}