Example usage for org.apache.commons.dbcp.datasources SharedPoolDataSource setTestWhileIdle

List of usage examples for org.apache.commons.dbcp.datasources SharedPoolDataSource setTestWhileIdle

Introduction

In this page you can find the example usage for org.apache.commons.dbcp.datasources SharedPoolDataSource setTestWhileIdle.

Prototype

public void setTestWhileIdle(boolean testWhileIdle) 

Source Link

Document

When true, objects will be {*link PoolableObjectFactory#validateObject validated} by the idle object evictor (if any).

Usage

From source file:com.stehno.sanctuary.core.Sanctuary.java

private static DataSource createDataSource() throws ClassNotFoundException {
    SharedPoolDataSource poolDataSource = new SharedPoolDataSource();

    DriverAdapterCPDS cpds = new DriverAdapterCPDS();
    cpds.setDriver("org.h2.Driver");
    cpds.setUrl("jdbc:h2:/home/cjstehno/h2/sanctuary");
    cpds.setUser("sa");
    cpds.setPassword("");

    poolDataSource.setConnectionPoolDataSource(cpds);
    poolDataSource.setMaxActive(10);//from   www. jav  a  2s .  c  o  m
    poolDataSource.setMaxIdle(5);
    poolDataSource.setMinEvictableIdleTimeMillis(1000);
    poolDataSource.setTestWhileIdle(true);
    poolDataSource.setValidationQuery("select 1");

    return poolDataSource;
}

From source file:hk.hku.cecid.piazza.commons.dao.ds.SimpleDSDAOFactory.java

/**
 * Initializes this DAOFactory.//from  w w w.j  a  va 2s .co m
 */
public void initFactory() throws DAOException {
    try {
        String driver = null;
        String url = null;
        String username = null;
        String password = null;

        boolean isPooling = true;
        int maxIdle = 0;
        int maxActive = 10;
        int maxWait = 50;

        boolean testOnBorrow = false;
        boolean testOnReturn = false;
        boolean testWhileIdle = false;
        String validationQuery = null;

        try {
            driver = getParameter("driver");
            url = getParameter("url");
            username = getParameter("username", null);
            password = getParameter("password", null);

            maxIdle = StringUtilities.parseInt(getParameter("maxIdle", null), 0);
            maxActive = StringUtilities.parseInt(getParameter("maxActive", null), 0);
            maxWait = StringUtilities.parseInt(getParameter("maxWait", null), -1);

            validationQuery = StringUtilities.trim(getParameter("validationQuery", null));
            if (validationQuery != null) {
                testOnBorrow = StringUtilities.parseBoolean(getParameter("testOnBorrow", "false"));
                testOnReturn = StringUtilities.parseBoolean(getParameter("testOnReturn", "false"));
                testWhileIdle = StringUtilities.parseBoolean(getParameter("testWhileIdle", "false"));
            }

        } catch (Exception e) {
            throw new DAOException("Invalid parameter for SimpleDSDAOFactory.");
        }

        if (getParameter("pooling", null) != null) {
            if (!getParameter("pooling").equalsIgnoreCase("true")
                    && !getParameter("pooling").equalsIgnoreCase("false")) {
                throw new DAOException("Invalid parameter for SimpleDSDAOFactory.");
            }
            isPooling = StringUtilities.parseBoolean(getParameter("pooling", "true"));
        }

        DataSource datasource;

        if (isPooling) {
            DriverAdapterCPDS cpds = new DriverAdapterCPDS();
            cpds.setDriver(driver);
            cpds.setUrl(url);
            cpds.setUser(username);
            cpds.setPassword(password);

            SharedPoolDataSource sds = new SharedPoolDataSource();
            sds.setConnectionPoolDataSource(cpds);
            sds.setMaxIdle(maxIdle);
            sds.setMaxActive(maxActive);
            sds.setMaxWait(maxWait);

            sds.setTestOnBorrow(testOnBorrow);
            sds.setTestOnReturn(testOnReturn);
            sds.setTestWhileIdle(testWhileIdle);
            sds.setValidationQuery(validationQuery);

            datasource = sds;
        } else {
            datasource = new SimpleDataSource(driver, url, username, password);
        }

        setDataSource(datasource);
    } catch (Exception e) {
        throw new DAOException("Cannot initialize SimpleDSDAOFactory!", e);
    }
}