List of usage examples for org.apache.commons.dbcp BasicDataSource setMinIdle
public synchronized void setMinIdle(int minIdle)
From source file:com.alibaba.cobar.manager.test.ConnectionTest.java
/** * @param args/*w w w. j a v a 2s. co m*/ */ public static void main(String[] args) { try { BasicDataSource ds = new BasicDataSource(); ds.setUsername("test"); ds.setPassword(""); ds.setUrl("jdbc:mysql://10.20.153.178:9066/"); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setMaxActive(-1); ds.setMinIdle(0); ds.setTimeBetweenEvictionRunsMillis(600000); ds.setNumTestsPerEvictionRun(Integer.MAX_VALUE); ds.setMinEvictableIdleTimeMillis(GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); Connection conn = ds.getConnection(); Statement stm = conn.createStatement(); stm.execute("show @@version"); ResultSet rst = stm.getResultSet(); rst.next(); String version = rst.getString("VERSION"); System.out.println(version); } catch (Exception exception) { System.out.println("10.20.153.178:9066 " + exception.getMessage() + exception); } }
From source file:com.google.gerrit.server.schema.H2AccountPatchReviewStore.java
private static DataSource createDataSource(String url) { BasicDataSource datasource = new BasicDataSource(); datasource.setDriverClassName("org.h2.Driver"); datasource.setUrl(url);/*from w ww. j a v a2 s .c o m*/ datasource.setMaxActive(50); datasource.setMinIdle(4); datasource.setMaxIdle(16); long evictIdleTimeMs = 1000 * 60; datasource.setMinEvictableIdleTimeMillis(evictIdleTimeMs); datasource.setTimeBetweenEvictionRunsMillis(evictIdleTimeMs / 2); return datasource; }
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"); // ??/* w w w .ja va 2 s . c om*/ dataSource.setTestWhileIdle(true); // ?sql? dataSource.setValidationQuery("select 'test'"); // ? dataSource.setValidationQueryTimeout(5000); // dataSource.setTimeBetweenEvictionRunsMillis(3600000); // ?? dataSource.setMinEvictableIdleTimeMillis(3600000); ds = dataSource; }
From source file:com.gs.obevo.db.impl.core.jdbc.JdbcDataSourceFactory.java
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()); dataSource.setUrl(url);/*w w w . j av a 2 s. c om*/ 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; }
From source file:net.sf.taverna.t2.provenance.api.ProvenanceAccess.java
/** * Initialises a named JNDI DataSource if not already set up externally. * The DataSource is named jdbc/taverna//from w ww .j av a 2s. c om * * @param driverClassName - the classname for the driver to be used. * @param jdbcUrl - the jdbc connection url * @param username - the username, if required (otherwise null) * @param password - the password, if required (oteherwise null) * @param minIdle - if the driver supports multiple connections, then the minumum number of idle connections in the pool * @param maxIdle - if the driver supports multiple connections, then the maximum number of idle connections in the pool * @param maxActive - if the driver supports multiple connections, then the minumum number of connections in the pool */ public static void initDataSource(String driverClassName, String jdbcUrl, String username, String password, int minIdle, int maxIdle, int maxActive) { System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory"); System.setProperty("org.osjava.sj.jndi.shared", "true"); BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driverClassName); ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); ds.setMaxActive(maxActive); ds.setMinIdle(minIdle); ds.setMaxIdle(maxIdle); ds.setDefaultAutoCommit(true); if (username != null) { ds.setUsername(username); } if (password != null) { ds.setPassword(password); } ds.setUrl(jdbcUrl); InitialContext context; try { context = new InitialContext(); context.rebind("jdbc/taverna", ds); } catch (NamingException ex) { logger.error("Problem rebinding the jdbc context: " + ex); } }
From source file:com.pinterest.deployservice.db.DatabaseUtil.java
/** * Create a MySQL datasource.//from ww w . j a va 2 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.weibo.datasys.common.db.DBManager.java
/** * //from w w w . j a v a 2 s .c o m * ????? * * @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.cobar.manager.qa.modle.CobarFactory.java
public static CobarAdapter getCobarAdapter(String cobarNodeName) throws IOException { CobarAdapter cAdapter = new CobarAdapter(); Properties prop = new Properties(); prop.load(CobarFactory.class.getClassLoader().getResourceAsStream("cobarNode.properties")); BasicDataSource ds = new BasicDataSource(); String user = prop.getProperty(cobarNodeName + ".user").trim(); String password = prop.getProperty(cobarNodeName + ".password").trim(); String ip = prop.getProperty(cobarNodeName + ".ip").trim(); int managerPort = Integer.parseInt(prop.getProperty(cobarNodeName + ".manager.port").trim()); int maxActive = -1; int minIdle = 0; long timeBetweenEvictionRunsMillis = 10 * 60 * 1000; int numTestsPerEvictionRun = Integer.MAX_VALUE; long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; ds.setUsername(user);//from w ww. j a v a 2 s . c o m ds.setPassword(password); ds.setUrl(new StringBuilder().append("jdbc:mysql://").append(ip).append(":").append(managerPort).append("/") .toString()); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setMaxActive(maxActive); ds.setMinIdle(minIdle); ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); ds.setNumTestsPerEvictionRun(numTestsPerEvictionRun); ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); cAdapter.setDataSource(ds); return cAdapter; }
From source file:com.alibaba.druid.benckmark.pool.Oracle_Case0.java
public void test_1() throws Exception { final BasicDataSource dataSource = new BasicDataSource(); dataSource.setInitialSize(initialSize); dataSource.setMaxActive(maxActive);/*from ww w. j ava 2s. com*/ 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(validationQuery); dataSource.setTestOnBorrow(true); for (int i = 0; i < LOOP_COUNT; ++i) { p0(dataSource, "dbcp"); } System.out.println(); }
From source file:edu.tamu.tcat.oss.account.test.mock.MockDataSource.java
public void activate() { try {/*from w w w. j a v a 2 s .co m*/ String url = props.getPropertyValue(PROP_URL, String.class); String user = props.getPropertyValue(PROP_USER, String.class); String pass = props.getPropertyValue(PROP_PASS, String.class); Objects.requireNonNull(url, "Database connection URL not supplied"); Objects.requireNonNull(user, "Database username not supplied"); Objects.requireNonNull(pass, "Database password not supplied"); int maxActive = getIntValue(props, PROP_MAX_ACTIVE, 30); int maxIdle = getIntValue(props, PROP_MAX_IDLE, 3); int minIdle = getIntValue(props, PROP_MIN_IDLE, 0); int minEviction = getIntValue(props, PROP_MIN_EVICTION, 10 * 1000); int betweenEviction = getIntValue(props, PROP_BETWEEN_EVICTION, 100); PostgreSqlDataSourceFactory factory = new PostgreSqlDataSourceFactory(); PostgreSqlPropertiesBuilder builder = factory.getPropertiesBuilder().create(url, user, pass); dataSource = factory.getDataSource(builder.getProperties()); //HACK: should add this API to the properties builder instead of downcasting and overriding { BasicDataSource basic = (BasicDataSource) dataSource; basic.setMaxActive(maxActive); basic.setMaxIdle(maxIdle); basic.setMinIdle(minIdle); basic.setMinEvictableIdleTimeMillis(minEviction); basic.setTimeBetweenEvictionRunsMillis(betweenEviction); } this.executor = Executors.newSingleThreadExecutor(); } catch (Exception e) { throw new IllegalStateException("Failed initializing data source", e); } }