List of usage examples for org.apache.commons.dbcp BasicDataSource setTestWhileIdle
public synchronized void setTestWhileIdle(boolean testWhileIdle)
testWhileIdle
property. From source file:com.weibo.datasys.parser.sql.DBConnectionFactory.java
public static void init() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(ConfigFactory.getString("jdbc.driverClassName")); dataSource.setUrl(ConfigFactory.getString("jdbc.url")); dataSource.setUsername(ConfigFactory.getString("jdbc.username")); dataSource.setPassword(ConfigFactory.getString("jdbc.password")); dataSource.setInitialSize(ConfigFactory.getInt("jdbc.initialSize", 1)); dataSource.setMinIdle(ConfigFactory.getInt("jdbc.minIdle", 2)); dataSource.setMaxIdle(ConfigFactory.getInt("jdbc.maxIdle", 10)); dataSource.setMaxWait(ConfigFactory.getInt("jdbc.maxWait", 1000)); dataSource.setMaxActive(ConfigFactory.getInt("jdbc.maxActive", 2)); dataSource.addConnectionProperty("autoReconnect", "true"); // ??/*from w w w .j a va 2 s .c o m*/ dataSource.setTestWhileIdle(true); // ?sql? dataSource.setValidationQuery("select 'test'"); // ? dataSource.setValidationQueryTimeout(5000); // dataSource.setTimeBetweenEvictionRunsMillis(3600000); // ?? dataSource.setMinEvictableIdleTimeMillis(3600000); ds = dataSource; }
From source file:com.pinterest.deployservice.db.DatabaseUtil.java
/** * Create a MySQL datasource./*from w w w . jav a2 s. c o m*/ * * @param url the url of the DB. * @param user the user name to connect to MySQL as. * @param passwd the password for the corresponding MySQL user. * @param poolSize the connection pool size string, in the format of * initialSize:maxActive:maxIdle:minIdle. * @param maxWaitInMillis the max wait time in milliseconds to get a connection from the pool. * @return a BasicDataSource for the target MySQL instance. */ public static BasicDataSource createDataSource(String driverClassName, String url, String user, String passwd, String poolSize, int maxWaitInMillis) { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(passwd); dataSource.setDefaultAutoCommit(true); dataSource.setDefaultReadOnly(false); // poolSize parsing, the poolsize string passed in the following format // initialSize:maxActive:maxIdle:minIdle String[] sizeStrs = poolSize.split(":"); dataSource.setInitialSize(Integer.parseInt(sizeStrs[0])); dataSource.setMaxActive(Integer.parseInt(sizeStrs[1])); dataSource.setMaxIdle(Integer.parseInt(sizeStrs[2])); dataSource.setMinIdle(Integer.parseInt(sizeStrs[3])); dataSource.setValidationQuery("SELECT 1"); dataSource.setTestOnBorrow(true); dataSource.setTestOnReturn(false); dataSource.setTestWhileIdle(true); dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000); dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000); // dataSource.setNumTestsPerEvictionRun(3); // max wait in milliseconds for a connection. dataSource.setMaxWait(maxWaitInMillis); // 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 db connection when creating DataSource, url = %s", url), e); } finally { DbUtils.closeQuietly(conn); } return dataSource; }
From source file:com.pinterest.pinlater.backends.mysql.MySQLDataSources.java
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/* w ww. j a v a 2 s . com*/ .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; }
From source file:com.alibaba.otter.node.etl.common.datasource.AbstractDbDialectTest.java
private DataSource createDataSource(String url, String userName, String password, String driverClassName, DataMediaType dataMediaType, String encoding) { BasicDataSource dbcpDs = new BasicDataSource(); dbcpDs.setRemoveAbandoned(true);//from ww w . j av a 2 s . c om dbcpDs.setLogAbandoned(true); dbcpDs.setTestOnBorrow(true); dbcpDs.setTestWhileIdle(true); // ?? 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", "true"); dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true"); if (StringUtils.isNotEmpty(encoding)) { dbcpDs.addConnectionProperty("characterEncoding", encoding); } dbcpDs.setValidationQuery("select 1"); } return dbcpDs; }
From source file:com.widsons.spr4.conf.DataBaseConf.java
@Bean(destroyMethod = "close") public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setUrl(dbUrl);//from w w w .j a v a2 s .c om dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); dataSource.setTestOnBorrow(true); dataSource.setTestOnReturn(true); dataSource.setTestWhileIdle(true); dataSource.setTimeBetweenEvictionRunsMillis(1800000); dataSource.setNumTestsPerEvictionRun(3); dataSource.setMinEvictableIdleTimeMillis(1800000); return dataSource; }
From source file:com.weibo.datasys.common.db.DBManager.java
/** * /*www .j a v a 2 s. c om*/ * ????? * * @param configData */ private static void initDataSource(CommonData configData) { String dsname = configData.getBaseField("dsname"); BasicDataSource dataSource = new BasicDataSource(); // ?? dataSource.setDriverClassName(configData.getBaseField("driverClassName")); // ?? dataSource.setUrl(configData.getBaseField("connectURL")); // ??? dataSource.setUsername(configData.getBaseField("username")); dataSource.setPassword(configData.getBaseField("password")); // ? dataSource.setInitialSize(StringUtils.parseInt(configData.getBaseField("initialSize"), 1)); // ? dataSource.setMinIdle(StringUtils.parseInt(configData.getBaseField("minIdle"), 1)); // dataSource.setMaxIdle(StringUtils.parseInt(configData.getBaseField("maxIdle"), 1)); // dataSource.setMaxActive(StringUtils.parseInt(configData.getBaseField("maxActive"), 1)); // ?,?(ms) dataSource.setMaxWait(StringUtils.parseInt(configData.getBaseField("maxWait"), 1000)); // ?? dataSource.setTestWhileIdle(true); // ?sql? dataSource.setValidationQuery("select 'test'"); // ? dataSource.setValidationQueryTimeout(5000); // dataSource.setTimeBetweenEvictionRunsMillis(3600000); // ?? dataSource.setMinEvictableIdleTimeMillis(3600000); dsMap.put(dsname, dataSource); logger.info("[InitDataSourceOK] - dsname={}", dsname); }
From source file:com.alibaba.druid.pool.dbcp.Test0.java
public void test_idle() throws Exception { MockDriver driver = MockDriver.instance; BasicDataSource dataSource = new BasicDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver"); dataSource.setInitialSize(0);/*w ww. j a v a 2 s .c o m*/ dataSource.setMaxActive(4); dataSource.setMaxIdle(4); dataSource.setMinIdle(1); dataSource.setMinEvictableIdleTimeMillis(5000 * 1); dataSource.setTimeBetweenEvictionRunsMillis(10); dataSource.setTestWhileIdle(false); dataSource.setTestOnBorrow(false); dataSource.setValidationQuery("SELECT 1"); { Connection conn = dataSource.getConnection(); // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size()); System.out.println("raw size : " + driver.getConnections().size()); conn.close(); System.out.println("raw size : " + driver.getConnections().size()); } { Connection conn = dataSource.getConnection(); // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size()); System.out.println("raw size : " + driver.getConnections().size()); conn.close(); System.out.println("raw size : " + driver.getConnections().size()); } dataSource.close(); }
From source file:fr.gouv.diplomatie.applitutoriel.integration.conf.DataSourceConf.java
@Bean public DataSource dataSource() { final BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url);//from w ww.j av a 2 s . c o m dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setValidationQuery(validationQuery); dataSource.setMaxActive(maxActive); dataSource.setMaxIdle(maxIdle); dataSource.setMaxWait(maxWait); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun); dataSource.setRemoveAbandoned(removeAbandoned); dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); dataSource.setLogAbandoned(logAbandoned); return dataSource; }
From source file:$.DataSourceConf.java
@Bean public DataSource dataSource() { final BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url);/*from w ww.j a va2 s .c o m*/ dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setValidationQuery(validationQuery); dataSource.setMaxActive(maxActive); dataSource.setMaxIdle(maxIdle); dataSource.setMaxWait(maxWait); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun); dataSource.setRemoveAbandoned(removeAbandoned); dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); dataSource.setLogAbandoned(logAbandoned); return dataSource; }
From source file:binky.reportrunner.service.impl.DatasourceServiceImpl.java
private DataSource getDs(RunnerDataSource runnerDs) throws SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException, PropertyVetoException, NamingException, EncryptionException { final String jndiDataSource = runnerDs.getJndiName(); if (StringUtils.isBlank(jndiDataSource)) { EncryptionUtil enc = new EncryptionUtil(); logger.info("using dbcp pooled connection for: " + runnerDs.getDataSourceName()); String jdbcUser = runnerDs.getUsername(); if (StringUtils.isBlank(runnerDs.getPassword())) throw new SecurityException("password is empty"); String jdbcPassword = enc.decrpyt(secureKey, runnerDs.getPassword()); String jdbcUrl = runnerDs.getJdbcUrl(); String databaseDriver = runnerDs.getJdbcClass(); Class.forName(databaseDriver).newInstance(); BasicDataSource ds1 = new BasicDataSource(); ds1.setDriverClassName(databaseDriver); ds1.setUrl(jdbcUrl);/*from w ww . java2 s.c om*/ ds1.setUsername(jdbcUser); ds1.setPassword(jdbcPassword); ds1.setInitialSize(runnerDs.getInitialPoolSize()); ds1.setMaxActive(runnerDs.getMaxPoolSize()); ds1.setRemoveAbandoned(true); ds1.setRemoveAbandonedTimeout(600); // do not want anything updating anything ds1.setDefaultReadOnly(true); ds1.setLogAbandoned(true); ds1.setTestOnBorrow(true); ds1.setTestOnReturn(true); ds1.setTestWhileIdle(true); // does this work across all RBMS? - no it doesn't //ds1.setValidationQuery("select 1"); //ds1.setValidationQueryTimeout(300); return ds1; } else { logger.info( "getting datasource from JNDI url: " + jndiDataSource + " for " + runnerDs.getDataSourceName()); Context initContext = new InitialContext(); DataSource ds = (DataSource) initContext.lookup("java:/comp/env/" + jndiDataSource); return ds; } }