连接池的作用就是为了提高性能,既然能提高性能还等啥,我们自己模拟编写一个连接池,探其究竟,明其原理。

本文主要是介绍连接池的作用就是为了提高性能,既然能提高性能还等啥,我们自己模拟编写一个连接池,探其究竟,明其原理。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

连接池的作用就是为了提高性能。

连接池的作用:连接池是将已经创建好的连接保存在池中,当有请求来时,直接使用已经创建好的连接对数据库进行访问。这样省略了创建连接和销毁连接的过程。这样性能上得到了提高。

基本原理是这样的:

  1. 建立数据库连接池对象(服务器启动)。
  2. 按照事先指定的参数创建初始数量的数据库连接(即:空闲连接数)。
  3. 对于一个数据库访问请求,直接从连接池中得到一个连接。如果数据库连接池对象中没有空闲的连接,且连接数没有达到最大(即:最大活跃连接数),创建一个新的数据库连接。
  4. 存取数据库。
  5. 关闭数据库,释放所有数据库连接(此时的关闭数据库连接,并非真正关闭,而是将其放入空闲队列中。如实际空闲连接数大于初始空闲连接数则释放连接)。
  6. 释放数据库连接池对象(服务器停止、维护期间,释放数据库连接池对象,并释放所有连接)。

连接池的概念和为什么要使用连接池?

连接池放了N个Connection对象,本质上放在内存当中,在内存中划出一块缓存对象,应用程序每次从池里获得Connection对象,而不是直接从数据里获得,这样不占用服务器的内存资源。

如果不使用连接池会出现的情况:

  • 占用服务器的内存资源
  • 导致服务器的速度非常慢

 既然,大家都了解了连接池的好处,那么我就动手写个固定大小的连接池,以方便伙伴们学习和交流。话不多说,直接上代码,走着~

package com.smallfan.connectionpool;import com.smallfan.TestVisiblity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicIntegerArray;/*** @PACKAGE_NAME: com.smallfan.connectionpool* @NAME: ConnectionPool* @USER: dell* @DATE: 2020/5/28* @PROJECT_NAME: aboutthread* 模拟实现固定大小的数据库连接池*/
public class TestPool{public static void main(String[] args) {//初始化连接池ConnectionPool pool = new ConnectionPool(2);for (int i = 0; i < 5; i++) {//创建5个线程争抢连接池new Thread(()->{Connection apply = pool.apply();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}pool.free(apply);}).start();}}
}
class ConnectionPool {static Logger logger = LoggerFactory.getLogger(ConnectionPool.class);//初始化连接池池大小private final int poolSize;//使用数组存储连接对象private Connection[] connections;//使用原子数组记录连接池状态 0:空闲 1:繁忙private AtomicIntegerArray status;//初始化public ConnectionPool(int poolSize) {this.poolSize = poolSize;this.connections = new Connection[poolSize];this.status = new AtomicIntegerArray(new int[poolSize]);for (int i = 0; i < poolSize; i++) {//初始化数据库连接connections[i] = new TestConnection("连接"+i);}}//申请连接public Connection apply(){while (true){for (int i = 0; i < poolSize; i++) {if(status.get(i) == 0){//是否存在空闲的连接if(status.compareAndSet(i,0,1)){//更新空闲连接为繁忙状态,保证原子操作并返回truelogger.info("获取连接 {}",connections[i]);return connections[i];}}}//若都是繁忙状态,阻塞synchronized (this){try {logger.info("等待连接");this.wait();} catch (InterruptedException e) {e.printStackTrace();}}}}//释放连接public void free(Connection connection){for (int i = 0; i < poolSize; i++) {if (connections[i] == connection){logger.info("释放连接{}",connections[i]);status.set(i,0);synchronized (this){this.notifyAll();}break;}}}}
//模拟实现Connection接口
class TestConnection implements Connection{String name;public TestConnection(String name) {this.name = name;}@Overridepublic String toString() {return "TestConnection{" +"name='" + name + '\'' +'}';}public Statement createStatement() throws SQLException {return null;}public PreparedStatement prepareStatement(String sql) throws SQLException {return null;}public CallableStatement prepareCall(String sql) throws SQLException {return null;}public String nativeSQL(String sql) throws SQLException {return null;}public void setAutoCommit(boolean autoCommit) throws SQLException {}public boolean getAutoCommit() throws SQLException {return false;}public void commit() throws SQLException {}public void rollback() throws SQLException {}public void close() throws SQLException {}public boolean isClosed() throws SQLException {return false;}public DatabaseMetaData getMetaData() throws SQLException {return null;}public void setReadOnly(boolean readOnly) throws SQLException {}public boolean isReadOnly() throws SQLException {return false;}public void setCatalog(String catalog) throws SQLException {}public String getCatalog() throws SQLException {return null;}public void setTransactionIsolation(int level) throws SQLException {}public int getTransactionIsolation() throws SQLException {return 0;}public SQLWarning getWarnings() throws SQLException {return null;}public void clearWarnings() throws SQLException {}public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {return null;}public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {return null;}public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {return null;}public Map<String, Class<?>> getTypeMap() throws SQLException {return null;}public void setTypeMap(Map<String, Class<?>> map) throws SQLException {}public void setHoldability(int holdability) throws SQLException {}public int getHoldability() throws SQLException {return 0;}public Savepoint setSavepoint() throws SQLException {return null;}public Savepoint setSavepoint(String name) throws SQLException {return null;}public void rollback(Savepoint savepoint) throws SQLException {}public void releaseSavepoint(Savepoint savepoint) throws SQLException {}public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {return null;}public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {return null;}public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {return null;}public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {return null;}public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {return null;}public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {return null;}public Clob createClob() throws SQLException {return null;}public Blob createBlob() throws SQLException {return null;}public NClob createNClob() throws SQLException {return null;}public SQLXML createSQLXML() throws SQLException {return null;}public boolean isValid(int timeout) throws SQLException {return false;}public void setClientInfo(String name, String value) throws SQLClientInfoException {}public void setClientInfo(Properties properties) throws SQLClientInfoException {}public String getClientInfo(String name) throws SQLException {return null;}public Properties getClientInfo() throws SQLException {return null;}public Array createArrayOf(String typeName, Object[] elements) throws SQLException {return null;}public Struct createStruct(String typeName, Object[] attributes) throws SQLException {return null;}public void setSchema(String schema) throws SQLException {}public String getSchema() throws SQLException {return null;}public void abort(Executor executor) throws SQLException {}public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {}public int getNetworkTimeout() throws SQLException {return 0;}public <T> T unwrap(Class<T> iface) throws SQLException {return null;}public boolean isWrapperFor(Class<?> iface) throws SQLException {return false;}
}

查看输出结果:

2020-05-28 23:37:18:687 [Thread-2] 等待连接
2020-05-28 23:37:18:689 [Thread-0] 获取连接 TestConnection{name='连接1'}
2020-05-28 23:37:18:689 [Thread-1] 获取连接 TestConnection{name='连接0'}
2020-05-28 23:37:18:716 [Thread-4] 等待连接
2020-05-28 23:37:18:716 [Thread-3] 等待连接
2020-05-28 23:37:19:717 [Thread-1] 释放连接TestConnection{name='连接0'}
2020-05-28 23:37:19:717 [Thread-0] 释放连接TestConnection{name='连接1'}
2020-05-28 23:37:19:718 [Thread-3] 获取连接 TestConnection{name='连接0'}
2020-05-28 23:37:19:718 [Thread-2] 等待连接
2020-05-28 23:37:19:718 [Thread-4] 获取连接 TestConnection{name='连接1'}
2020-05-28 23:37:20:732 [Thread-3] 释放连接TestConnection{name='连接0'}
2020-05-28 23:37:20:732 [Thread-2] 获取连接 TestConnection{name='连接0'}
2020-05-28 23:37:20:732 [Thread-4] 释放连接TestConnection{name='连接1'}
2020-05-28 23:37:21:732 [Thread-2] 释放连接TestConnection{name='连接0'}

 好像没有什么问题,欢迎伙伴们交流讨论,等你哦~

 

这篇关于连接池的作用就是为了提高性能,既然能提高性能还等啥,我们自己模拟编写一个连接池,探其究竟,明其原理。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068895

相关文章

Java的"伪泛型"变"真泛型"后对性能的影响

《Java的伪泛型变真泛型后对性能的影响》泛型擦除本质上就是擦除与泛型相关的一切信息,例如参数化类型、类型变量等,Javac还将在需要时进行类型检查及强制类型转换,甚至在必要时会合成桥方法,这篇文章主... 目录1、真假泛型2、性能影响泛型存在于Java源代码中,在编译为字节码文件之前都会进行泛型擦除(ty

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component

使用Java编写一个字符脱敏工具类

《使用Java编写一个字符脱敏工具类》这篇文章主要为大家详细介绍了如何使用Java编写一个字符脱敏工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、字符脱敏工具类2、测试工具类3、测试结果1、字符脱敏工具类import lombok.extern.slf4j.Slf4j

Kotlin运算符重载函数及作用场景

《Kotlin运算符重载函数及作用场景》在Kotlin里,运算符重载函数允许为自定义类型重新定义现有的运算符(如+-…)行为,从而让自定义类型能像内置类型那样使用运算符,本文给大家介绍Kotlin运算... 目录基本语法作用场景类对象数据类型接口注意事项在 Kotlin 里,运算符重载函数允许为自定义类型重

spring IOC的理解之原理和实现过程

《springIOC的理解之原理和实现过程》:本文主要介绍springIOC的理解之原理和实现过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、IoC 核心概念二、核心原理1. 容器架构2. 核心组件3. 工作流程三、关键实现机制1. Bean生命周期2.

Redis实现分布式锁全解析之从原理到实践过程

《Redis实现分布式锁全解析之从原理到实践过程》:本文主要介绍Redis实现分布式锁全解析之从原理到实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、背景介绍二、解决方案(一)使用 SETNX 命令(二)设置锁的过期时间(三)解决锁的误删问题(四)Re

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

SpringBoot中配置Redis连接池的完整指南

《SpringBoot中配置Redis连接池的完整指南》这篇文章主要为大家详细介绍了SpringBoot中配置Redis连接池的完整指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以... 目录一、添加依赖二、配置 Redis 连接池三、测试 Redis 操作四、完整示例代码(一)pom.