///*
// * Copyright 2002-2018 Jalal Kiswani. 
// * E-mail: Kiswani.Jalal@Gmail.com
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package com.jk.db.datasource;
//
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.sql.SQLException;
//
//import org.apache.commons.dbcp2.BasicDataSource;
//
//import com.jk.db.dataaccess.core.JKDbConstants;
//import com.jk.util.JK;
//import com.jk.util.JKIOUtil;
//import com.jk.util.config.JKConfig;
//
//// TODO: Auto-generated Javadoc
///**
// * The Class JKPoolingDataSource.
// *
// * @author Jalal Kiswani
// */
//public class JKPoolingDataSource extends JKDataSourceImpl {
//
//	/** The datasource. */
//	private BasicDataSource datasource;
//
//	/**
//	 * Instantiates a new JK pooling data source.
//	 *
//	 * @param prop the prop
//	 */
//	public JKPoolingDataSource(final JKConfig prop) {
//		super(prop);
//	}
//
//	/**
//	 * Instantiates a new JK pooling data source.
//	 *
//	 * @param configFileName the config file name
//	 */
//	public JKPoolingDataSource(String configFileName) {
//		this(JKIOUtil.readConfigFile(configFileName));
//	}
//
//	/*
//	 * (non-Javadoc)
//	 *
//	 * @see com.jk.db.datasource.JKAbstractDataSource#connect()
//	 */
//	@Override
//	protected Connection connect() throws SQLException {
//		return this.datasource.getConnection();
//	}
//
//	/**
//	 * Gets the initial pool size.
//	 *
//	 * @return the initial pool size
//	 */
//	private int getInitialPoolSize() {
//		return Integer.parseInt(getProperty(JKDbConstants.PROPERTY_INITIAL_POOL_SIZE_KEY, JKDbConstants.DEFAULT_POOL_INITIAL_SIZE));
//	}
//
//	/**
//	 * Gets the max pool size.
//	 *
//	 * @return the max pool size
//	 */
//	private int getMaxPoolSize() {
//		return Integer.parseInt(getProperty(JKDbConstants.PROPERTY_MAX_POOL_SIZE_KEY, JKDbConstants.DEFAULT_POOL_MAX_SIZE));
//	}
//
////	/*
////	 * (non-Javadoc)
////	 *
////	 * @see com.jk.db.datasource.JKAbstractDataSource#getQueryConnection()
////	 */
////	@Override
////	public Connection getQueryConnection() throws JKDataAccessException {
////		Connection queryConnection = super.getQueryConnection();
////		try {
////			testConnection(queryConnection);
////			this.queryConection
////		} catch (final SQLException e) {
////			queryConnection = null;
////			throw new JKDataAccessException("DATABASE_DOWN_ERROR", e);
////		}
////		return queryConnection;
////	}
//
//	/*
//	 * (non-Javadoc)
//	 *
//	 * @see com.jk.db.datasource.JKDefaultDataSource#init()
//	 */
//	@Override
//	protected void init() {
//		super.init();
//		this.logger.debug("Constructing pool with settings ({})", super.getProperties().toProperties());
//		this.datasource = new BasicDataSource();
//		this.datasource.setDriverClassName(super.getDriverName());
//		this.datasource.setUrl(getDatabaseUrl());
//		this.datasource.setUsername(getUsername());
//		this.datasource.setPassword(getPassword());
//		this.datasource.setInitialSize(getInitialPoolSize());
//		this.datasource.setMaxTotal(getMaxPoolSize());
////		this.datasource.setValidationQuery("SELECT 1");
////		this.datasource.setTestOnBorrow(true);
//		// config specific to mysql
//		this.datasource.addConnectionProperty("allowMultiQueries", "true");
//		this.datasource.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");
//	}
//
//	/**
//	 * Test connection.
//	 *
//	 * @param queryConnection the query connection
//	 * @throws SQLException the SQL exception
//	 */
//	public void testConnection(final Connection queryConnection) throws SQLException {
//		PreparedStatement ps = null;
//		try {
//			ps = queryConnection.prepareStatement(getTestQuery());
//			ps.executeQuery();
//		} finally {
//			close(ps);
//		}
//	}
//
//	/*
//	 * (non-Javadoc)
//	 * 
//	 * @see com.jk.db.datasource.JKDefaultDataSource#close()
//	 */
//	@Override
//	public void close() {
//		try {
//			close(getQueryConnection());
//			datasource.close();
//		} catch (SQLException e) {
//			JK.throww(e);
//		}
//	}
//}
