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:com.alibaba.druid.benckmark.pool.CaseKylin_Oracle.java

public void dbcp() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);//from   ww w .  j ava  2  s .  c  o m
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMinIdle(minIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(oracleDriverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setTestOnBorrow(testWhileIdle);
    dataSource.setTestOnBorrow(testOnReturn);
    dataSource.setRemoveAbandoned(removeAbandoned);
    dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    dataSource.setValidationQuery(validateQuery);

    for (int i = 0; i < TEST_COUNT; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}

From source file:com.alibaba.druid.benckmark.pool.Case3.java

public void dbcp() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);//from  w w  w  .j a v a2s . co  m
    dataSource.setMaxIdle(maxIdle);
    dataSource.setMaxWait(maxWait);
    dataSource.setMinIdle(minIdle);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery(validationQuery);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setConnectionProperties(connectionProperties);
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    for (int i = 0; i < TEST_COUNT; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    // dataSource.close();
    System.out.println();
}

From source file:com.alibaba.druid.benckmark.pool.Case1.java

public void test_dbcp() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);/*from ww w  .  j  a  v  a 2  s. c  o  m*/
    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("SELECT 1");
    dataSource.setTestOnBorrow(false);

    for (int i = 0; i < loopCount; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}

From source file:blueprint.sdk.experimental.florist.db.ConnectionListener.java

public void contextInitialized(final ServletContextEvent arg0) {
    Properties poolProp = new Properties();
    try {//from   www  .  ja va2  s  .c o m
        Context initContext = new InitialContext();

        // load pool properties file (from class path)
        poolProp.load(ConnectionListener.class.getResourceAsStream("/jdbc_pools.properties"));
        StringTokenizer stk = new StringTokenizer(poolProp.getProperty("PROP_LIST"));

        // process all properties files list in pool properties (from class
        // path)
        while (stk.hasMoreTokens()) {
            try {
                String propName = stk.nextToken();
                LOGGER.info(this, "loading jdbc properties - " + propName);

                Properties prop = new Properties();
                prop.load(ConnectionListener.class.getResourceAsStream(propName));

                DataSource dsr;
                if (prop.containsKey("JNDI_NAME")) {
                    // lookup DataSource from JNDI
                    // FIXME JNDI support is not tested yet. SPI or Factory
                    // is needed here.
                    LOGGER.warn(this, "JNDI DataSource support needs more hands on!");
                    dsr = (DataSource) initContext.lookup(prop.getProperty("JNDI_NAME"));
                } else {
                    // create new BasicDataSource
                    BasicDataSource bds = new BasicDataSource();
                    bds.setMaxActive(Integer.parseInt(prop.getProperty("MAX_ACTIVE")));
                    bds.setMaxIdle(Integer.parseInt(prop.getProperty("MAX_IDLE")));
                    bds.setMaxWait(Integer.parseInt(prop.getProperty("MAX_WAIT")));
                    bds.setInitialSize(Integer.parseInt(prop.getProperty("INITIAL")));
                    bds.setDriverClassName(prop.getProperty("CLASS_NAME"));
                    bds.setUrl(prop.getProperty("URL"));
                    bds.setUsername(prop.getProperty("USER"));
                    bds.setPassword(prop.getProperty("PASSWORD"));
                    bds.setValidationQuery(prop.getProperty("VALIDATION"));
                    dsr = bds;
                }
                boundDsrs.put(prop.getProperty("POOL_NAME"), dsr);
                initContext.bind(prop.getProperty("POOL_NAME"), dsr);
            } catch (RuntimeException e) {
                LOGGER.trace(e);
            }
        }
    } catch (IOException | NamingException e) {
        LOGGER.trace(e);
    }
}

From source file:fr.cnes.sitools.datasource.jdbc.business.SitoolsSQLDataSourceFactory.java

/**
 * Local creation of a DataSource/*from w w w.  jav  a 2  s . c  o  m*/
 * 
 * @param driver
 *          the database driver
 * @param connectURI
 *          the URI to connect
 * @param userName
 *          the database user name
 * @param password
 *          the password
 * @param schemaOnConnection
 *          the schema on connection
 * @return SitoolsDataSource a standard data source for SITools
 */
public SitoolsSQLDataSource setupDataSource(String driver, String connectURI, String userName, String password,
        String schemaOnConnection) {

    String key = connectURI + "@" + userName;
    SitoolsSQLDataSource foundDatasource = dataSources.get(key);
    if (foundDatasource == null) {

        BasicDataSource ds = new BasicDataSource();
        // OSGi
        ds.setDriverClassLoader(getClass().getClassLoader());
        ds.setDriverClassName(driver);
        ds.setUsername(userName);
        ds.setPassword(password);
        ds.setUrl(connectURI);
        ds.setMaxActive(10);
        ds.setInitialSize(1);
        // test that the connection is alive on each request. If not It will be dropped from the pool and another
        // connection will be created
        if (!"org.hsqldb.jdbcDriver".equals(driver)) {
            ds.setTestOnBorrow(true);
            ds.setValidationQuery("SELECT 1");
        }
        // ds.setDefaultReadOnly(false);
        // ds.setDefaultAutoCommit(true);
        JDBCDataSource jdbcDS = new JDBCDataSource();
        jdbcDS.setName(key);
        jdbcDS.setDriverClass(driver);
        foundDatasource = new SitoolsSQLDataSource(jdbcDS, ds, schemaOnConnection);
        dataSources.put(key, foundDatasource);
    }
    return foundDatasource;
}

From source file:com.alibaba.druid.benckmark.pool.Case4.java

public void test_dbcp() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);/*from  ww w  .  j a  v a2  s  .  c  om*/
    dataSource.setMinIdle(minPoolSize);
    dataSource.setMaxIdle(maxPoolSize);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setDriverClassName(driverClass);
    dataSource.setUrl(jdbcUrl);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    dataSource.setValidationQuery("SELECT 1");
    dataSource.setTestOnBorrow(false);

    for (int i = 0; i < loopCount; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
}

From source file:com.alibaba.druid.benckmark.pool.PoolPerformanceTest.java

@Test
public void test_dbcp() throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setInitialSize(initialSize);
    dataSource.setMaxActive(maxActive);//w  w w . ja  va2 s  .  c  o  m
    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("SELECT 1");
    dataSource.setTestOnBorrow(false);
    System.out.println(dataSource.getClass().getSimpleName());
    for (int i = 0; i < loopCount; ++i) {
        p0(dataSource, "dbcp", threadCount);
    }
    System.out.println();
    dataSource.close();
    TestDriver.instance.reset();
}

From source file:fr.cnes.sitools.datasource.jdbc.business.SitoolsSQLDataSourceFactory.java

/**
 * Setup a dataSource for "users". Usage is for all users for consultation functions.
 * //from   ww  w  .  j a  v a 2s.  c o m
 * @param dataSource
 *          the DataSource to update
 * @return SitoolsDataSource the new DataSource
 */
public SitoolsSQLDataSource setupDataSourceForUsers(JDBCDataSource dataSource) {
    String key = dataSource.getId();
    SitoolsSQLDataSource foundDatasource = dataSources.get(key);
    if (foundDatasource == null) {

        BasicDataSource ds = new BasicDataSource();
        // OSGi
        ds.setDriverClassLoader(getClass().getClassLoader());
        ds.setDriverClassName(dataSource.getDriverClass());
        ds.setUsername(dataSource.getUserLogin());
        ds.setPassword(dataSource.getUserPassword());
        ds.setUrl(dataSource.getUrl());
        ds.setMaxActive(dataSource.getMaxActive());
        ds.setInitialSize(dataSource.getInitialSize());
        ds.setDefaultReadOnly(true);
        // test that the connection is alive on each request. If not It will be dropped from the pool and another
        // connection will be created
        ds.setTestOnBorrow(true);
        ds.setValidationQuery("SELECT 1");
        if ((dataSource.getSchemaOnConnection() != null) && !dataSource.getSchemaOnConnection().equals("")) {
            ds.setDefaultCatalog(dataSource.getSchemaOnConnection());
        }
        foundDatasource = new SitoolsSQLDataSource(dataSource, ds, dataSource.getSchemaOnConnection());
        dataSources.put(key, foundDatasource);
    }
    return foundDatasource;
}

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