日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

手写java数据库连接池,自定义实现数据库连接池,兼容springboot

發布時間:2024/10/5 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手写java数据库连接池,自定义实现数据库连接池,兼容springboot 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、目標

? ? ? ? ? 用精簡的代碼實現一個類似于Hikari,Druid一樣的高性能數據庫連接池。

二、實現思路

? ? ? ?1:新建連接池配置類保存連接池配置。

? ? ? ?2:實現DataSource接口。

? ? ? ?3:新增SmpDbPool類,內部維護一個阻塞隊列保存數據庫連接,并提供數據庫連接的獲取回收等方法。

? ? ? ?4:大致類圖:

三、核心代碼

1:數據庫連接代理類SmpConnectionProxy

SmpConnectionProxy實現jdbc的Connection接口

重寫close方法,確保外部調用close的時候將連接歸還到連接池。

public void close() throws SQLException {if (this.closed) return;this.closed = true;//回滾掉未提交的臟statementif (this.isCommitStateDirty && !this.isAutoCommit){this.delegate.rollback();//}this.clearWarnings();this.smpDbPool.releaseConnection(this);}

新增判斷連接是否有效方法供連接池類使用。

public boolean isValid() throws SQLException {if (System.currentTimeMillis() - this.lastActiveTime < this.smpDbPool.getConfig().getKeepaliveTimeInMill()) return true;long validationTimeout = this.smpDbPool.getConfig().getValidationTimeoutInMill();this.delegate.setNetworkTimeout(this.smpDbPool.getNetTimeoutExecutor(), (int) validationTimeout);int validationSeconds = (int) Math.max(1000L, validationTimeout) / 1000;return this.delegate.isValid(validationSeconds);}

2:數據源類SmpDataSource

SmpDataSource實現DataSource接口

實現getConnection方法,以便外界從連接池獲取連接。

@Overridepublic Connection getConnection() throws SQLException {try {return this.dbPool.borrowConnection();} catch (Exception e) {LOG.error("getConnection error", e);throw new SQLException(e);}}

3:連接池類SmpDbPool

獲取數據庫連接方法:

public Connection borrowConnection() throws Exception {boolean isDebugEnable = LOG.isDebugEnabled();long sTime = 0;if (isDebugEnable){//盡量少訪問臨界資源sTime = System.currentTimeMillis();}try {SmpConnectionProxy connection = this.getConnectionFromQueue();if (connection != null) return connection;//未獲取到有效連接,校驗是否創建連接int maxActive = this.smpConfig.getMaxActive(), dbAmount = createdDbConnectionAmount.get();if (dbAmount >= maxActive){//嘗試等待其他線程釋放連接connection = this.dbConnectQueue.poll(this.smpConfig.getMaxWaitInMill(), TimeUnit.MILLISECONDS);if (connection != null) return connection.borrowConnection();LOG.error("get connection error, no connection available,maxActive:{}, activated connection:{}", maxActive, dbAmount);throw new IllegalStateException("database connection pool too busy");}connection = this.createConnectionForPool();if (connection != null) return connection;throw new IllegalStateException("database connection pool too busy");} finally {if (isDebugEnable){LOG.debug("get database connection cost {} ms", System.currentTimeMillis() - sTime);}}}

從阻塞隊列獲取數據庫連接方法:

private SmpConnectionProxy getConnectionFromQueue() throws SQLException {int times = 0;while (times++ < 1000){SmpConnectionProxy connection = this.dbConnectQueue.poll();if (connection == null) return null;if (connection.isValid()) return connection.borrowConnection();//destroy invalid connectionconnection.destroy();createdDbConnectionAmount.decrementAndGet();LOG.info("destroyed invalid jdbc connection");}return null;}

新建數據庫連接:

private SmpConnectionProxy createConnectionForPool() throws Exception{boolean locked = false;try {locked = GET_CONNECTION_LOCK.tryLock(this.smpConfig.getMaxWaitInMill() >> 1, TimeUnit.MILLISECONDS);if (!locked){LOG.error("get lock to create connection error");return null;}SmpConnectionProxy connection = this.getConnectionFromQueue();if (connection != null) return connection;int dbAmount = createdDbConnectionAmount.get(), maxActive = this.smpConfig.getMaxActive();if (dbAmount >= maxActive){LOG.error("get connection error, no connection available,maxActive:{}, activated connection:{}", maxActive, dbAmount);return null;}connection = this.createConnection();createdDbConnectionAmount.addAndGet(1);return connection.borrowConnection();} catch (ClassNotFoundException ex){LOG.error("class {} not found", this.smpConfig.getDriverClassName(), ex);throw new SQLException(ex);}finally {if (locked){GET_CONNECTION_LOCK.unlock();}}}private SmpConnectionProxy createConnection() throws ClassNotFoundException, SQLException {Class.forName( this.smpConfig.getDriverClassName() );Connection connection = DriverManager.getConnection( this.smpConfig.getUrl(), this.smpConfig.getUsername(), this.smpConfig.getPassword());LOG.debug("create new database connection with url:{}", this.smpConfig.getUrl());return new SmpConnectionProxy(connection, this, connection.getAutoCommit());}

4:構建spring-boot-starter

? ? ? ? ?這里需要說明的是springboot默認會嘗試使用com.zaxxer.hikari.HikariDataSource,org.apache.tomcat.jdbc.pool.DataSource,org.apache.commons.dbcp2.BasicDataSource作為連接池,相關代碼邏輯在org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration,因此配置spring-boot-starter的時候先要確保應用沒有配置連接池。

核心代碼:

?5:springboot使用

添加依賴后再添加如下配置:

spring.datasource.type=com.lauor.smpdb.SmpDataSource #最小連接數,默認4 spring.datasource.smpdb.minIdle=4 #最大連接數,默認40 spring.datasource.smpdb.maxActive=20 #數據庫連接有效期檢查間隔時間ms,默認30分鐘,最小1s spring.datasource.smpdb.keepaliveTimeInMill=1800000 #獲取數據庫連接最大等待時間ms,默認30s,最小1s spring.datasource.smpdb.maxWaitInMill=30000 #數據連接合法性校驗超時時間,默認5s,最小1s spring.datasource.smpdb.validationTimeoutInMill=5000

連接池完整代碼:https://gitee.com/tandatda/smpdb

連接池使用完整demo:https://gitee.com/tandatda/demo-edr-smpdb

總結

以上是生活随笔為你收集整理的手写java数据库连接池,自定义实现数据库连接池,兼容springboot的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产女人18水真多毛片18精品 | 69超碰 | 精品国产一区二区三区四区 | 少妇精品一区二区 | 人人射视频| 熟妇高潮一区二区 | 国产一线二线在线观看 | 国语对白对话在线观看 | 在线观看一区二区视频 | 亚洲精品在线视频 | 中国一级特黄录像播放 | 91免费看片 | 精品国产乱码久久久久久1区二区 | 69精品人人 | 青春草免费视频 | 精品国产乱码久久久久久图片 | a∨视频 | 性欧美videos另类hd | 日韩av成人 | 日韩天堂在线 | 久久久久免费看 | 九九在线免费视频 | 国产人妖ts重口系列网站观看 | 婷婷五月综合久久中文字幕 | 涩涩涩av | 欧美精品一级在线观看 | 四虎在线影院 | 黄a视频 | 亚洲天堂麻豆 | 自拍偷拍在线播放 | 正在播放91 | 天天操人人爽 | 好邻居韩国剧在线观看 | 国产精品久久久999 www日本高清视频 | 十八岁世界在线观看高清免费韩剧 | 裸体美女免费视频网站 | 一级黄大片 | 少妇高潮久久久久久潘金莲 | 91涩涩视频| 伊人久久免费 | 色呦呦在线免费观看 | 悟空影视大全免费高清观看在线 | 天天干天天操天天碰 | wwwwxxxxx日本| 视频1区 | 中文字幕成人av | 一级淫片免费看 | 越南毛茸茸的少妇 | 欧美日本日韩 | 国产乱人乱偷精品视频a人人澡 | av大片免费观看 | 在线国产视频一区 | 国产精品麻豆成人av电影艾秋 | 国产亚洲在线观看 | а√天堂www在线天堂小说 | 乳罩脱了喂男人吃奶视频 | 在线观看免费高清在线观看 | 国产精品3区 | 91porny在线 | 免费的毛片视频 | 朝桐光av在线一区二区三区 | 精品国产系列 | 国产夫妻性爱视频 | 澳门黄色网 | 亚洲精品二三区 | 1024久久 | 天天天天天天干 | 日本熟女一区二区 | 台湾少妇xxxx做受 | 色中文| 夜间福利视频 | 免费看黄色aaaaaa 片 | 天天干天天操天天操 | 越南毛茸茸的少妇 | 久久精品噜噜噜成人 | 老子影院午夜精品无码 | 中文字幕无码毛片免费看 | 成人xxx| 日韩欧美99| 国产成人在线观看 | 欧洲美熟女乱又伦 | a视频在线看 | 成人动漫在线观看 | 国产精品美女在线 | 你懂的网站在线观看 | 五月六月婷婷 | 一本大道综合伊人精品热热 | 欧美精品在线视频观看 | 国产男女猛烈无遮挡免费观看网站 | 欧美视频一区二区在线观看 | 第一av在线 | av免费国产 | 国精品一区 | 亚洲 精品 综合 精品 自拍 | 亚洲中文字幕第一区 | 在线观看黄网 | 99精品人妻国产毛片 | 九九操| 亚洲精品久久久中文字幕痴女 |