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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

连接池实现原理

發(fā)布時(shí)間:2025/4/9 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 连接池实现原理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
自己寫一個(gè)線程池 ? public class OraclePool { ??????private static volatile OraclePool pool; ??????private OracleDataSource ds; ??????private Map<Connection,Boolean> map; ?????? ??????private String url= "jdbc:oracle:thin:@127.0.0.1:1521:orcl" ; ??????private String username= "lmdc461"; ??????private String password= "lmdc"; ??????private int initPoolSize=10; ??????private int maxPoolSize=200; ??????private int waitTime=100; ?????? ??????public OraclePool(){ ????????????init(); ??????} ?????? ??????public static OraclePool getInstance(){ ???????????? if ( pool== null) { ?????????????????? synchronized (OraclePool. class) { ???????????????????????? pool= new OraclePool(); ??????????????????} ????????????} ???????????? return pool; ??????} ?????? ??????private void init(){ ???????????? try { ?????????????????? ds= new OracleDataSource(); ?????????????????? ds.setURL( url); ?????????????????? ds.setUser( username); ?????????????????? ds.setPassword( password); ?????????????????? //ds.set ?????????????????? ds.setLoginTimeout(2000); ???????????? ?????????????????? map= new HashMap<Connection, Boolean>(); ?????????????????? for ( int i = 0; i < initPoolSize; i++) { ???????????????????????? map.put(getNewConnection(), true); ??????????????????} ?????????????????? ????????????} catch (Exception e) { ?????????????????? e.printStackTrace(); ????????????} ??????} ?????? ??????public Connection getNewConnection(){ ???????????? try { ?????????????????? return ds.getConnection(); ????????????} catch (Exception e) { ?????????????????? e.printStackTrace(); ????????????} ???????????? return null; ??????} ?????? ??????public synchronized Connection getConnection(){ ????????????Connection conn= null; ???????????? try { ?????????????????? for (Entry<Connection,Boolean> entry : map.entrySet()) { ???????????????????????? if ( entry.getValue()) { ?????????????????????????????? conn= entry.getKey(); ?????????????????????????????? map.put( entry.getKey(), false); ?????????????????????????????? break; ????????????????????????} ??????????????????} ?????????????????? if ( conn== null) { ???????????????????????? if ( map.size()< maxPoolSize) { ?????????????????????????????? conn=getNewConnection(); ?????????????????????????????? map.put( conn, false); ????????????????????????} else{ ??????????????????????????????wait( waitTime); ?????????????????????????????? conn=getConnection(); ????????????????????????} ??????????????????} ?????????????????? ????????????} catch (Exception e) { ?????????????????? e.printStackTrace(); ????????????} ???????????? return conn; ??????} ?????? ??????public void releaseConnection(Connection conn){ ???????????? if ( conn== null) { ?????????????????? return ; ????????????} ???????????? try { ?????????????????? if ( map.containsKey( conn)) { ???????????????????????? if ( conn.isClosed()) { ?????????????????????????????? map.remove( conn); ????????????????????????} else{ ?????????????????????????????? if (! conn.getAutoCommit()) { ???????????????????????????????????? conn.setAutoCommit( true); ??????????????????????????????} ?????????????????????????????? map.put( conn, true); ????????????????????????} ??????????????????} else{ ???????????????????????? conn.close(); ??????????????????} ????????????} catch (Exception e) { ?????????????????? e.printStackTrace(); ????????????} ??????} } 使用: ? public class TestOraclePool { ??????private static volatile int a; ?????? ??????private synchronized static void incr(){ ???????????? a++; ??????} ?????? ??????public static void main(String[] args) throws InterruptedException{ ???????????? int times=10; ????????????System. out.println( "start``````````"); ???????????? long start=System. currentTimeMillis(); ???????????? for ( int i = 0; i < times; i++) { ?????????????????? new Thread( new Runnable(){ ? ???????????????????????? @Override ???????????????????????? public void run() { ??????????????????????????????OraclePool pool= new OraclePool(); ??????????????????????????????Connection conn= pool.getConnection(); ??????????????????????????????Statement stmt= null; ??????????????????????????????ResultSet rs= null; ?????????????????????????????? try { ???????????????????????????????????? stmt= conn.createStatement(); ???????????????????????????????????? rs= stmt.executeQuery( "select * from gd_user"); ???????????????????????????????????? while ( rs.next()) { ??????????????????????????????????????????System. out.println( "recordid:"+ rs.getString( "recordid")+ ?????????????????????????????????????????????????????? ";username:"+ rs.getString( "username"));??????????????????????????????????????? ????????????????????????????????????} ??????????????????????????????} catch (Exception e) { ???????????????????????????????????? e.printStackTrace(); ??????????????????????????????} finally{ ???????????????????????????????????? incr(); ???????????????????????????????????? if ( rs!= null) { ?????????????????????????????????????????? try { ???????????????????????????????????????????????? rs.close(); ??????????????????????????????????????????} catch (Exception e2) { ???????????????????????????????????????????????? // TODO: handle exception ??????????????????????????????????????????} ????????????????????????????????????} ???????????????????????????????????? if ( stmt!= null) { ?????????????????????????????????????????? try { ???????????????????????????????????????????????? stmt.close(); ??????????????????????????????????????????} catch (Exception e2) { ???????????????????????????????????????????????? e2.printStackTrace(); ??????????????????????????????????????????} ????????????????????????????????????} ???????????????????????????????????? pool.releaseConnection( conn); ??????????????????????????????} ????????????????????????} ???????????????????????? ??????????????????}).start(); ?????????????????? ?????????????????? ????????????} ???????????? ???????????? while ( true){ ?????????????????? if ( a== times) { ????????????????????????System. out.println( "finished,time:"+ ????????????????????????????????????(System. currentTimeMillis()-start)); ???????????????????????? break; ??????????????????}????? ??????????????????Thread. sleep(1000); ????????????} ??????} }

轉(zhuǎn)載于:https://www.cnblogs.com/wanglao/p/5329702.html

總結(jié)

以上是生活随笔為你收集整理的连接池实现原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。