package com.nms.server.util;

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

import org.apache.log4j.Logger;

import com.nms.server.common.Constants;

public class DbPool {
	private static Logger logger = Logger.getLogger(DbPool.class);
	public static void initPool() throws ClassNotFoundException ,SQLException ,Exception{
		logger.debug("连接池初始化开始");
		if("bonecp".equalsIgnoreCase(Constants.DB_POOL_TYPE)){
			BoneCPPool.initPool();
		}else if("druid".equalsIgnoreCase(Constants.DB_POOL_TYPE)){
			DruidPool.initPool();
		}else {
			logger.error("Please configure the connection pool type correctly: db.pool.type");
		}
		logger.debug("连接池初始化结束");
	}
	
	public static Connection getConnection() throws SQLException{
		Connection connection = null;
		if("bonecp".equalsIgnoreCase(Constants.DB_POOL_TYPE)){
			connection = BoneCPPool.getConnection();
		}else if("druid".equalsIgnoreCase(Constants.DB_POOL_TYPE)){
			connection = DruidPool.getConnection();
		}else {
			logger.error("Please configure the connection pool type correctly: db.pool.type");
		}
		return connection;
	}
	
	
	public static void shutdown(){
		Connection connection = null;
		if("bonecp".equalsIgnoreCase(Constants.DB_POOL_TYPE)){
			BoneCPPool.shutdown();
		}else if("druid".equalsIgnoreCase(Constants.DB_POOL_TYPE)){
			DruidPool.shutdown();
		}else {
			logger.error("Please configure the connection pool type correctly: db.pool.type");
		}
	}
}
