创新实训——数据库连接池的实现(二)

博客介绍了使用代理模式实现RealConnection和RealPreparedStatement。其中,RealConnection持有数据库连接Connection,管理连接负载和空闲时间;RealPreparedStatement持有PreparedStatement,通过接口注入回调新建和关闭事件。

3.使用代理模式实现RealConnection和RealPreparedStatement

  1.RealConnection持有一个数据库连接Connection,并且管理连接的负载和空闲时间。

public class RealConnection implements IStatementCallback {
    private Connection connection;
    private AtomicInteger statementCount=new AtomicInteger(0);
    private long idleStartNanoTime = Long.MAX_VALUE;

    public RealConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://...", "root", "passwd");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public RealPreparedStatement prepareStatement(String sql) {
        RealPreparedStatement realStatement = null;
        try {
            realStatement = new RealPreparedStatement(
                    connection.prepareStatement(sql), this);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return realStatement;
    }

    public int getCount() {
        return statementCount.get();
    }

    public long getIdleAtNanos() {
        if (idleStartNanoTime < 0)
            return idleStartNanoTime;
        return System.nanoTime() - idleStartNanoTime;
    }

    public void close() {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void newStatement() {
        statementCount.incrementAndGet();
        idleStartNanoTime = -1L;
    }

    @Override
    public void closeStatement() {
        if (statementCount.decrementAndGet() <= 0) {
            idleStartNanoTime = System.nanoTime();
        }
    }
}

  2.RealPreparedStatement持有一个PreparedStatement,并且使用接口注入的方式回调新建和关闭事件。

public class RealPreparedStatement implements AutoCloseable{
    private PreparedStatement statement;
    private IStatementCallback callback;

    public RealPreparedStatement(PreparedStatement statement, IStatementCallback callback) {
        this.statement = statement;
        this.callback = callback;
        callback.newStatement();
    }

    public void setInt(int parameterIndex, int x) throws SQLException {
        statement.setInt(parameterIndex, x);
    }

    public void setString(int parameterIndex, String x) throws SQLException {
        statement.setString(parameterIndex, x);
    }

    public int executeUpdate() throws SQLException {
        return statement.executeUpdate();
    }

    public ResultSet executeQuery() throws SQLException {
        return statement.executeQuery();
    }

    public void close() throws SQLException {
        if (statement != null) {
            statement.close();
            callback.closeStatement();
        }
    }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值