List of usage examples for org.apache.commons.dbcp BasicDataSource setTestOnReturn
public synchronized void setTestOnReturn(boolean testOnReturn)
testOnReturn
property. From source file:com.pinterest.deployservice.db.DatabaseUtil.java
/** * Create a MySQL datasource./*from www. ja 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.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//www .j a 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.widsons.spr4.conf.DataBaseConf.java
@Bean(destroyMethod = "close") public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setUrl(dbUrl);//from w w w. java2 s . 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:be.ugent.tiwi.sleroux.newsrec.newsreclib.dao.mysqlImpl.AbstractJDBCBaseDao.java
private synchronized BasicDataSource createConnectionPool() { BasicDataSource source; logger.debug("creating connectionpool"); String driver = bundle.getString("dbDriver"); String user = bundle.getString("dbUser"); String pass = bundle.getString("dbPass"); String url = bundle.getString("dbUrl"); url = url + "?user=" + user + "&password=" + pass; source = new BasicDataSource(); source.setDriverClassName(driver);//from ww w.ja va 2 s. c o m source.setUsername(user); source.setPassword(pass); source.setUrl(url); source.setTestOnReturn(true); source.setValidationQuery("SELECT 1"); logger.debug("connectionpool created"); return source; }
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);//w w w. ja v a 2 s. co m 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; } }
From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java
@Bean public DataSource userDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("user.jdbc.driverClassName")); dataSource.setUrl(env.getRequiredProperty("user.jdbc.url")); dataSource.setUsername(env.getRequiredProperty("user.jdbc.username")); dataSource.setPassword(env.getRequiredProperty("user.jdbc.password")); dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class)); dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class)); dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class)); dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class)); dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class)); dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class)); dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery")); dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class)); dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class)); dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class)); dataSource.setTimeBetweenEvictionRunsMillis( env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class)); dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class)); dataSource.setMinEvictableIdleTimeMillis( env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class)); return dataSource; }
From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java
@Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("gotour.jdbc.driverClassName")); dataSource.setUrl(env.getRequiredProperty("gotour.jdbc.url")); dataSource.setUsername(env.getRequiredProperty("gotour.jdbc.username")); dataSource.setPassword(env.getRequiredProperty("gotour.jdbc.password")); dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class)); dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class)); dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class)); dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class)); dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class)); dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class)); dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery")); dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class)); dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class)); dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class)); dataSource.setTimeBetweenEvictionRunsMillis( env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class)); dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class)); dataSource.setMinEvictableIdleTimeMillis( env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class)); return dataSource; }
From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java
@Bean public DataSource slaveDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("gotour.slave.jdbc.driverClassName")); dataSource.setUrl(env.getRequiredProperty("gotour.slave.jdbc.url")); dataSource.setUsername(env.getRequiredProperty("gotour.slave.jdbc.username")); dataSource.setPassword(env.getRequiredProperty("gotour.slave.jdbc.password")); dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class)); dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class)); dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class)); dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class)); dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class)); dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class)); dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery")); dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class)); dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class)); dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class)); dataSource.setTimeBetweenEvictionRunsMillis( env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class)); dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class)); dataSource.setMinEvictableIdleTimeMillis( env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class)); return dataSource; }
From source file:com.alibaba.otter.manager.biz.common.DataSourceCreator.java
private DataSource createDataSource(String url, String userName, String password, String driverClassName, DataMediaType dataMediaType, String encoding) { BasicDataSource dbcpDs = new BasicDataSource(); dbcpDs.setInitialSize(initialSize);// ? dbcpDs.setMaxActive(maxActive);// ????? dbcpDs.setMaxIdle(maxIdle);// ?? dbcpDs.setMinIdle(minIdle);// ?0? dbcpDs.setMaxWait(maxWait);// ??-1? dbcpDs.setRemoveAbandoned(true);// ??removeAbandonedTimeout dbcpDs.setLogAbandoned(true);// ?? dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // ? dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// ?? dbcpDs.setTestOnBorrow(false);// ?? dbcpDs.setTestOnReturn(false);// ?? dbcpDs.setTestWhileIdle(true);// ???? dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // ???????? dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // ??????? // ??//from w w w .j a va 2 s .com 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", "false"); dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true"); dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 0000-00-00null dbcpDs.addConnectionProperty("yearIsDateType", "false");// ??year?date? dbcpDs.addConnectionProperty("noDatetimeStringSync", "true");// ,??? if (StringUtils.isNotEmpty(encoding)) { if (StringUtils.equalsIgnoreCase(encoding, "utf8mb4")) { dbcpDs.addConnectionProperty("characterEncoding", "utf8"); dbcpDs.setConnectionInitSqls(Arrays.asList("set names utf8mb4")); } else { dbcpDs.addConnectionProperty("characterEncoding", encoding); } } // dbcpDs.setValidationQuery("select 1"); } else { logger.error("ERROR ## Unknow database type"); } return dbcpDs; }
From source file:com.alibaba.otter.node.etl.common.datasource.impl.DBDataSourceService.java
private DataSource createDataSource(String url, String userName, String password, String driverClassName, DataMediaType dataMediaType, String encoding) { BasicDataSource dbcpDs = new BasicDataSource(); dbcpDs.setInitialSize(initialSize);// ? dbcpDs.setMaxActive(maxActive);// ????? dbcpDs.setMaxIdle(maxIdle);// ?? dbcpDs.setMinIdle(minIdle);// ?0? dbcpDs.setMaxWait(maxWait);// ??-1? dbcpDs.setRemoveAbandoned(true);// ??removeAbandonedTimeout dbcpDs.setLogAbandoned(true);// ?? dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // ? dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// ?? dbcpDs.setTestOnBorrow(false);// ?? dbcpDs.setTestOnReturn(false);// ?? dbcpDs.setTestWhileIdle(true);// ???? dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // ???????? dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // ??????? // ??//from w w w . j ava2 s . c o m 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", "false"); dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true"); dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 0000-00-00null dbcpDs.addConnectionProperty("yearIsDateType", "false");// ??year?date? dbcpDs.addConnectionProperty("noDatetimeStringSync", "true");// ,??? if (StringUtils.isNotEmpty(encoding)) { if (StringUtils.equalsIgnoreCase(encoding, "utf8mb4")) { dbcpDs.addConnectionProperty("characterEncoding", "utf8"); dbcpDs.setConnectionInitSqls(Arrays.asList("set names utf8mb4")); } else { dbcpDs.addConnectionProperty("characterEncoding", encoding); } } dbcpDs.setValidationQuery("select 1"); } else { logger.error("ERROR ## Unknow database type"); } return dbcpDs; }