生活随笔
收集整理的這篇文章主要介紹了
java 数据库连接池
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
數(shù)據(jù)庫連接池,是一種相當實用的應(yīng)用程序。它可以保存、維護及創(chuàng)建用戶所需的數(shù)據(jù)庫連接。從而使得用戶得到一個連接的時間降低90%以上。大大提升了數(shù)據(jù)庫訪問的反應(yīng)時間。
?
這個是一個開源的代碼。大家可以修改它、使用它。
?
希望我的代碼能對大家有用。
?
此代碼,經(jīng)過1000數(shù)量級的多線程并發(fā)訪問測試。在四核CPU下也進行了多線程測試。保證了連接池在真多線程上同步訪問的安全性。
里面包含了一個公開的接口。使用這個接口里面的函數(shù)。可以輕易創(chuàng)建使用數(shù)據(jù)庫連接池服務(wù)。
使用一個守護線程維護這個連接池,完全自動化。
?
下載連接:http://download.csdn.net/user/lauo1188
?
?
連接池接口類:
?
[java]?view plaincopy
package?mysql;??import?java.sql.*;??import?java.sql.ResultSet;??public?interface?Pool??{??????public?boolean?start(String?dbname,String?user,String?psw);??????????????public?boolean?start(int?lows,int?maxs,int?maxc,String?dbname,String?user,String?psw);??????public?Connection?getConnection();????????public?boolean?freeConnection(Connection?con);??????public?boolean?close();???}?? ?
?
?
第一個實現(xiàn)類:VectorPool
?
?
[java]?view plaincopy
??????????package?mysql;??import?mysql.*;??import?java.sql.*;??import?java.util.*;??????public?class?VectorPool?extends?Thread?implements?Pool???{??????protected?final?boolean?debug?=?false;???????protected?boolean?init?=?false;????????????protected?final?long?timeDiff?=??60*60*1000?;??????protected?int?lows?=?20?;???????protected?int?maxs?=?100?;???????protected?int?maxc?=?100;??????protected?String?dbname?=?null;??????protected?String?user?=?null;??????protected?String?psw?=?null;????????????protected?Integer?conCount?=?0;?????????????protected?Vector<ConInfo>?pool?=?new?Vector<ConInfo>();??????public?VectorPool()???????{??????}??????public?void?start()??????{????????????????}????????????protected?int?getConCount()??????{??????????return?conCount;??????}??????????????public??synchronized??boolean?start(String?db,String?u,String?p)??????{??????????if(false?==?init?&&?null?!=?pool)??????????{??????????????dbname??=???db;??????????????user????=???u;??????????????psw?????=???p;??????????????if(test())??????????????{??????????????????init????=???true;??????????????????super.start();???????????????????return?true;??????????????}??????????????else?return?false;??????????}??????????else??????????????return?false;??????}??????public?synchronized?boolean?start(int?l,int?m,int?mc,String?db,String?u,String?p)??????{??????????if(l<0?||?m<0?||l>=m?||?m>mc)??????????????return?false;??????????if(false?==?init?&&?null?!=?pool)??????????{??????????????dbname??=???db;??????????????user????=???u;??????????????psw?????=???p;??????????????if(test())??????????????{??????????????????init????=???true;??????????????????lows?=?l<5?5:l;????????????????????maxs?=?m<10?10:m;???????????????????maxc?=?mc<20??20:mc;??????????????????super.start();???????????????????return?true;??????????????}??????????????else???????????????????return?false;??????????}??????????else??????????????return?false;??????}??????????????public?synchronized?Connection?getConnection()??????{????????????????????if(init)??????????{??????????????ConInfo?c?=?getOneConnection();??????????????return?c!=null?c.con:null;??????????}??????????else??????????????return?null;??????}??????????????protected?synchronized?ConInfo?getOneConnection()??????{??????????if(pool.size()!=0)??????????{??????????????return?pool.remove(0);??????????}??????????else???????????{??????????????if(debug)?System.out.println("new?connection!");??????????????return?newConInfo();??????????}??????}??????????????public?synchronized?boolean?freeConnection(Connection?con)??????{??????????if(init==false?||?con?==?null?)??????????????return?false;??????????ConInfo?c?=??new?ConInfo(con);??????????pool.addElement(c);??????????return?true;??????}???????????????public?synchronized??boolean?close()??????{??????????if(init)??????????{??????????????init?=?false;??????????????for(int?i?=?0;?i?<pool.size();i++)??????????????????pool.get(i).close();??????????????pool.clear();??????????????pool????=???null;??????????????dbname??=???null;??????????????user????=???null;??????????????psw?????=???null;??????????????return?true;??????????}??????????else???????????????return?false;??????}???????????????????????public?void?run()??????{??????????final?long?sleepTime?=?2*1000??;???????????final?long?refreshTime?=?10*60*1000;???????????final?long?lowsCheckTime?=?2*1000;??????????final?long?maxsCheckTime?=?60*1000;??????????long?loop?=?0;??????????while(true)??????????{??????????????if(loop%refreshTime?==?0)??????????????{??????????????????if(debug)?System.out.println("call?refresh:"+pool.size());??????????????????refreshByTimeDiff();??????????????}??????????????if(loop%maxsCheckTime?==?0)??????????????{??????????????????if(debug)?System.out.println("call?toMaxs:"+pool.size());??????????????????toMaxs();??????????????}??????????????if(loop%lowsCheckTime?==?0)??????????????{??????????????????if(debug)?System.out.println("conCnt:"+conCount+"?call?toLows:"+pool.size());??????????????????toLows();??????????????}??????????????loop?+=?sleepTime;??????????????try{??????????????????sleep(sleepTime);??????????????}??????????????catch(Exception?e){??????????????????e.printStackTrace();??????????????????stop();??????????????}????????????????????????}????????????????}??????????????protected?void?toLows()??????{??????????int?size?=?pool.size();???????????????????????for(;size<lows;size++)??????????{??????????????if(debug)?System.out.println("toLows?is?running!");??????????????ConInfo?c?=??newConInfo();??????????????if(c!=null)??????????????????synchronized(pool)??????????????????{??????????????????????pool.addElement(c);??????????????????}??????????}??????}??????????????protected?void?toMaxs()??????{??????????int?size?=?pool.size();??????????for(;size>maxs;size--)??????????{??????????????if(debug)?System.out.println("toMaxs?is?running!");??????????????try{??????????????????getConnection().close();??????????????????synchronized(conCount)??????????????????{??????????????????????conCount?--;??????????????????}??????????????}??????????????catch(Exception?e)??????????????{??????????????????e.printStackTrace();??????????????}??????????}??????}???????????????public?void?refreshByTimeDiff()??????{??????????Calendar?nowTime?=?Calendar.getInstance();??????????long?now?=?nowTime.getTimeInMillis();??????????for(int?i=0;i<pool.size();i++)??????????{??????????????ConInfo?c?=?getOneConnection();??????????????if(now??-?c.lastTime.getTimeInMillis()?>=timeDiff)??????????????{??????????????????if(debug)?System.out.println("refresh?the?pool!,,,,,,,,");??????????????????synchronized(pool)??????????????????{??????????????????????pool.remove(i).close();??????????????????}??????????????}??????????}??????}??????????????protected?boolean?test()??????{??????????int?cnt?=?10;??????????Connection?c;??????????do{??????????????c?=?newConnection();??????????????cnt?--?;??????????}while(c==null?&&?cnt>=0);??????????if(c!=null)??????????{??????????????(new?ConInfo(c)).close();??????????????return?true;??????????}??????????else??????????????return?false;????????????????????}????????????????class?ConInfo??????{??????????protected?Calendar?lastTime;??????????????protected?Connection?con?=?null;??????????????public?ConInfo(Connection?c)??????????{??????????????con?=???c;??????????????lastTime?=?Calendar.getInstance();??????????}??????????public?synchronized?void?close()??????????{??????????????try??????????????{??????????????????if(con!=null)??????????????????????con.close();??????????????????synchronized(conCount)??????????????????{??????????????????????conCount-=con!=null?1:0;??????????????????}??????????????????lastTime?=?null;??????????????}??????????????catch(Exception?e)??????????????{??????????????????e.printStackTrace();??????????????}??????????}??????}??????????????protected??Connection?newConnection()??????{??????????try??????????{??????????????Class.forName("com.mysql.jdbc.Driver");??????????????int?cnt?=?0;??????????????Connection?c?=?null;??????????????synchronized(conCount)??????????????{??????????????????if(conCount<=maxc)??????????????????do??????????????????{??????????????????????try{??????????????????????c?=?DriverManager.getConnection(dbname,user,psw);??????????????????????}catch(Exception?es){c=null;if(debug)?System.out.println("create?new?connection?error!");}??????????????????????cnt++;??????????????????}while(null?==?c??&&?cnt<3);??????????????????conCount+=c!=null?1:0;??????????????}??????????????return?c;??????????}??????????catch(Exception?e)??????????{??????????????e.printStackTrace();??????????????return?null;??????????}??????}??????????????protected?ConInfo?newConInfo()??????{??????????Connection?c?=?newConnection();??????????if(c==null)??????????????return?null;??????????return?new?ConInfo(c);??????}????????????????public?static?void?main(String[]?argvs)??????{??????????VectorPool?pool?=?new?VectorPool();??????????Connection?con?=?null;??????????boolean?flag?=?pool.start("jdbc:mysql://localhost/finance","root","");??????????int?cnt?=?10000;??????????long?t0??=?Calendar.getInstance().getTimeInMillis();????????????????????final?boolean?tflag?=?true?;????????????????????while(flag){????????????????????????con?=?tflag???pool.getConnection():pool.newConnection();??????????????try??????????????{?????????????????????Statement?stm?=?con.createStatement();??????????????????ResultSet?rev?=?stm.executeQuery("select?*?from?user");??????????????????while(false?&&?rev!=null?&&?rev.next())??????????????????{??????????????????????System.out.println(rev.getString("id"));??????????????????}??????????????????stm.close();??????????????}??????????????catch(Exception?e)??????????????{??????????????????e.printStackTrace();????????????????????????????????}????????????????????????if(tflag)??????????????????pool.freeConnection(con);??????????????try{??????????????????if(!tflag)??????????????????????con.close();??????????????}??????????????catch(Exception?exx)??????????????{??????????????????exx.printStackTrace();??????????????}????????????????????????????????????????????????????????????if(--cnt<0)??????????????????break;??????????}??????????System.out.println("used?time:"+(Calendar.getInstance().getTimeInMillis()-t0));??????}??}????????????????????????????????????? ?
?
?
第二個實現(xiàn)類:ListPool
?
?
[java]?view plaincopy
????????package?mysql;??import?mysql.*;??import?java.sql.*;??import?java.util.*;??????public?class?ListPool?extends?Thread?implements?Pool???{??????protected?final?boolean?debug?=?false;???????protected?boolean?init?=?false;????????????protected?final?long?timeDiff?=??60*60*1000?;??????protected?int?lows?=?20?;???????protected?int?maxs?=?100?;???????protected?int?maxc?=?100;??????protected?String?dbname?=?null;??????protected?String?user?=?null;??????protected?String?psw?=?null;????????????protected?Integer?conCount?=?0;?????????????protected?LinkedList<ConInfo>?pool?=?new?LinkedList<ConInfo>();??????public?ListPool()???????{??????}??????public?void?start()??????{????????????????}????????????protected?int?getConCount()??????{??????????return?conCount;??????}??????????????public??synchronized??boolean?start(String?db,String?u,String?p)??????{??????????if(false?==?init?&&?null?!=?pool)??????????{??????????????dbname??=???db;??????????????user????=???u;??????????????psw?????=???p;??????????????if(test())??????????????{??????????????????init????=???true;??????????????????super.start();???????????????????return?true;??????????????}??????????????else?return?false;??????????}??????????else??????????????return?false;??????}??????public?synchronized?boolean?start(int?l,int?m,int?mc,String?db,String?u,String?p)??????{??????????if(l<0?||?m<0?||l>=m?||?m>mc)??????????????return?false;??????????if(false?==?init?&&?null?!=?pool)??????????{??????????????dbname??=???db;??????????????user????=???u;??????????????psw?????=???p;??????????????if(test())??????????????{??????????????????init????=???true;??????????????????lows?=?l<5?5:l;????????????????????maxs?=?m<10?10:m;???????????????????maxc?=?mc<20??20:mc;??????????????????super.start();???????????????????return?true;??????????????}??????????????else???????????????????return?false;??????????}??????????else??????????????return?false;??????}??????????????public?synchronized?Connection?getConnection()??????{????????????????????if(init)??????????{??????????????ConInfo?c?=?getOneConnection();??????????????return?c!=null?c.con:null;??????????}??????????else??????????????return?null;??????}??????????????protected?synchronized?ConInfo?getOneConnection()??????{??????????if(pool.size()!=0)??????????{??????????????return?pool.remove(0);??????????}??????????else???????????{??????????????if(debug)?System.out.println("new?connection!");??????????????return?newConInfo();??????????}??????}??????????????public?synchronized?boolean?freeConnection(Connection?con)??????{??????????if(init==false?||?con?==?null?)??????????????return?false;??????????ConInfo?c?=??new?ConInfo(con);??????????pool.add(c);??????????return?true;??????}???????????????public?synchronized??boolean?close()??????{??????????if(init)??????????{??????????????init?=?false;??????????????for(int?i?=?0;?i?<pool.size();i++)??????????????????pool.get(i).close();??????????????pool.clear();??????????????pool????=???null;??????????????dbname??=???null;??????????????user????=???null;??????????????psw?????=???null;??????????????return?true;??????????}??????????else???????????????return?false;??????}???????????????????????public?void?run()??????{??????????final?long?sleepTime?=?2*1000??;???????????final?long?refreshTime?=?10*60*1000;???????????final?long?lowsCheckTime?=?2*1000;??????????final?long?maxsCheckTime?=?60*1000;??????????long?loop?=?0;??????????while(true)??????????{??????????????if(loop%refreshTime?==?0)??????????????{??????????????????if(debug)?System.out.println("call?refresh:"+pool.size());??????????????????refreshByTimeDiff();??????????????}??????????????if(loop%maxsCheckTime?==?0)??????????????{??????????????????if(debug)?System.out.println("call?toMaxs:"+pool.size());??????????????????toMaxs();??????????????}??????????????if(loop%lowsCheckTime?==?0)??????????????{??????????????????if(debug)?System.out.println("conCnt:"+conCount+"?call?toLows:"+pool.size());??????????????????toLows();??????????????}??????????????loop?+=?sleepTime;??????????????try{??????????????????sleep(sleepTime);??????????????}??????????????catch(Exception?e){??????????????????e.printStackTrace();??????????????????stop();??????????????}????????????????????????}????????????????}??????????????protected?void?toLows()??????{??????????int?size?=?pool.size();???????????????????????for(;size<lows;size++)??????????{??????????????if(debug)?System.out.println("toLows?is?running!");??????????????ConInfo?c?=??newConInfo();??????????????if(c!=null)??????????????????synchronized(pool)??????????????????{??????????????????????pool.add(c);??????????????????}??????????}??????}??????????????protected?void?toMaxs()??????{??????????int?size?=?pool.size();??????????for(;size>maxs;size--)??????????{??????????????if(debug)?System.out.println("toMaxs?is?running!");??????????????try{??????????????????getConnection().close();??????????????????synchronized(conCount)??????????????????{??????????????????????conCount?--;??????????????????}??????????????}??????????????catch(Exception?e)??????????????{??????????????????e.printStackTrace();??????????????}??????????}??????}???????????????public?void?refreshByTimeDiff()??????{??????????Calendar?nowTime?=?Calendar.getInstance();??????????long?now?=?nowTime.getTimeInMillis();??????????for(int?i=0;i<pool.size();i++)??????????{??????????????ConInfo?c?=?getOneConnection();??????????????if(now??-?c.lastTime.getTimeInMillis()?>=timeDiff)??????????????{??????????????????if(debug)?System.out.println("refresh?the?pool!,,,,,,,,");??????????????????synchronized(pool)??????????????????{??????????????????????pool.remove(i).close();??????????????????}??????????????}??????????}??????}??????????????protected?boolean?test()??????{??????????int?cnt?=?10;??????????Connection?c;??????????do{??????????????c?=?newConnection();??????????????cnt?--?;??????????}while(c==null?&&?cnt>=0);??????????if(c!=null)??????????{??????????????(new?ConInfo(c)).close();??????????????return?true;??????????}??????????else??????????????return?false;????????????????????}????????????????class?ConInfo??????{??????????protected?Calendar?lastTime;??????????????protected?Connection?con?=?null;??????????????public?ConInfo(Connection?c)??????????{??????????????con?=???c;??????????????lastTime?=?Calendar.getInstance();??????????}??????????public?synchronized?void?close()??????????{??????????????try??????????????{??????????????????if(con!=null)??????????????????????con.close();??????????????????synchronized(conCount)??????????????????{??????????????????????conCount-=con!=null?1:0;??????????????????}??????????????????lastTime?=?null;??????????????}??????????????catch(Exception?e)??????????????{??????????????????e.printStackTrace();??????????????}??????????}??????}??????????????protected??Connection?newConnection()??????{??????????try??????????{??????????????Class.forName("com.mysql.jdbc.Driver");??????????????int?cnt?=?0;??????????????Connection?c?=?null;??????????????synchronized(conCount)??????????????{??????????????????if(conCount<=maxc)??????????????????do??????????????????{??????????????????????try{??????????????????????c?=?DriverManager.getConnection(dbname,user,psw);??????????????????????}catch(Exception?es){c=null;if(debug)?System.out.println("create?new?connection?error!");}??????????????????????cnt++;??????????????????}while(null?==?c??&&?cnt<3);??????????????????conCount+=c!=null?1:0;??????????????}??????????????return?c;??????????}??????????catch(Exception?e)??????????{??????????????e.printStackTrace();??????????????return?null;??????????}??????}??????????????protected?ConInfo?newConInfo()??????{??????????Connection?c?=?newConnection();??????????if(c==null)??????????????return?null;??????????return?new?ConInfo(c);??????}????????????????public?static?void?main(String[]?argvs)??????{??????????ListPool?pool?=?new?ListPool();??????????Connection?con?=?null;??????????boolean?flag?=?pool.start("jdbc:mysql://localhost/finance","root","");??????????int?cnt?=?10000;??????????long?t0??=?Calendar.getInstance().getTimeInMillis();????????????????????final?boolean?tflag?=?true?;????????????????????while(flag){????????????????????????con?=?tflag???pool.getConnection():pool.newConnection();??????????????try??????????????{?????????????????????Statement?stm?=?con.createStatement();??????????????????ResultSet?rev?=?stm.executeQuery("select?*?from?user");??????????????????while(false?&&?rev!=null?&&?rev.next())??????????????????{??????????????????????System.out.println(rev.getString("id"));??????????????????}??????????????????stm.close();??????????????}??????????????catch(Exception?e)??????????????{??????????????????e.printStackTrace();????????????????????????????????}????????????????????????if(tflag)??????????????????pool.freeConnection(con);??????????????try{??????????????????if(!tflag)??????????????????????con.close();??????????????}??????????????catch(Exception?exx)??????????????{??????????????????exx.printStackTrace();??????????????}????????????????????????????????????????????????????????if(--cnt<0)??????????????????break;??????????}??????????System.out.println("used?time:"+(Calendar.getInstance().getTimeInMillis()-t0));??????}??}??????????????????????????? ?
?
?
?
?
一個測試類:testPool
?
[java]?view plaincopy
????????package??mysql;??import?java.sql.*;??import?java.util.*;??import?mysql.*;??public?class?testPool?extends?Thread??{??????protected??String?dbname?=?null,??????????????????????????user?=?null,??????????????????????????psw??=?null;??????Pool?pool=?null;??????boolean?flag?=?true;??????public?testPool(Pool?p,boolean?f,String?d,String?u,String?pw)???????{??????????pool?=?p;??????????flag?=?f;??????????dbname?=?d;??????????user?=?u;??????????psw?=?pw;??????}??????public?void?start(int?n)??????{??????????while(n-->0)??????????{??????????????super.start();??????????}??????}??????public?void?run()??????{??????????long?id?=?this.getId();??????????Connection?con?=?null;??????????int?cnt?=?100;??????????long?t0??=?Calendar.getInstance().getTimeInMillis();????????????????????final?boolean?tflag?=?true?;????????????????????while(flag){????????????????????????con?=?tflag???pool.getConnection():newConnection();??????????????if(con?==?null)?continue;??????????????try??????????????{?????????????????????Statement?stm?=?con.createStatement();??????????????????ResultSet?rev?=?stm.executeQuery("select?*?from?user");??????????????????while(false?&&?rev!=null?&&?rev.next())??????????????????{??????????????????????System.out.println(rev.getString("id"));??????????????????}??????????????????stm.close();??????????????}??????????????catch(Exception?e)??????????????{??????????????????e.printStackTrace();????????????????????????????????}????????????????????????if(tflag)??????????????????pool.freeConnection(con);??????????????try{??????????????????if(!tflag)??????????????????????con.close();??????????????}??????????????catch(Exception?exx)??????????????{??????????????????exx.printStackTrace();??????????????}???????????????????????????????????????????????????????????if(--cnt<0)??????????????????break;??????????}??????????System.out.println("used?time:"+(Calendar.getInstance().getTimeInMillis()-t0));??????}??????public?Connection?newConnection()??????{??????????try??????????{??????????????Class.forName("com.mysql.jdbc.Driver");??????????????int?cnt?=?0;??????????????Connection?c?=?null;??????????????do??????????????{??????????????????c?=?DriverManager.getConnection(dbname,user,psw);??????????????????cnt++;??????????????}while(null?==?c?&&?cnt<15);??????????????if(null?==?c)??????????????????return?null;??????????????else??????????????????return?c;??????????}??????????catch(Exception?e)??????????{??????????????e.printStackTrace();??????????????return?null;??????????}??????}??????public?static?void?main(String?argv[])??????{??????????Pool?pool?=?null;??????????boolean?who?=?true;??????????pool?=?who???new?VectorPool()?:?new?ListPool();??????????boolean?flag?=?pool.start(20,30,100,"jdbc:mysql://localhost/finance","root","");??????????int?cnt?=?100;??????????while(cnt-->0)??????????{??????????????testPool?p?=?new?testPool(pool,flag,"jdbc:mysql://localhost/finance","root","");??????????????p.start();??????????}?????????????}????????}????????????????????????????????????????????????????? ?
轉(zhuǎn)載于:https://www.cnblogs.com/jiushini/archive/2012/06/05/2535595.html
總結(jié)
以上是生活随笔為你收集整理的java 数据库连接池的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。