【事务】连接池

一通过addBack()返回conn

【事务】连接池_第1张图片【事务】连接池_第2张图片
代码:
1.DataSource

public class MyDataSource implements DataSource {
    List list = new ArrayList();
    public MyDataSource(){
        for (int i=0;i<10;i++){
            Connection connection = JDBCUtils.getConn();
            list.add(connection);
        }
    }
    public Connection getConnection() throws SQLException {
        if (list.size()==0){
            for (int i=0;i<5;i++){
                Connection connection = JDBCUtils.getConn();
                list.add(connection);
            }
        }
        Connection conn =list.remove(0);
        return conn;
    }
    //用完之后的归还
    public void addBack(Connection conn){
        list.add(conn);
    }

2.Test

   @Test
    public void polltest(){
        MyDataSource mds = new MyDataSource();
        Connection conn = null;
        PreparedStatement pst = null;
        try {
            conn = mds.getConnection();
            String sql ="insert into transaction1(money) value(41)";
            pst=conn.prepareStatement(sql);
            if (pst.executeUpdate()!=0)
                System.out.println("succeed");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closePst(pst);
            mds.addBack(conn);
        }
    }

二 装饰者模式 extends Connection

【事务】连接池_第3张图片
代码:
1.DataSource

public class WraperDataSource implements DataSource {
    List list = new ArrayList();
    public WraperDataSource(){
        for (int i=0;i<10;i++){
            Connection connection = JDBCUtils.getConn();
            list.add(connection);
        }
    }
    public Connection getConnection() throws SQLException {
        if (list.size()==0){
            for (int i=0;i<5;i++){
                Connection connection = JDBCUtils.getConn();
                list.add(connection);
            }
        }
        Connection conn =list.remove(0);
        // 对象抛出的时候进行包装
        Connection connection =new ConnectionWrap(conn,list);
        return connection;
    }

2.一个类继承Connection 重写colse() preparedStatement()

public class ConnectionWrap implements Connection {
    Connection connection = null;
    List list;
    public ConnectionWrap(Connection connection,List list) {
        super();
        this.connection = connection;
        this.list= list;
    }

    public void close() throws SQLException {
        System.out.println("归还之前的List:"+list.size());
        list.add(connection);
        System.out.println("归还之后的List:"+list.size());
    }
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        return connection.prepareStatement(sql);
    }

3.Test

 @Test
    public void polwrap() throws SQLException {
        WraperDataSource wraperDS = new WraperDataSource();
        Connection conn = null;
        PreparedStatement pst = null;
        try {
            conn = wraperDS.getConnection();
            String sql ="insert into transaction1(money) value(777)";
            pst=conn.prepareStatement(sql);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(pst,conn);
        }
    }

【事务】连接池_第4张图片

你可能感兴趣的:(事务)