com.tera.common.database.dbcp.CPoolableConnectionFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.tera.common.database.dbcp.CPoolableConnectionFactory.java

Source

/**
 * This file is part of tera-api.
 * 
 * tera-api is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * tera-api is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with tera-api.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.tera.common.database.dbcp;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.ObjectPool;

/**
 * @author ATracer
 */
public class CPoolableConnectionFactory extends PoolableConnectionFactory {

    private final int validationTimeout;

    /**
     * @param connFactory
     * @param pool
     * @param stmtPoolFactory
     * @param validationTimeout
     * @param defaultReadOnly
     * @param defaultAutoCommit
     */
    public CPoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool,
            KeyedObjectPoolFactory stmtPoolFactory, int validationTimeout, boolean defaultReadOnly,
            boolean defaultAutoCommit) {
        super(connFactory, pool, stmtPoolFactory, null, defaultReadOnly, defaultAutoCommit);
        this.validationTimeout = validationTimeout;
    }

    @Override
    public void validateConnection(Connection conn) throws SQLException {
        if (conn.isClosed())
            throw new SQLException("validateConnection: connection closed");
        if (validationTimeout >= 0 && !conn.isValid(validationTimeout))
            throw new SQLException("validateConnection: connection invalid");
    }
}