Example usage for org.apache.commons.dbcp BasicDataSource setValidationQuery

List of usage examples for org.apache.commons.dbcp BasicDataSource setValidationQuery

Introduction

In this page you can find the example usage for org.apache.commons.dbcp BasicDataSource setValidationQuery.

Prototype

public synchronized void setValidationQuery(String validationQuery) 

Source Link

Document

Sets the #validationQuery .

Note: this method currently has no effect once the pool has been initialized.

Usage

From source file:net.certifi.audittablegen.HsqldbDMR.java

/**
 * Generate a Hsqldb DataSource from Properties
 *
 * @param props/*w  w  w . j a v a2  s  . c om*/
 * @return BasicDataSource as DataSource
 */
static DataSource getRunTimeDataSource(Properties props) {

    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUsername(props.getProperty("username"));
    dataSource.setPassword(props.getProperty("password"));
    dataSource.setUrl(props.getProperty("url"));
    dataSource.setMaxActive(10);
    dataSource.setMaxIdle(5);
    dataSource.setInitialSize(5);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");

    return dataSource;
}

From source file:com.wso2.raspberrypi.Util.java

private static BasicDataSource getBasicDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://rss1.stratoslive.wso2.com/rpi_azeez_org");
    ds.setUsername("rpi_FHdBOhR3");
    ds.setPassword("wso2");
    ds.setValidationQuery("SELECT 1");
    return ds;/* ww  w  .  j  a va2 s.c om*/
}

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 .  j a v a  2s. 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.pinterest.deployservice.db.DatabaseUtil.java

/**
 * Create a MySQL datasource./*w  ww .j ava2  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// w w w  .ja  va2  s .  co  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.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 ava  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:jp.classmethod.aws.brian.config.DataSourceConfiguration.java

@Bean
public DataSource dataSource() {
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    basicDataSource.setUrl(url);/*from   w w  w  . j  ava2s .  com*/
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);
    basicDataSource.setValidationQuery("SELECT 1");
    basicDataSource.setMaxActive(50);
    basicDataSource.setMaxIdle(10);
    basicDataSource.setMinIdle(5);
    return basicDataSource;
}

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 a  v 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;
}

From source file:com.googlecode.jdbcproc.daofactory.guice.SimpleModule.java

@Provides
@Singleton/* w w  w.jav a 2 s.c  om*/
DataSource provideDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/jdbcprocdb");
    dataSource.setUsername("jdbcproc");
    dataSource.setPassword("jdbcproc");
    dataSource.setDefaultAutoCommit(true);
    dataSource.setValidationQuery("call create_collections()");

    return dataSource;
}

From source file:$.DataSourceConf.java

@Bean
    public DataSource dataSource() {
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);//  w  w  w.j ava2 s  .  co  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;
    }