All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cq.database.pool.ConnectionPool Maven / Gradle / Ivy

The newest version!
package cq.database.pool;

import cq.database.exception.DBException;
import cq.database.helper.DBConfig;
import cq.database.helper.DBHelper;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;

public class ConnectionPool {
    private int min;
    private int max;
    private int refreshTime;
    private boolean isRelease;
    private DBConfig dbConfig;
    private Queue free = new ConcurrentLinkedQueue<>();
    private Set used = new HashSet<>();

    private ConnectionPool() {
    }

    public static ConnectionPool getInstance() {
        return Pool.pool;
    }

    public void init(ConnectionPoolConfig poolConfig, DBConfig dbConfig) {
        this.dbConfig = dbConfig;
        refreshTime = poolConfig.refreshTime();
        isRelease = poolConfig.isRelease();
        min = poolConfig.min() < 1 ? 1 : poolConfig.min();
        max = poolConfig.max();
        addHelper(min);
        refreshFreeConnection();
    }

    private void addHelper(int size) {
        for (int i = 0; i < size; i++) {
            DBHelper dbHelper = new DBHelper(dbConfig);
            free.offer(dbHelper);
        }
    }

    public DBHelper getDBHelper() throws SQLException {
        if (free.size() > 0) {
            DBHelper help = free.poll();
            if (help != null) {
                if (help.getConnection().isValid(5)) {
                    if (used.add(help)) {
                        return help;
                    } else {
                        free.offer(help);
                        return getDBHelper();
                    }
                } else {
                    help.close();
                    return getDBHelper();
                }
            } else {
                return getDBHelper();
            }
        } else if ((free.size() + used.size()) < max) {
            synchronized (this) {
                if (free.size() + used.size() + min <= max) {
                    addHelper(min);
                } else {
                    addHelper(max - free.size() - used.size());
                }
            }
            return getDBHelper();
        } else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return getDBHelper();
        }
    }

    private void refreshFreeConnection() {
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(refreshTime * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int size = free.size();
                int index = 0;
                while (index < size) {
                    DBHelper help = free.poll();
                    if (help != null) {
                        try {
                            if (help.getConnection().isValid(5)) {
                                if (isRelease && index > min * 2) {
                                    help.close();
                                } else {
                                    help.refresh();
                                    free.offer(help);
                                }
                            } else {
                                help.close();
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                    index++;
                }
            }
        }).start();
    }

    public void recovery(DBHelper helper) {
        used.remove(helper);
        if (helper != null) {
            try {
                if (helper.getConnection() != null && helper.getConnection().isValid(5)) {
                    free.offer(helper);
                } else {
                    helper.close();
                }
            } catch (SQLException e) {
                throw new DBException(e);
            }
        }
    }

    public void destroy() {
        for (DBHelper h : free) {
            h.close();
        }
        for (DBHelper h : used) {
            h.close();
        }
        free = null;
        used = null;
    }

    private static class Pool {
        private static ConnectionPool pool = new ConnectionPool();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy