成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

Java 模擬數據庫連接池的實現代碼

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

前面學習過等待 - 通知機制,現在我們在其基礎上添加一個超時機制,模擬從連接池中獲取、使用和釋放連接的過程。客戶端獲取連接的過程被設定為等待超時模式,即如果在 1000 毫秒內無法獲取到可用連接,將會返回給客戶端一個 null。設定連接池的大小為 10 個,然后通過調節客戶端的線程數來模擬無法獲取連接的場景

由于 java.sql.Connection 只是一個接口,最終實現是由數據庫驅動提供方來實現,考慮到本例只是演示,我們通過動態代理構造一個 Connection,該 Connection 的代理僅僅是在調用 commit() 方法時休眠 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; } } /** * 創建一個 Connection 的代理,在 commit 時休眠 100 毫秒 */ public static Connection createConnection() { return (Connection) Proxy.newProxyInstance(ConnectionDriver.class.getClassLoader(),new Class<?>[]{Connection.class}, new ConnectionHandler()); }}

接下來是線程池的實現。本例通過一個雙向隊列來維護連接,調用方需要先調用 fetchConnection(long) 方法來指定在多少毫秒內超時獲取連接,當連接使用完成后,需要調用 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) {/* 連接釋放后需要進行通知 * 這樣其他消費者就能知道連接池已經歸還了一個連接 */pool.addLast(connection);pool.notifyAll(); } } } /** * 在給定毫秒時間內獲取連接 */ public Connection fetchConnection(long mills) throws InterruptedException { synchronized (pool) { // 完全超時 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; } } }}

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

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 { // 線程數量 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 內無法獲取到,將返回 null // 分別統計獲取連接的數量 got 和未獲取到的數量 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(); } }}

筆者設置線程數量為 200 時,得出結果如下

Java 模擬數據庫連接池的實現代碼

當設置為 500 時,得出結果如下,當然具體結果根據機器性能而異

Java 模擬數據庫連接池的實現代碼

可見,隨著客戶端線程數的增加,客戶端出現超時無法獲取連接的比率不斷升高。這種等待超時模式能保證程序出問題時,線程不會一直運行,而是按時返回,并告知客戶端獲取連接出現問題。數據庫連接池的實際也可以應用到其他資源獲取的場景,針對昂貴資源的獲取都應該加以限制

到此這篇關于Java 模擬數據庫連接池的實現代碼的文章就介紹到這了,更多相關Java 數據庫連接池內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 亚洲综合色一区二区三区另类 | 免费观看一级特黄欧美大片 | 欧美亚洲91 | 欧美激情综合亚洲五月蜜桃 | 国产在线毛片 | 性欧美高清久久久久久久 | 在线观看成年人免费视频 | 国产区最新 | 看久久| 国产aⅴ片 | 那种视频在线观看 | 99在线视频精品 | 免费观看一级成人毛片 | 中文字幕99在线精品视频免费看 | 一级做a爱片特黄在线观看 一级做a爱片特黄在线观看免费看 | 97精品国产91久久久久久 | 九九毛片| 在线另类视频 | 免费一级a毛片在线 | 国产精品李雅在线观看 | 日本成人在线免费观看 | 波多野结衣视频在线观看 | 精品国产三级a | 国产精品热久久毛片 | 一本大道香蕉久在线不卡视频 | 99久久精品免费观看区一 | 国产一及片 | 夜夜骚视频 | 亚洲 欧美 日韩 丝袜 另类 | 日本无卡码免费一区二区三区 | 久久国产一片免费观看 | 日韩特黄毛片 | 国产精品观看在线亚洲人成网 | 欧美日韩永久久一区二区三区 | 日本一级毛片中文字幕 | 国产乱子精品免费视观看片 | 亚洲精品视频久久久 | 久久精品视频9 | 久久aa毛片免费播放嗯啊 | 国产欧美亚洲精品一区 | 成人免费福利片在线观看 |