001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.pool.sql;
019
020import java.sql.Connection;
021import java.sql.SQLException;
022
023import net.sf.hajdbc.pool.AbstractPoolProvider;
024import net.sf.hajdbc.util.Resources;
025
026/**
027 * {@link Connection} object pool provider implementation.
028 * @author Paul Ferraro
029 */
030public class ConnectionPoolProvider extends AbstractPoolProvider<Connection, SQLException>
031{
032        private final ConnectionFactory factory;
033        
034        public ConnectionPoolProvider(ConnectionFactory factory)
035        {
036                super(Connection.class, SQLException.class);
037                
038                this.factory = factory;
039        }
040
041        @Override
042        public void close(Connection connection)
043        {
044                Resources.close(connection);
045        }
046
047        /**
048         * {@inheritDoc}
049         * @see net.sf.hajdbc.pool.PoolProvider#create()
050         */
051        @Override
052        public Connection create() throws SQLException
053        {
054                return this.factory.getConnection();
055        }
056
057        /**
058         * {@inheritDoc}
059         * @see net.sf.hajdbc.pool.PoolProvider#isValid(java.lang.Object)
060         */
061        @Override
062        public boolean isValid(Connection connection)
063        {
064                try
065                {
066                        return connection.isValid(0);
067                }
068                catch (SQLException e)
069                {
070                        return false;
071                }
072        }
073}