自定义连接池

1.实现步骤

/**
 * @author LJL
 * @version 1.0
 * @title ConnectionPoolConfiguration
 * @date 2023/6/9 18:47
 * @description 连接池的配置类
 */
public class ConnectionPoolConfiguration {

    private final String url;
    private final String username;
    private final String password;
    private final int maxConnections;


    // 其他属性
    public ConnectionPoolConfiguration(String url, String username, String password, int maxConnections) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.maxConnections = maxConnections;
    }

    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public int getMaxConnections() {
        return maxConnections;
    }
}
/**
 * @author LJL
 * @version 1.0
 * @title ConnectionPool
 * @date 2023/6/9 19:25
 * @description 连接池接口
 */
public interface ConnectionPool {

    void init(ConnectionPoolConfiguration connectionPoolConfiguration) throws SQLException;

    void destroy() throws SQLException;

    Connection getConnection() throws SQLException;

    void releaseConnection(Connection conn);

}
/**
 * @author LJL
 * @version 1.0
 * @title ConnectionPoolImpl
 * @date 2023/6/9 18:49
 * @description 连接池
 */
public class ConnectionPoolImpl implements ConnectionPool {

    private final ConnectionPoolConfiguration configuration;
    private LinkedList<Connection> connectionQueue = new LinkedList<>();
    private int currentConnections = 0;



    public ConnectionPoolImpl(ConnectionPoolConfiguration configuration){
        this.configuration = configuration;
    }

    private Connection createConnection() throws SQLException {
        currentConnections++;
        return DriverManager.getConnection(configuration.getUrl(), configuration.getUsername(),
                configuration.getPassword());
    }

    @Override
    public void init(ConnectionPoolConfiguration configuration) throws SQLException {
        for (int i = 0; i < configuration.getMaxConnections(); i++) {
            connectionQueue.offer(createConnection());
        }
    }

    @Override
    public void destroy() throws SQLException {
        SQLException exception = null;
        for (Connection conn : connectionQueue) {
            try {
                conn.close();
            } catch (SQLException e) {
                exception = e;
            }
        }

        connectionQueue.clear();
        currentConnections = 0;

        if (exception != null) {
            throw exception;
        }
    }


    public synchronized Connection getConnection() throws SQLException {
        if (connectionQueue.isEmpty()) {
            if (currentConnections < configuration.getMaxConnections()) {
                return createConnection();
            } else {
                try {
                    wait();
                } catch (InterruptedException e) {
                    // 中断线程
                    Thread.currentThread().interrupt();
                }
                return getConnection();
            }
        }

        Connection connection = connectionQueue.poll();
        if (connection != null) {
            notifyAll();
        }
        return connection;
    }


    public synchronized void releaseConnection(Connection connection) {
        if (connection != null) {
            connectionQueue.offer(connection);
            notifyAll();
        }
    }



}
public class Test {

    public static void main(String[] args) throws SQLException {
        ConnectionPoolConfiguration configuration = new ConnectionPoolConfiguration("jdbc:mysql://localhost:3306/mydatabase", "root", "password", 10);
        ConnectionPoolImpl connectionPoolImpl = new ConnectionPoolImpl(configuration);
        Connection connection = null;
        try  {
            connection = connectionPoolImpl.getConnection();
            // 使用数据库连接执行业务逻辑
        } catch (SQLException e) {
            // 处理异常情况
        } finally {
            connectionPoolImpl.releaseConnection(connection);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值