線程池談談我本身的理解吧: html
針對以上的理解,在編程中能夠這樣實現: java
下面是主要實現代碼 sql
public Connection getConnection() throws SQLException { synchronized(MysqlUtils.class) { while(list.size() <= 0 ) { try { log.info(Thread.currentThread().getName() + " wait() " ); MysqlUtils.class.wait(); } catch (InterruptedException e) { log.error(" Thread " + Thread.currentThread().getName() + " wait() error!"); } } final Connection con = list.removeFirst(); Connection conn = (Connection) Proxy.newProxyInstance(MysqlUtils.class.getClassLoader() , new Class[]{Connection.class} ,new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(!method.getName().equals("close")) { return method.invoke(con, args); } else { synchronized(MysqlUtils.class) { list.add(con); MysqlUtils.class.notifyAll(); log.info(Thread.currentThread().getName() + " release connection "); return null; } } } }); return conn; } }
池子的最大鏈接,最少鏈接,定時之類的不少都沒有實現,只是簡單的弄了一個固定鏈接數量 數據庫
本篇博客參考了http://www.cnblogs.com/xdp-gacl/p/4002804.html 博客的代碼,特此指出,也算是我本身的總結 編程
以上有什麼錯誤的地方,歡迎你們指出,目前也在學習中,一塊兒進步哈 網絡
12樓:frankiegao123 發表於 2012-01-21 13:14 回覆此評論 一個能夠用於生產環境中的鏈接池最少須要具有如下條件: 1:實現了 javax.sql.DataSource 接口 2:從鏈接池中得到的鏈接,在調用 Connection#close 時並非真正意義上的關閉鏈接,而是將其還回到池中去 3:池中的鏈接被數據庫服務端關閉時,這些鏈接該如何處理 4:鏈接回收問題。若是設了最大 50 個鏈接,在併發量高時正好用到這 50 個鏈接,若是高併發期事後,假如只有 10 個鏈接被用到,那剩下的 40 個應該要被回收,不能佔用這些寶貴的資源 5:若是網絡中斷後又從新鏈接上,池中的鏈接該如何處理,這與 3 是相似的,涉及一個池中鏈接的健康檢查機制