Java Code Examples for org.apache.commons.dbcp.BasicDataSource#setMaxIdle()
The following examples show how to use
org.apache.commons.dbcp.BasicDataSource#setMaxIdle() .
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: PersistService.java From diamond with Apache License 2.0 | 6 votes |
@PostConstruct public void initDataSource() throws Exception { // 读取jdbc.properties配置, 加载数据源 Properties props = ResourceUtils.getResourceAsProperties("jdbc.properties"); BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(JDBC_DRIVER_NAME); ds.setUrl(ensurePropValueNotNull(props.getProperty("db.url"))); ds.setUsername(ensurePropValueNotNull(props.getProperty("db.user"))); ds.setPassword(ensurePropValueNotNull(props.getProperty("db.password"))); ds.setInitialSize(Integer.parseInt(ensurePropValueNotNull(props.getProperty("db.initialSize")))); ds.setMaxActive(Integer.parseInt(ensurePropValueNotNull(props.getProperty("db.maxActive")))); ds.setMaxIdle(Integer.parseInt(ensurePropValueNotNull(props.getProperty("db.maxIdle")))); ds.setMaxWait(Long.parseLong(ensurePropValueNotNull(props.getProperty("db.maxWait")))); ds.setPoolPreparedStatements(Boolean.parseBoolean(ensurePropValueNotNull(props .getProperty("db.poolPreparedStatements")))); this.jt = new JdbcTemplate(); this.jt.setDataSource(ds); // 设置最大记录数,防止内存膨胀 this.jt.setMaxRows(MAX_ROWS); // 设置JDBC执行超时时间 this.jt.setQueryTimeout(QUERY_TIMEOUT); }
Example 2
Source File: DBCPDataSource.java From maven-framework-project with MIT License | 6 votes |
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 3
Source File: CompareWithPopularPool.java From clearpool with GNU General Public License v3.0 | 6 votes |
@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 4
Source File: JDBCPersistenceProvider.java From oxd with Apache License 2.0 | 6 votes |
@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 5
Source File: JdbcHelper.java From snakerflow with Apache License 2.0 | 6 votes |
/** * 在没有任何dataSource注入的情况下,默认使用dbcp数据源 */ private static void initialize() { String driver = ConfigHelper.getProperty("jdbc.driver"); String url = ConfigHelper.getProperty("jdbc.url"); String username = ConfigHelper.getProperty("jdbc.username"); String password = ConfigHelper.getProperty("jdbc.password"); int maxActive = ConfigHelper.getNumerProperty("jdbc.max.active"); int maxIdle = ConfigHelper.getNumerProperty("jdbc.max.idle"); AssertHelper.notNull(driver); AssertHelper.notNull(url); AssertHelper.notNull(username); AssertHelper.notNull(password); //初始化DBCP数据源 BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); if(maxActive != 0) { ds.setMaxActive(maxActive); } if(maxIdle != 0) { ds.setMaxIdle(maxIdle); } dataSource = ds; }
Example 6
Source File: AbstractJdbcAdaptor.java From kylin with Apache License 2.0 | 6 votes |
/** * 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: JdbcPushDownConnectionManager.java From kylin with Apache License 2.0 | 6 votes |
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 8
Source File: JdbcPushDownConnectionManager.java From kylin-on-parquet-v2 with Apache License 2.0 | 6 votes |
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 9
Source File: JdbcDataSourceFactory.java From obevo with Apache License 2.0 | 6 votes |
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 10
Source File: DbHelper.java From MediaPlayer with GNU General Public License v2.0 | 6 votes |
public static QueryRunner getQueryRunner(){ if(DbHelper.dataSource==null){ //����dbcp����Դ BasicDataSource dbcpDataSource = new BasicDataSource(); dbcpDataSource.setUrl("jdbc:mysql://localhost:3306/employees?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"); dbcpDataSource.setDriverClassName("com.mysql.jdbc.Driver"); dbcpDataSource.setUsername("root"); dbcpDataSource.setPassword("root"); dbcpDataSource.setDefaultAutoCommit(true); dbcpDataSource.setMaxActive(100); dbcpDataSource.setMaxIdle(30); dbcpDataSource.setMaxWait(500); DbHelper.dataSource = (DataSource)dbcpDataSource; System.out.println("Initialize dbcp..."); } return new QueryRunner(DbHelper.dataSource); }
Example 11
Source File: PersistService.java From diamond with Apache License 2.0 | 6 votes |
@PostConstruct public void initDataSource() throws Exception { // 读取jdbc.properties配置, 加载数据源 Properties props = ResourceUtils.getResourceAsProperties("jdbc.properties"); BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(JDBC_DRIVER_NAME); ds.setUrl(ensurePropValueNotNull(props.getProperty("db.url"))); ds.setUsername(ensurePropValueNotNull(props.getProperty("db.user"))); ds.setPassword(ensurePropValueNotNull(props.getProperty("db.password"))); ds.setInitialSize(Integer.parseInt(ensurePropValueNotNull(props.getProperty("db.initialSize")))); ds.setMaxActive(Integer.parseInt(ensurePropValueNotNull(props.getProperty("db.maxActive")))); ds.setMaxIdle(Integer.parseInt(ensurePropValueNotNull(props.getProperty("db.maxIdle")))); ds.setMaxWait(Long.parseLong(ensurePropValueNotNull(props.getProperty("db.maxWait")))); ds.setPoolPreparedStatements(Boolean.parseBoolean(ensurePropValueNotNull(props .getProperty("db.poolPreparedStatements")))); this.jt = new JdbcTemplate(); this.jt.setDataSource(ds); // 设置最大记录数,防止内存膨胀 this.jt.setMaxRows(MAX_ROWS); // 设置JDBC执行超时时间 this.jt.setQueryTimeout(QUERY_TIMEOUT); }
Example 12
Source File: DataSourceContainer.java From DBus with Apache License 2.0 | 6 votes |
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 13
Source File: AbstractJdbcAdaptor.java From kylin-on-parquet-v2 with Apache License 2.0 | 6 votes |
/** * 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 14
Source File: DbCommonServiceImpl.java From bdf3 with Apache License 2.0 | 5 votes |
@Override public DataSource createDataSource(String url, String driverClassName, String username, String password) { BasicDataSource ds = new SerializableBasicDataSource(); ds.setUsername(username); ds.setPassword(password); ds.setUrl(url); ds.setDriverClassName(driverClassName); ds.setMaxActive(5); ds.setMaxIdle(2); return ds; }
Example 15
Source File: AbstractWebDriverTestClass.java From Asqatasun with GNU Affero General Public License v3.0 | 5 votes |
/** * Initialises the Db connection */ private void initDb() { dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername(dbUser); dataSource.setPassword(dbPassword); dataSource.setUrl("jdbc:mysql://" + dbUrl + "/" + dbName); dataSource.setMaxActive(10); dataSource.setMaxIdle(5); dataSource.setInitialSize(5); }
Example 16
Source File: MySQLDBConnection.java From Gatekeeper with Apache License 2.0 | 5 votes |
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 17
Source File: DBCPDataSourceCreater.java From tangyuan2 with GNU General Public License v3.0 | 4 votes |
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 18
Source File: DbcpBean.java From DistributedCrawler with Apache License 2.0 | 4 votes |
/** * 指定所有参数连接数据源 * * @param connectURI * 数据库 * @param username * 用户名 * @param pswd * 密码 * @param driverClass * 数据库连接驱动名 * @param initialSize * 初始连接池连接个数 * @param maxActive * 最大激活连接数 * @param maxIdle * 最大闲置连接数 * @param maxWait * 获得连接的最大等待毫秒数 * @return */ protected static void initDS(String connectURI, String username, String pswd, String driverClass, int initialSize, int maxActive, int maxIdle, int maxWait) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driverClass); ds.setUsername(username); ds.setPassword(pswd); ds.setUrl(connectURI); ds.setInitialSize(initialSize); // 初始的连接数; ds.setMaxActive(maxActive); ds.setMaxIdle(maxIdle); ds.setMaxWait(maxWait); DS = ds; LOG.info("数据源初始化完成"); }
Example 19
Source File: MySQLDataSources.java From pinlater with Apache License 2.0 | 4 votes |
private static DataSource createDataSource( String host, int port, String user, String passwd, int poolSize, int maxWaitMillis, int socketTimeoutMillis) { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl(String.format( "jdbc:mysql://%s:%d?" + "connectTimeout=5000&" + "socketTimeout=%d&" + "enableQueryTimeouts=false&" + "cachePrepStmts=true&" + "characterEncoding=UTF-8", host, port, socketTimeoutMillis)); dataSource.setUsername(user); dataSource.setPassword(passwd); dataSource.setDefaultAutoCommit(true); dataSource.setInitialSize(poolSize); dataSource.setMaxActive(poolSize); dataSource.setMaxIdle(poolSize); // deal with idle connection eviction dataSource.setValidationQuery("SELECT 1 FROM DUAL"); dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); dataSource.setTestWhileIdle(true); dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000); dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000); dataSource.setNumTestsPerEvictionRun(poolSize); // max wait in milliseconds for a connection. dataSource.setMaxWait(maxWaitMillis); // force connection pool initialization. Connection conn = null; try { // Here not getting the connection from ThreadLocal no need to worry about that. conn = dataSource.getConnection(); } catch (SQLException e) { LOG.error( String.format("Failed to get a mysql connection when creating DataSource, " + "host: %s, port: %d", host, port), e); } finally { JdbcUtils.closeConnection(conn); } return dataSource; }
Example 20
Source File: DataSourceConfig.java From oneops with Apache License 2.0 | 4 votes |
private void setNumConnections(BasicDataSource ds, int initialSize, int maxActive, int maxIdle) { ds.setInitialSize(initialSize); ds.setMaxActive(maxActive); ds.setMaxIdle(maxIdle); }