色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java 模擬數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)代碼

瀏覽:4日期:2022-08-16 11:08:44

前面學(xué)習(xí)過(guò)等待 - 通知機(jī)制,現(xiàn)在我們?cè)谄浠A(chǔ)上添加一個(gè)超時(shí)機(jī)制,模擬從連接池中獲取、使用和釋放連接的過(guò)程。客戶端獲取連接的過(guò)程被設(shè)定為等待超時(shí)模式,即如果在 1000 毫秒內(nèi)無(wú)法獲取到可用連接,將會(huì)返回給客戶端一個(gè) null。設(shè)定連接池的大小為 10 個(gè),然后通過(guò)調(diào)節(jié)客戶端的線程數(shù)來(lái)模擬無(wú)法獲取連接的場(chǎng)景

由于 java.sql.Connection 只是一個(gè)接口,最終實(shí)現(xiàn)是由數(shù)據(jù)庫(kù)驅(qū)動(dòng)提供方來(lái)實(shí)現(xiàn),考慮到本例只是演示,我們通過(guò)動(dòng)態(tài)代理構(gòu)造一個(gè) Connection,該 Connection 的代理僅僅是在調(diào)用 commit() 方法時(shí)休眠 100 毫秒

public class ConnectionDriver { static class ConnectionHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ('commit'.equals(method.getName())) {TimeUnit.MICROSECONDS.sleep(100); } return null; } } /** * 創(chuàng)建一個(gè) Connection 的代理,在 commit 時(shí)休眠 100 毫秒 */ public static Connection createConnection() { return (Connection) Proxy.newProxyInstance(ConnectionDriver.class.getClassLoader(),new Class<?>[]{Connection.class}, new ConnectionHandler()); }}

接下來(lái)是線程池的實(shí)現(xiàn)。本例通過(guò)一個(gè)雙向隊(duì)列來(lái)維護(hù)連接,調(diào)用方需要先調(diào)用 fetchConnection(long) 方法來(lái)指定在多少毫秒內(nèi)超時(shí)獲取連接,當(dāng)連接使用完成后,需要調(diào)用 releaseConnection(Connection) 方法將連接放回線程池

public class ConnectionPool { private final LinkedList<Connection> pool = new LinkedList<>(); public ConnectionPool(int initialSize) { // 初始化連接的最大上限 if (initialSize > 0) { for (int i = 0; i < initialSize; i++) {pool.addLast(ConnectionDriver.createConnection()); } } } public void releaseConnection(Connection connection) { if (connection != null) { synchronized (pool) {/* 連接釋放后需要進(jìn)行通知 * 這樣其他消費(fèi)者就能知道連接池已經(jīng)歸還了一個(gè)連接 */pool.addLast(connection);pool.notifyAll(); } } } /** * 在給定毫秒時(shí)間內(nèi)獲取連接 */ public Connection fetchConnection(long mills) throws InterruptedException { synchronized (pool) { // 完全超時(shí) if (mills < 0) {while (pool.isEmpty()) { pool.wait();}return pool.removeFirst(); } else {long future = System.currentTimeMillis() + mills;long remaining = mills;while (pool.isEmpty() && remaining > 0) { pool.wait(remaining); remaining = future - System.currentTimeMillis();}Connection result = null;if (!pool.isEmpty()) { result = pool.removeFirst();}return result; } } }}

最后編寫一個(gè)用于模擬客戶端獲取連接的示例,該示例將模擬多個(gè)線程同時(shí)從連接池獲取連接,并記錄總嘗試獲取數(shù)、獲取成功數(shù)和獲取失敗數(shù)

public class ConnectionPoolTest { static ConnectionPool pool = new ConnectionPool(10); static CountDownLatch start = new CountDownLatch(1); static CountDownLatch end; public static void main(String[] args) throws InterruptedException { // 線程數(shù)量 int threadCount = 200; end = new CountDownLatch(threadCount); int count = 20; AtomicInteger got = new AtomicInteger(); AtomicInteger notGot = new AtomicInteger(); for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(new ConnectionRunner(count, got, notGot), 'ConnectionRunnerThread'); thread.start(); } start.countDown(); end.await(); System.out.println('total invoke : ' + (threadCount * count)); System.out.println('got connection : ' + got); System.out.println('not got connection : ' + notGot); } static class ConnectionRunner implements Runnable { int count; AtomicInteger got; AtomicInteger notGot; public ConnectionRunner(int count, AtomicInteger got, AtomicInteger notGot) { this.count = count; this.got = got; this.notGot = notGot; } @Override public void run() { try {start.await(); } catch (Exception e) {e.printStackTrace(); } while (count > 0) {try { // 從線程池中獲取連接,如果 1000ms 內(nèi)無(wú)法獲取到,將返回 null // 分別統(tǒng)計(jì)獲取連接的數(shù)量 got 和未獲取到的數(shù)量 notGot Connection connection = pool.fetchConnection(1000); if (connection != null) { try { connection.createStatement(); connection.commit(); } finally { pool.releaseConnection(connection); got.incrementAndGet(); } } else { notGot.incrementAndGet(); }} catch (Exception e) { e.printStackTrace();} finally { count--;} } end.countDown(); } }}

筆者設(shè)置線程數(shù)量為 200 時(shí),得出結(jié)果如下

Java 模擬數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)代碼

當(dāng)設(shè)置為 500 時(shí),得出結(jié)果如下,當(dāng)然具體結(jié)果根據(jù)機(jī)器性能而異

Java 模擬數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)代碼

可見,隨著客戶端線程數(shù)的增加,客戶端出現(xiàn)超時(shí)無(wú)法獲取連接的比率不斷升高。這種等待超時(shí)模式能保證程序出問(wèn)題時(shí),線程不會(huì)一直運(yùn)行,而是按時(shí)返回,并告知客戶端獲取連接出現(xiàn)問(wèn)題。數(shù)據(jù)庫(kù)連接池的實(shí)際也可以應(yīng)用到其他資源獲取的場(chǎng)景,針對(duì)昂貴資源的獲取都應(yīng)該加以限制

到此這篇關(guān)于Java 模擬數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Java 數(shù)據(jù)庫(kù)連接池內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 日韩毛片免费线上观看 | 国产91久久久久久久免费 | 99九九视频 | aaaaaa精品视频在线观看 | 国产成人高清精品免费5388密 | 性夜黄a爽爽免费视频国产 性夜影院爽黄a爽免费看网站 | 在线免费黄色网址 | 欧美日本在线视频 | 国产a∨一区二区三区香蕉小说 | 女人张开双腿让男人 | 亚洲最大网站在线 | 成人99国产精品一级毛片 | 毛片免费观看成人 | 国产人成亚洲第一网站在线播放 | 欧美一级情欲片在线 | 欧美在线综合视频 | 91碰碰 | 精品久久在线观看 | 美女张开腿让我 | 玖玖爱精品 | 偷柏自拍亚洲欧美综合在线图 | 一级片免费在线播放 | 亚洲国产成人va在线观看网址 | 最新在线精品国自拍视频 | 在线亚洲日产一区二区 | 欧美特黄高清免费观看的 | 久久影院国产 | 日韩一级视频在线观看播放 | 成年女人免费又黄又爽视频 | 久草在线在线 | 亚洲gogo人体大胆西西安徽 | 国产日韩一区二区三区在线播放 | 亚洲rct中文字幕在线 | 国产只有精品 | gv手机在线观看 | 欧美特级视频 | 欧美高清一区二区三 | 亚洲国产一区二区a毛片 | 在线成人亚洲 | 古代级a毛片可以免费看 | 99国产小视频 |