自定义连接池 用代码读写全过程

package 连接池;import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;public class MyConnection implements Connection {private  MyDateSource myDateSource;private Connection connection;public  MyConnection(MyDateSource myDateSource,Connection connection){this.myDateSource=myDateSource;this.connection=connection;}@Overridepublic void close() throws SQLException {//把连接从工作连接池放回空闲连接池if(myDateSource.getIdlePoll().size()<myDateSource.getMaxActive()){myDateSource.getWorkPool().remove(this);myDateSource.getIdlePoll().addLast(this);}}@Overridepublic boolean isClosed() throws SQLException {return false;}@Overridepublic Statement createStatement() throws SQLException {return connection.createStatement();}@Overridepublic PreparedStatement prepareStatement(String sql) throws SQLException {return connection.prepareStatement(sql);}@Overridepublic CallableStatement prepareCall(String sql) throws SQLException {return null;}@Overridepublic String nativeSQL(String sql) throws SQLException {return null;}@Overridepublic void setAutoCommit(boolean autoCommit) throws SQLException {}@Overridepublic boolean getAutoCommit() throws SQLException {return false;}@Overridepublic void commit() throws SQLException {}@Overridepublic void rollback() throws SQLException {}@Overridepublic DatabaseMetaData getMetaData() throws SQLException {return null;}@Overridepublic void setReadOnly(boolean readOnly) throws SQLException {}@Overridepublic boolean isReadOnly() throws SQLException {return false;}@Overridepublic void setCatalog(String catalog) throws SQLException {}@Overridepublic String getCatalog() throws SQLException {return null;}@Overridepublic void setTransactionIsolation(int level) throws SQLException {}@Overridepublic int getTransactionIsolation() throws SQLException {return 0;}@Overridepublic SQLWarning getWarnings() throws SQLException {return null;}@Overridepublic void clearWarnings() throws SQLException {}@Overridepublic Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {return null;}@Overridepublic PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {return null;}@Overridepublic CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {return null;}@Overridepublic Map<String, Class<?>> getTypeMap() throws SQLException {return null;}@Overridepublic void setTypeMap(Map<String, Class<?>> map) throws SQLException {}@Overridepublic void setHoldability(int holdability) throws SQLException {}@Overridepublic int getHoldability() throws SQLException {return 0;}@Overridepublic Savepoint setSavepoint() throws SQLException {return null;}@Overridepublic Savepoint setSavepoint(String name) throws SQLException {return null;}@Overridepublic void rollback(Savepoint savepoint) throws SQLException {}@Overridepublic void releaseSavepoint(Savepoint savepoint) throws SQLException {}@Overridepublic Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {return null;}@Overridepublic PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {return null;}@Overridepublic CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {return null;}@Overridepublic PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {return null;}@Overridepublic PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {return null;}@Overridepublic PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {return null;}@Overridepublic Clob createClob() throws SQLException {return null;}@Overridepublic Blob createBlob() throws SQLException {return null;}@Overridepublic NClob createNClob() throws SQLException {return null;}@Overridepublic SQLXML createSQLXML() throws SQLException {return null;}@Overridepublic boolean isValid(int timeout) throws SQLException {return false;}@Overridepublic void setClientInfo(String name, String value) throws SQLClientInfoException {}@Overridepublic void setClientInfo(Properties properties) throws SQLClientInfoException {}@Overridepublic String getClientInfo(String name) throws SQLException {return null;}@Overridepublic Properties getClientInfo() throws SQLException {return null;}@Overridepublic Array createArrayOf(String typeName, Object[] elements) throws SQLException {return null;}@Overridepublic Struct createStruct(String typeName, Object[] attributes) throws SQLException {return null;}@Overridepublic void setSchema(String schema) throws SQLException {}@Overridepublic String getSchema() throws SQLException {return null;}@Overridepublic void abort(Executor executor) throws SQLException {}@Overridepublic void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {}@Overridepublic int getNetworkTimeout() throws SQLException {return 0;}@Overridepublic <T> T unwrap(Class<T> iface) throws SQLException {return null;}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {return false;}
}

package 连接池;import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;public class MyDateSource extends MyAdstractDataSource{//工作连接池private LinkedList<MyConnection> workPool=new LinkedList<>();//空闲连接池private LinkedList<MyConnection> idlePoll=new LinkedList<>();public LinkedList<MyConnection> getWorkPool() {return workPool;}private  boolean initFlag=false;public LinkedList<MyConnection> getIdlePoll() {return idlePoll;}public MyDateSource(){}//TODO 从工作连接池获取一个连接@Overridepublic Connection getConnection() throws SQLException {MyConnection connection=null;this.init();//如果空闲连接不为空就直接取if(!idlePoll.isEmpty()){connection=idlePoll.removeFirst();}else{//如果工作连接数小于最大连接数就创建一个if(workPool.size()<super.getMaxActive()){connection =new MyConnection(this,super.getConnection());}}//连接不为空添加到工作连接if(connection!=null){workPool.addLast(connection);}return connection;}public  void init() throws SQLException {//初始化十个连接if(!initFlag==false){while (idlePoll.size()<super.getInitialSize()) {idlePoll.add(new MyConnection(this, super.getConnection()));}initFlag=true;}}
}

package 连接池;import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;public abstract class MyAdstractDataSource implements DataSource  {private  String url;public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}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 getDriverClassName() {return driverClassName;}public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}public int getInitialSize() {return initialSize;}public void setInitialSize(int initialSize) {this.initialSize = initialSize;}public int getMaxActive() {return maxActive;}public void setMaxActive(int maxActive) {this.maxActive = maxActive;}public boolean isInitFlag() {return initFlag;}public void setInitFlag(boolean initFlag) {this.initFlag = initFlag;}private  String username;private  String password;private  String driverClassName;//初始连接数private int initialSize=10;private  int maxActive=30;private  boolean initFlag=false;@Overridepublic Connection getConnection() throws SQLException {return getConnection(username,password);}@Overridepublic Connection getConnection(String username, String password) throws SQLException {return doDetConnection(username,password);}private  Connection doDetConnection(String username,String password) throws SQLException{if(!initFlag){try {Class.forName(driverClassName);} catch (ClassNotFoundException e) {e.printStackTrace();}initFlag=true;}return DriverManager.getConnection(url,username,password);}@Overridepublic <T> T unwrap(Class<T> iface) throws SQLException {return null;}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {return false;}@Overridepublic PrintWriter getLogWriter() throws SQLException {return null;}@Overridepublic void setLogWriter(PrintWriter out) throws SQLException {}@Overridepublic void setLoginTimeout(int seconds) throws SQLException {}@Overridepublic int getLoginTimeout() throws SQLException {return 0;}@Overridepublic Logger getParentLogger() throws SQLFeatureNotSupportedException {return null;}
}

package 连接池;import org.junit.Test;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class TestMyDataSource {public  static  String url="jdbc:mysql://localhost:3306/java001?useServerPrepStmts=true";public static String username="root";public static String password="root";public static String driverClassName="com.mysql.cj.jdbc.Driver";@Testpublic  void testMyDataSource() throws SQLException {MyDateSource myDateSource=new MyDateSource();myDateSource.setDriverClassName(driverClassName);myDateSource.setUrl(url);myDateSource.setUsername(username);myDateSource.setPassword(password);long statr=System.currentTimeMillis();for (int i = 0; i <500 ; i++) {Connection connection=myDateSource.getConnection();connection.close();}long end=System.currentTimeMillis();System.out.println(""+(end-statr)+"ms");statr=System.currentTimeMillis();for (int i = 0; i <500 ; i++) {Connection connection= DriverManager.getConnection(url,username,password);connection.close();}
end=System.currentTimeMillis();System.out.println(""+(end-statr)+"ms");}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/199825.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C语言:自定义类型(结构体、位段、共用体、枚举)】

文章目录 1.结构体1.1什么是结构体1.2结构体类型声明1.3结构体变量的定义和初始化1.4结构体的访问 2.结构体对齐2.1如何对齐2.2为什么存在内存对齐&#xff1f; 3.结构体实现位段3.1什么是位段3.2位段的内存分配3.3位段的跨平台问题3.4位段的应用3.5位段使用注意事项 4.联合体4…

Chapter 6 Managing Application Engine Programs 管理应用程序引擎程序

Chapter 6 Managing Application Engine Programs 管理应用程序引擎程序 Running Application Engine Programs 运行应用程序引擎程序 This section provides an overview of program run options and discusses how to: 本节提供程序运行选项的概述&#xff0c;并讨论如何…

上下拉电阻会增强驱动能力吗?

最近看到一个关于上下拉电阻的问题&#xff0c;发现不少人认为上下拉电阻能够增强驱动能力。随后跟几个朋友讨论了一下&#xff0c;大家一致认为不存在上下拉电阻增强驱动能力这回事&#xff0c;因为除了OC输出这类特殊结构外&#xff0c;上下拉电阻就是负载&#xff0c;只会减…

RT-Thread Studio文件消失不见或被排除构建

不得不说RT-Thread Studio里面配置真多&#xff0c;今天我同事的电脑发现根本没有被画斜杠的文件夹&#xff0c;导致我想移植f1的写内部flash这个&#xff08;可以看上一个文章&#xff09;时候不能直接点击属性排除构建&#xff0c;然后在网上查找的时候也没怎么找到说法&…

《漫长的等待》—— 读后感

前几天下班地铁上&#xff0c;人太多&#xff0c;看技术书籍看不进去&#xff0c;翻阅微信读书&#xff0c;看到了这本书&#xff0c;看了几章免费的章节&#xff0c;因为后续需要买会员就没有继续读&#xff0c;但是这几天偶尔还是会想到书籍中的情节&#xff0c;所以今天充了…

基于SQL语言的数据库管理系统

zstarling 常见的SQL数据库管理系统具体区别PLPGSQL和MYSQL的区别 常见的SQL数据库管理系统 下面是一些常见的SQL数据库管理系统和它们的语法联系和区别 数据库管理系统语法联系法区别Oracle基于SQL的语言拥有自身独特的函数和特性&#xff0c;例如行级锁和高级分组处理Micro…

代码随想录算法训练营 ---第五十五天

今天是 动态规划&#xff1a;编辑距离问题。 第一题&#xff1a; 简介&#xff1a; 动态规划五部曲&#xff1a; 1.确定dp数组的含义 dp[i][j] 表示以下标i-1为结尾的字符串s&#xff0c;和以下标j-1为结尾的字符串t&#xff0c;相同子序列的长度为dp[i][j]。 2.确定递推公…

智能优化算法应用:基于寄生捕食算法无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于寄生捕食算法无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于寄生捕食算法无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.寄生捕食算法4.实验参数设定5.算法结果6.参考…

玩转Omniverse | 将FBX文件导入Omniverse View,以及step等3D格式如何转换为USD文件的过程

1&#xff0c;参考这个过程&#xff0c;玩转Omniverse | 将FBX文件导入Omniverse View 2&#xff0c;实际操作&#xff1a; 在omniverse中安装usd explorer 打开usd explorer 选择step&#xff0c;然后右键选择convert to USD&#xff0c;点击确认&#xff0c;稍等一会就会转换…

Python神器解析时间序列数据:数据分析者必读

更多资料获取 &#x1f4da; 个人网站&#xff1a;ipengtao.com 时间序列数据是在许多领域中都至关重要的数据类型&#xff0c;它涵盖了一系列按时间顺序排列的数据点。Python作为一种强大的数据分析工具&#xff0c;提供了许多库和工具&#xff0c;能够有效地处理、分析和可视…

高级搜索——伸展树Splay详解

文章目录 伸展树Splay伸展树Splay的定义局部性原理Splay的伸展操作逐层伸展双层伸展zig-zig/zag-zagzig-zag/zag-zigzig/zag双层伸展的效果与效率 伸展树的实现动态版本实现递增分配器节点定义Splay类及其接口定义伸展操作左单旋右单旋右左/左右双旋伸展 查找操作删除操作插入操…

基于Java SSM邮局订报管理系统

尽管电子读物越来越普及&#xff0c;但还是有很多读者对纸质刊物情有独钟&#xff0c;所以邮局的报刊征订业务一直非常受欢迎。邮局订报管理系统就是对客户在邮局订阅报刊进行管理&#xff0c;包括查询报刊、订阅报刊、订阅信息的查询、统计等的处理&#xff0c;系统的主要业务…

C语言初学4:C 存储类

auto 存储类 auto 是所有局部变量默认的存储类&#xff0c;只能用在函数内&#xff0c;在函数开始时被创建&#xff0c;结束时被销毁 #include<stdio.h>int main(){/*定义两个具有相同存储类的变量 */int mouth;auto int month;} register 存储类 意味着变量可能存储…

springcloud整合Oauth2自定义登录/登出接口

我使用的是password模式&#xff0c;并配置了token模式 一、登录 (这里我使用的示例是用户名密码认证方式) 1. Oath2提供默认登录授权接口 org.springframework.security.oauth2.provider.endpoint.postAccess; Tokenpublic ResponseEntity<OAuth2AccessToken> pos…

使用docker搭建『Gitea』私有仓库

文章目录 一、安装 docker 环境1、移除以前的 docker 相关包2、配置yum源3、安装 docker4、启动 docker 二、安装 docker compose1、安装docker compose2、赋予下载的docker-compose执行权限 三、安装 gitea1. 创建工作目录2. 创建 Docker Compose 文件3. 启动 Gitea4. 访问 Gi…

【活动】还记得当初自己为什么选择计算机?

方向一&#xff1a;为什么当初选择计算机行业 从小就想当一个生物学家&#xff0c;奈何高考分数不够上哪怕是中国药科大学的药学专业&#xff08;还是计算机更好就业&#xff0c;少不更事不知道这些&#xff0c;一心只想着什么科学信仰之类的&#xff09;&#xff0c;后来被父母…

c++操作数据库(增删改查)------otl库----c++

文章目录 一, insert 插入数据库二, select 查询select 写法一select 写法二 三, update 修改四, delete 删除 包含头文件&#xff1a;#include <otl/otlv4.h> 一, insert 插入数据库 #include <iostream> #include <otl/otlv4.h> // 请确保正确包含 OTL 头…

回溯算法题型分类

题型一&#xff1a;排列、组合、子集相关问题 提示&#xff1a;这部分练习可以帮助我们熟悉「回溯算法」的一些概念和通用的解题思路。解题的步骤是&#xff1a;先画图&#xff0c;再编码。去思考可以剪枝的条件&#xff0c; 为什么有的时候用 used 数组&#xff0c;有的时候设…

前后端接口设计规范

设计规范原则 1. 前端应只关注渲染逻辑&#xff0c;而不应该关注数据的处理逻辑。接口返回的数据应该是能够直接展示在界面上的。 2. 一个功能应避免多个接口嵌套调用获取数据&#xff0c;后台应该处理好一次性返回。 3. 响应格式应该是JSON&#xff0c;而且应避免多级JSON的出…

hbase thrift2 jar包冲突导致启动失败问题排查记录

1、启动命令 ${HBASE_HOME}/bin/hbase-daemon.sh start thrift2 2、异常情况 hbase-root-thrift2-hdfs-test07.yingzi.com.out异常日志&#xff1a; Exception in thread "main" java.lang.AbstractMethodError: org.apache.hadoop.metrics2.sink.timeline.Hadoo…