List of usage examples for org.apache.commons.dbcp BasicDataSource setTestOnBorrow
public synchronized void setTestOnBorrow(boolean testOnBorrow)
From source file:com.pinterest.deployservice.db.DatabaseUtil.java
/** * Create a MySQL datasource./*from w w w. j a v a2 s .com*/ * * @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:gobblin.metastore.MysqlStateStore.java
/** * creates a new {@link BasicDataSource} * @param config the properties used for datasource instantiation * @return//from w w w .j a v a2 s . c o m */ public static BasicDataSource newDataSource(Config config) { BasicDataSource basicDataSource = new BasicDataSource(); PasswordManager passwordManager = PasswordManager.getInstance(ConfigUtils.configToProperties(config)); basicDataSource .setDriverClassName(ConfigUtils.getString(config, ConfigurationKeys.STATE_STORE_DB_JDBC_DRIVER_KEY, ConfigurationKeys.DEFAULT_STATE_STORE_DB_JDBC_DRIVER)); // MySQL server can timeout a connection so need to validate connections before use basicDataSource.setValidationQuery("select 1"); basicDataSource.setTestOnBorrow(true); basicDataSource.setDefaultAutoCommit(false); basicDataSource.setTimeBetweenEvictionRunsMillis(60000); basicDataSource.setUrl(config.getString(ConfigurationKeys.STATE_STORE_DB_URL_KEY)); basicDataSource.setUsername( passwordManager.readPassword(config.getString(ConfigurationKeys.STATE_STORE_DB_USER_KEY))); basicDataSource.setPassword( passwordManager.readPassword(config.getString(ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY))); basicDataSource.setMinEvictableIdleTimeMillis( ConfigUtils.getLong(config, ConfigurationKeys.STATE_STORE_DB_CONN_MIN_EVICTABLE_IDLE_TIME_KEY, ConfigurationKeys.DEFAULT_STATE_STORE_DB_CONN_MIN_EVICTABLE_IDLE_TIME)); return basicDataSource; }
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/*from w w w .ja v a 2 s .co m*/ .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 . jav a 2s . 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);/*w ww.ja v a2s . c o m*/ 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.amuponda.estorehack.business.config.EstoreHackDataConfig.java
@Bean(destroyMethod = "close") public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/estore"); dataSource.setUsername("root"); dataSource.setPassword("uctatm"); //This should solve the notorious broken pipe exception dataSource.setValidationQuery("SELECT 1"); dataSource.setTestOnBorrow(true); dataSource.setRemoveAbandoned(true); dataSource.setRemoveAbandonedTimeout(60); dataSource.setLogAbandoned(true);//w ww.j a va 2 s . c om return dataSource; }
From source file:com.metamx.druid.db.DbConnector.java
private DataSource getDatasource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setUsername(config.getDatabaseUser()); dataSource.setPassword(config.getDatabasePassword()); dataSource.setUrl(config.getDatabaseConnectURI()); if (config.useValidationQuery()) { dataSource.setValidationQuery(config.getValidationQuery()); dataSource.setTestOnBorrow(true); }/*from ww w. jav a2 s . c o m*/ return dataSource; }
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 w w .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: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 .co 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:$.DataSourceConf.java
@Bean public DataSource dataSource() { final BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url);//from w w w . j av a2 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; }