Appuim环境搭建 数据结构 OpenCV4 audio cookies autocomplete routes stream jqgrid rss 安卓项目实战 kafka默认端口 etc文件夹 oracle自增长 ajax里面可以嵌套ajax吗 不用u盘装双系统 matlab区分大小写吗 vue与html5 安装mysql mysql删除表 python自学教材 python正则表达式语法 数据结构java版 java的运行环境 java单继承 java生成文件 java多线程编程 linux用户管理 垃圾邮件数据集 hadoop权威指南 圆形截图 ps怎么插入表格 一件换肤 电子书制作软件 getelementbyid 七宗罪游戏下载 淘宝自动发货软件 自动答题软件 疯狂java 博途v14安装教程
当前位置: 首页 > 学习教程  > 编程语言

享元模式--自定义数据库连接池

2020/9/19 13:33:27 文章标签:

在String类和包装类大量使用了享元模式,就是使用Cache缓存了一部分数据而不是每次先创建元素或对象。

自定义数据库连接池

package 享元模式;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicIntegerArray;

public class 自定义数据库连接池 {

    public static void main(String[] args) {
        Pool pool = new Pool(2);
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                Connection conn = pool.borrow();
                try {
                    Thread.sleep(new Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                pool.free(conn);
            }).start();
        }
    }


}

@Slf4j
class Pool{
    // 1. 连接池大小
    private final int poolSize;

    // 2. 连接池数组
    private Connection[] connections;

    // 3. 连接状态数组
    private AtomicIntegerArray states;

    // 4. 构造方法初始化

    public Pool(int poolSize) {
        this.poolSize = poolSize;
        this.connections = new Connection[poolSize];
        this.states = new AtomicIntegerArray(new int[poolSize]);
        for (int i = 0; i < poolSize; i++) {
            connections[i] = new MockConnection("连接" + i);
        }
    }

    // 5. 借连接
    public Connection borrow(){
        while(true){
            for (int i = 0; i < poolSize; i++) {
                //获取空连接
                if(states.get(i) == 0){
                    if(states.compareAndSet(i,0,1)){
                        log.debug("borrow {}",connections[i]);
                        return connections[i];
                    }
                }
            }
            //如果没有空闲连接,当前线程进入等待
            synchronized (this){
                try {
                    log.debug("wait ...");
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 6. 归还连接
    public void free(Connection conn){
        for (int i = 0; i < poolSize; i++) {
            if(connections[i] == conn){
                states.set(i,0);
                log.debug("free {}",connections[i]);
                synchronized (this){
                    this.notifyAll();
                }
                break;
            }
        }
    }

}

class MockConnection implements Connection{

    private String name;

    public MockConnection(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "MockConnection{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    public Statement createStatement() throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        return null;
    }

    @Override
    public CallableStatement prepareCall(String sql) throws SQLException {
        return null;
    }

    @Override
    public String nativeSQL(String sql) throws SQLException {
        return null;
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {

    }

    @Override
    public boolean getAutoCommit() throws SQLException {
        return false;
    }

    @Override
    public void commit() throws SQLException {

    }

    @Override
    public void rollback() throws SQLException {

    }

    @Override
    public void close() throws SQLException {

    }

    @Override
    public boolean isClosed() throws SQLException {
        return false;
    }

    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        return null;
    }

    @Override
    public void setReadOnly(boolean readOnly) throws SQLException {

    }

    @Override
    public boolean isReadOnly() throws SQLException {
        return false;
    }

    @Override
    public void setCatalog(String catalog) throws SQLException {

    }

    @Override
    public String getCatalog() throws SQLException {
        return null;
    }

    @Override
    public void setTransactionIsolation(int level) throws SQLException {

    }

    @Override
    public int getTransactionIsolation() throws SQLException {
        return 0;
    }

    @Override
    public SQLWarning getWarnings() throws SQLException {
        return null;
    }

    @Override
    public void clearWarnings() throws SQLException {

    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return null;
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return null;
    }

    @Override
    public Map<String, Class<?>> getTypeMap() throws SQLException {
        return null;
    }

    @Override
    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {

    }

    @Override
    public void setHoldability(int holdability) throws SQLException {

    }

    @Override
    public int getHoldability() throws SQLException {
        return 0;
    }

    @Override
    public Savepoint setSavepoint() throws SQLException {
        return null;
    }

    @Override
    public Savepoint setSavepoint(String name) throws SQLException {
        return null;
    }

    @Override
    public void rollback(Savepoint savepoint) throws SQLException {

    }

    @Override
    public void releaseSavepoint(Savepoint savepoint) throws SQLException {

    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return null;
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
        return null;
    }

    @Override
    public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
        return null;
    }

    @Override
    public Clob createClob() throws SQLException {
        return null;
    }

    @Override
    public Blob createBlob() throws SQLException {
        return null;
    }

    @Override
    public NClob createNClob() throws SQLException {
        return null;
    }

    @Override
    public SQLXML createSQLXML() throws SQLException {
        return null;
    }

    @Override
    public boolean isValid(int timeout) throws SQLException {
        return false;
    }

    @Override
    public void setClientInfo(String name, String value) throws SQLClientInfoException {

    }

    @Override
    public void setClientInfo(Properties properties) throws SQLClientInfoException {

    }

    @Override
    public String getClientInfo(String name) throws SQLException {
        return null;
    }

    @Override
    public Properties getClientInfo() throws SQLException {
        return null;
    }

    @Override
    public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
        return null;
    }

    @Override
    public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
        return null;
    }

    @Override
    public void setSchema(String schema) throws SQLException {

    }

    @Override
    public String getSchema() throws SQLException {
        return null;
    }

    @Override
    public void abort(Executor executor) throws SQLException {

    }

    @Override
    public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {

    }

    @Override
    public int getNetworkTimeout() throws SQLException {
        return 0;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}

输出:

13:29:04.601 [Thread-2] DEBUG 享元模式.Pool - wait ...
13:29:04.601 [Thread-1] DEBUG 享元模式.Pool - borrow MockConnection{name='连接1'}
13:29:04.601 [Thread-0] DEBUG 享元模式.Pool - borrow MockConnection{name='连接0'}
13:29:04.611 [Thread-4] DEBUG 享元模式.Pool - wait ...
13:29:04.611 [Thread-3] DEBUG 享元模式.Pool - wait ...
13:29:04.718 [Thread-0] DEBUG 享元模式.Pool - free MockConnection{name='连接0'}
13:29:04.718 [Thread-4] DEBUG 享元模式.Pool - wait ...
13:29:04.718 [Thread-3] DEBUG 享元模式.Pool - borrow MockConnection{name='连接0'}
13:29:04.718 [Thread-2] DEBUG 享元模式.Pool - wait ...
13:29:04.727 [Thread-3] DEBUG 享元模式.Pool - free MockConnection{name='连接0'}
13:29:04.727 [Thread-4] DEBUG 享元模式.Pool - wait ...
13:29:04.727 [Thread-2] DEBUG 享元模式.Pool - borrow MockConnection{name='连接0'}
13:29:05.446 [Thread-2] DEBUG 享元模式.Pool - free MockConnection{name='连接0'}
13:29:05.446 [Thread-4] DEBUG 享元模式.Pool - borrow MockConnection{name='连接0'}
13:29:05.559 [Thread-1] DEBUG 享元模式.Pool - free MockConnection{name='连接1'}
13:29:05.970 [Thread-4] DEBUG 享元模式.Pool - free MockConnection{name='连接0'}

本文链接: http://www.dtmao.cc/news_show_200018.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?