/**
 *
 */
package com.cnd3b.common.data.db;

import com.cnd3b.common.BaseClass;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Properties;

/**
 * @author SJW
 *
 */
public class DBConnectPool extends BaseClass {
    public static HashMap<String, Properties> propertiesMap = new HashMap<String, Properties>();
    /**
     * 数据库连接池
     */
    public static final HashMap<String, LinkedList<Connection>> POOLS_MAP = new HashMap<String, LinkedList<Connection>>();

    public DBConnectPool() {

    }

    public Connection getConnect(String datakey) {
        synchronized (DBConnectPool.class) {
            Connection connection = null;
            int conncount = 0;
            if (POOLS_MAP.containsKey(datakey) && (conncount = POOLS_MAP.get(datakey).size()) > 0) {
                printInfoOut("数据库连接", "从" + datakey + "数据库连接池获取一个连接,当前剩余连接数为" + (conncount - 1));
                connection = POOLS_MAP.get(datakey).remove(0);
            }
            try {
                if (connection == null || connection.isClosed()) {
                    connection = buildConnect(datakey);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return connection;
        }
    }

    private Connection buildConnect(String datakey) {
        synchronized (DBConnectPool.class) {
            Connection conn = null;
            printInfoOut("数据库连接", "创建新的" + datakey + "链接" + getDateTime_Str());
            if (!propertiesMap.containsKey(datakey)) {
                SQLiteJDBC jdbc = new SQLiteJDBC();
                Properties prop = jdbc.getDataSource(datakey);
                propertiesMap.put(datakey, prop);
            }
            Properties p = propertiesMap.get(datakey);
            try {
                Class.forName(p.getProperty("driver"));
                conn = DriverManager.getConnection(p.getProperty("url"), p.getProperty("username"),
                        p.getProperty("password"));
                conn.setAutoCommit(false);
            } catch (Exception e) {
                e.printStackTrace();
                printErrOut("数据库连接", "创建新的" + datakey + "链接失败" + getDateTime_Str() + e.getMessage());
            }
            return conn;
        }
    }

    public void close(String datakey, Connection conn) {
        synchronized (DBConnectPool.class) {
            boolean isclose = true;
            try {
                isclose = conn.isClosed();
            } catch (SQLException e1) {
                e1.printStackTrace();
                printErrOut("数据库连接", "DBConnectPool 120" + e1.getMessage());
            }
            if (!isclose) {
                if (!POOLS_MAP.containsKey(datakey)) {
                    LinkedList<Connection> list = new LinkedList<Connection>();
                    POOLS_MAP.put(datakey, list);
                }
                if (POOLS_MAP.get(datakey).size() <= 20) {
                    POOLS_MAP.get(datakey).add(conn);
                } else {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        printErrOut("数据库连接", datakey + "连接关闭失败" + getDateTime_Str() + e.getMessage());
                    } finally {
                        conn = null;
                    }
                }
            } else {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    printErrOut("数据库连接", datakey + "连接关闭失败" + getDateTime_Str() + e.getMessage());
                } finally {
                    conn = null;
                }
            }
        }
    }
}
