package com.sino.publicclass.database;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;

import com.sino.publicclass.base.CException;

public class ConnectionPool {
	private static Log				log;
	private static String			configFile	= "";				// ÅäÖÃÎÄ¼þÂ·¾¶Ãû
	private static String			driver		= "";
	private static String			url			= "";
	private static String			usrID		= "";
	private static String			password	= "";
	private static boolean			usePool		= false;
	private static ConnectionPool	instance	= null;
	private static Integer			lock		= new Integer(1);

	public static void startup() throws CException {
		getInstance();
	}
  
	public static ConnectionPool getInstance() throws CException {
		if (instance == null) {
			synchronized (lock) {
				if (instance == null) {
					instance = new ConnectionPool();
					instance.init();
				}
			}
		}
		return instance;
	}

	public static Connection getConnection() throws CException {
		return getConnection("proxool.property-default");
	}

	public static Connection getConnectionStock() throws CException {
		return getConnection("proxool.property-default_stock");
	}

	public static Connection getConnection_comm() throws CException {
		return getConnection("proxool.property-comm");
	}

	public static Connection getConnectionGPS() throws CException {
		return getConnection("proxool.property-gps");
	}
    
	public static Connection getConnection(String alias) throws CException {
		try {
			getInstance();
			Connection dbCon = DriverManager.getConnection(alias);
			int i = 0;
			while (dbCon.isClosed()) {
				if (i > 100) {
					throw new Exception("can't get commection");
				}
				killConnection(dbCon);
				dbCon = DriverManager.getConnection(alias);
				i++;
			}
			return dbCon;
		} catch (Exception e) {
			log.error("", e);
			throw new CException("getConnection() error");
		}
	}

	private static void killConnection(Connection dbCon) throws CException {
		try {
			ProxoolFacade.killConnecton(dbCon, false);
		} catch (Exception e) {
			log.error("", e);
			throw new CException("killAllConnections_comm() error");
		}
	}

	public static Connection getConnection104() throws CException {
		try {
			getInstance();
			return DriverManager.getConnection("proxool.property-default104");
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new CException("getConnection() error");
		}
	}

	void init() throws CException {
		try {
	
			log = LogFactory.getLog(getClass().getName());
			log.info("start init connectionPool!!");
			configFile = "/dbconfig.properties";
			InputStream is = null;
			is = getClass().getResourceAsStream(configFile);
			Properties dbProps = null;
			dbProps = new Properties();
			dbProps.load(is);
			PropertyConfigurator.configure(dbProps);
		} catch (Exception e) {
			log.error("ConnectionPool init error", e);
			throw new CException("getConnection() init error" + e.getMessage());
		}
	}
}
