手搓一个数据库连接池

手搓一个数据库连接池

继承DataSource接口

/**
 * @author fate
 * @date 2022/11/3
 * @Description
 */
public interface FateDataSourceInterface extends DataSource {
    /**
     * 获得连接(必须实现)
     *
     * @return {@link Connection}
     * @throws SQLException sqlexception异常
     */
    @Override
    default Connection getConnection() throws SQLException{
        return null;
    }

    /**
     * 获得连接(必须实现)
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link Connection}
     * @throws SQLException sqlexception异常
     */
    @Override
    default Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    /**
     * 打开
     *
     * @param iface iface
     * @return {@link T}
     * @throws SQLException sqlexception异常
     */
    @Override
    default <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    /**
     * 是包装
     *
     * @param iface iface
     * @return boolean
     * @throws SQLException sqlexception异常
     */
    @Override
    default boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    /**
     * 获取日志
     *
     * @return {@link PrintWriter}
     * @throws SQLException sqlexception异常
     */
    @Override
    default PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    /**
     * 设置日志
     *
     * @param out 出
     * @throws SQLException sqlexception异常
     */
    @Override
    default void setLogWriter(PrintWriter out) throws SQLException {

    }

    /**
     * 设置登录超时
     *
     * @param seconds 秒
     * @throws SQLException sqlexception异常
     */
    @Override
    default void setLoginTimeout(int seconds) throws SQLException {
    }

    /**
     * 获得登录超时
     *
     * @return int
     * @throws SQLException sqlexception异常
     */
    @Override
    default int getLoginTimeout() throws SQLException {
        return  0;
    }

    /**
     * 得到父母记录器
     *
     * @return {@link Logger}
     * @throws SQLFeatureNotSupportedException sqlfeature不支持例外
     */
    @Override
    default Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}

编写一个抽象类注入数据库连接信息

public abstract class AbstractFateDataSource implements FateDataSourceInterface {

//    数据库基本信息

    private String url;

    private String username;

    private String password;

    private String driver;




    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * 获得连接(必须实现)
     *
     * @return {@link Connection}
     * @throws SQLException sqlexception异常
     */
    @Override
    public Connection getConnection() throws SQLException {
        return getConnection(username, password);
    }

    /**
     * 获得连接(必须实现)
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link Connection}
     * @throws SQLException sqlexception异常
     */
    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return doConnect(username, password);
    }

    /**
     * 获取连接
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link Connection}
     */
    private Connection doConnect(String username, String password) throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }
}

采用动态代理实现对数据库连接的代理

public class ConnectionProxy implements InvocationHandler {

    /**
     * 真正连接
     */
    private Connection realConnection;

    /**
     * 代理连接
     */
    private Connection proxyConnection;

    /**
     * 数据源
     */
    private FateDataSource dataSource;


    public ConnectionProxy(Connection realConnection, FateDataSource dataSource) {
        this.realConnection = realConnection;
        this.dataSource = dataSource;
        this.proxyConnection = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(),new Class<?>[]{Connection.class},this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        获取方法名字
        String methodName = method.getName();
        if ("close".equals(methodName)){
//           链接归还连接池
            dataSource.closeConnection(this);
            return null;
        }else {
            return method.invoke(realConnection, args);
        }

    }

    public Connection getRealConnection() {
        return realConnection;
    }

    public void setRealConnection(Connection realConnection) {
        this.realConnection = realConnection;
    }

    public Connection getProxyConnection() {
        return proxyConnection;
    }

    public void setProxyConnection(Connection proxyConnection) {
        this.proxyConnection = proxyConnection;
    }

    public FateDataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(FateDataSource dataSource) {
        this.dataSource = dataSource;
    }

}

编写抽象类实现类

public class FateDataSource extends AbstractFateDataSource{
    /**
     * 空闲连接
     */
    private final List<ConnectionProxy> idleConnections = new ArrayList<>();
    /**
     * 活跃连接
     */
    private final List<ConnectionProxy> activeConnections = new ArrayList<>();
    /**
     * 最大活跃连接池
     */
    private  int poolMaxActiveConnections=10;
    /**
     * 最大空闲连接池
     */
    private  int poolMaxIdleConnections=5;
    /**
     * 连接池等待时间
     */
    private  long poolMaxToWait= 30000L;
    private final Object monitor = new Object();

    public FateDataSource() {
    }

    public FateDataSource(int poolMaxActiveConnections, int poolMaxIdleConnections, long poolMaxToWait) {
        this.poolMaxActiveConnections = poolMaxActiveConnections;
        this.poolMaxIdleConnections = poolMaxIdleConnections;
        this.poolMaxToWait = poolMaxToWait;
    }

    /**
     * 获得连接(必须实现)
     *
     * @return {@link Connection}
     */
    @Override
    public Connection getConnection() throws SQLException {
        ConnectionProxy connectionProxy = getConnectionProxy(super.getUsername(),super.getPassword());
        return connectionProxy.getProxyConnection();
    }

    private ConnectionProxy getConnectionProxy(String username, String password) throws SQLException {
        Boolean wait = false;
        ConnectionProxy connectionProxy=null;
        while (connectionProxy==null){
//            线程同步
            synchronized (monitor){
                if (!idleConnections.isEmpty()){
//                    空闲链接直接使用
                    connectionProxy=idleConnections.remove(0);
                }else {
//                    未达到最大链接数,直接新建链接
                    if (activeConnections.size()<poolMaxActiveConnections){
                        connectionProxy = new ConnectionProxy(super.getConnection(username, password), this);
                    }
//                        反之等待
                }
            }
            if (!wait){
                wait = true;
            }
            if (connectionProxy==null){
                try {
//                   空链接需要等待
                    monitor.wait(poolMaxToWait);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
        if (connectionProxy!=null){
            activeConnections.add(connectionProxy);
        }
        return connectionProxy;
    }


    public void closeConnection(ConnectionProxy connectionProxy){
        synchronized (monitor){
//            逻辑关闭实则只是放在另外一个list中
            activeConnections.remove(connectionProxy);
            if (idleConnections.size()<poolMaxIdleConnections){
                idleConnections.add(connectionProxy);
            }
//            通知等待获取链接的线程
            monitor.notify();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Java数据库连接池实现,此实现仅用于学习和演示目的。请注意,这不是一个完整的、生产就绪的连接池实现,因为它缺少许多功能,例如:连接泄漏检测、空闲连接检测、最大连接数限制等。 首先,我们需要创建一个数据库连接池类。以下是一个简单的实现: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class ConnectionPool { private String jdbcUrl; private String username; private String password; private List<Connection> connectionPool; private List<Connection> usedConnections = new ArrayList<>(); private final int INITIAL_POOL_SIZE = 10; public ConnectionPool(String jdbcUrl, String username, String password) { this.jdbcUrl = jdbcUrl; this.username = username; this.password = password; connectionPool = new ArrayList<>(INITIAL_POOL_SIZE); for (int i = 0; i < INITIAL_POOL_SIZE; i++) { try { Connection connection = DriverManager.getConnection(jdbcUrl, username, password); connectionPool.add(connection); } catch (SQLException e) { System.err.println(e.getMessage()); } } } public synchronized Connection getConnection() { Connection connection = null; if (connectionPool.size() > 0) { connection = connectionPool.remove(connectionPool.size() - 1); usedConnections.add(connection); } else { System.err.println("No more connections available."); } return connection; } public boolean releaseConnection(Connection connection) { boolean released = usedConnections.remove(connection); if (released) { connectionPool.add(connection); } return released; } public int getSize() { return connectionPool.size() + usedConnections.size(); } public void shutdown() throws SQLException { usedConnections.forEach(this::releaseConnection); for (Connection c : connectionPool) { c.close(); } connectionPool.clear(); } } ``` 接下来,我们来解释一下上面的代码: - `jdbcUrl`、`username` 和 `password` 存储了数据库连接的信息。 - `connectionPool` 存储了可用的连接,`usedConnections` 存储了当前正在使用的连接。 - `INITIAL_POOL_SIZE` 是连接池的默认大小,可以根据需要进行修改。 - 在连接池的构造函数中,我们创建了一定数量的连接,并将它们存储在连接池中。 - `getConnection()` 方法从连接池中获取一个可用的连接,如果连接池为空,则返回 `null`。 - `releaseConnection()` 方法将一个连接释放回连接池中。 - `getSize()` 方法返回连接池的大小。 - `shutdown()` 方法关闭连接池中的所有连接,并清空连接池和正在使用的连接列表。 接下来,我们可以编写一个测试类来测试连接池的实现: ```java import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class TestConnectionPool { public static void main(String[] args) { String jdbcUrl = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "password"; ConnectionPool connectionPool = new ConnectionPool(jdbcUrl, username, password); // Get a connection from the pool Connection connection = connectionPool.getConnection(); try { PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM users"); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString("username")); } } catch (SQLException e) { System.err.println(e.getMessage()); } // Release the connection back to the pool connectionPool.releaseConnection(connection); // Shutdown the connection pool try { connectionPool.shutdown(); } catch (SQLException e) { System.err.println(e.getMessage()); } } } ``` 在上面的测试类中,我们首先创建了一个连接池对象,并从中获取一个连接。然后,我们使用该连接执行一个查询,并将结果打印到控制台。最后,我们将连接释放回连接池中,并关闭连接池。 希望这可以帮助您开始编写自己的数据库连接池实现!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值