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

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

Introduction

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

Prototype

public void setTestOnReturn(boolean testOnReturn) 

Source Link

Document

When true, objects will be {*link PoolableObjectFactory#validateObject validated} before being returned to the pool within the {*link #returnObject}.

Usage

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

/**
 * Initializes this DAOFactory./* w  w  w  .j  a va 2 s. c  o 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);
    }
}