Easyconnectionpool1.0

    1. <?xmlversion="1.0"encoding="gb2312"?>
    2. <driverxmlns:xsd="http://www.w3.org/2001/XMLSchema">
    3. <dbmsdrivername="华工达梦数据库系统">
    4. <driverclass>dm.jdbc.driver.DmDriver</driverclass>
    5. <url>jdbc:dm://localhost:12345/guest</url>
    6. <username>SYSDBA</username>
    7. <password>123456</password>
    8. <maxconnection>9</maxconnection>
    9. <minconnection>5</minconnection>
    10. <logpath>..//poolserver.log</logpath>
    11. </dbmsdriver>
    12. <!--thisprogramcopyrightbymengdejun-->
    13. </driver>
    package
    com.mdj.connectionpool;
  1. importjava.io.File;
  2. importjava.io.FileWriter;
  3. importjava.io.IOException;
  4. importjava.io.PrintWriter;
  5. importjava.sql.Connection;
  6. importjava.sql.DriverManager;
  7. importjava.sql.SQLException;
  8. importjava.util.Date;
  9. importjava.util.Vector;
  10. importjavax.xml.parsers.DocumentBuilder;
  11. importjavax.xml.parsers.DocumentBuilderFactory;
  12. importjavax.xml.parsers.ParserConfigurationException;
  13. importorg.w3c.dom.Document;
  14. importorg.w3c.dom.Element;
  15. importorg.w3c.dom.NodeList;
  16. importorg.xml.sax.SAXException;
  17. /**
  18. *@seeConnectionpooljava连接池.
  19. *@author武汉软件工程职业学院<br>
  20. *孟德军<br>
  21. *2009-01-01
  22. *@version1.0
  23. */
  24. publicclassConnectionpool{
  25. /**
  26. *@see#logpath日志文件存放的路径.
  27. */
  28. privateStringlogpath="//server.log";
  29. /**
  30. *@see#log记录连接池异常信息.
  31. */
  32. privatestaticPrintWriterlog;
  33. /**
  34. *@see#file日志文件
  35. */
  36. privateFilefile;
  37. /**
  38. *@see#driverclass数据库驱动类,默认mysql5.0
  39. */
  40. privateStringdriverclass="com.mysql.jdbc.Driver";
  41. /**
  42. *@see#url数据库连接地址,默认:jdbc:mysql://localhost:3306/guest
  43. */
  44. privateStringurl="jdbc:mysql://localhost:3306/guest";
  45. /**
  46. *@see#username数据库用户名,默认:root
  47. */
  48. privateStringusername="root";
  49. /**
  50. *@see#password数据库密码,默认:123456
  51. */
  52. privateStringpassword="123456";
  53. /**
  54. *@see#maxconnection连接池的最大容量,默认:10
  55. */
  56. privateintmaxconnection=10;
  57. /**
  58. *@see#minconnection连接池的最大容量,默认:5
  59. */
  60. privateintminconnection=5;
  61. /**
  62. *@see#pool连接池容器
  63. */
  64. privateVectorpool=null;//用于放连接池的容器
  65. privateConnectioncon=null;//
  66. privatestaticStringpath="";
  67. /**
  68. *@see#instanceconnectionpool:唯一实例,可通过getinstance获得.
  69. */
  70. privatestaticConnectionpoolinstance=null;
  71. /**
  72. *@see#Connectionpool()私有构造方法.
  73. */
  74. privateConnectionpool(){
  75. init();
  76. addConnection();
  77. }
  78. /**
  79. *@see#readconfig
  80. */
  81. privatevoidinit(){
  82. readconfig(path);
  83. pool=newVector(maxconnection);
  84. try{
  85. file=newFile(logpath);
  86. log=newPrintWriter(newFileWriter(file.getAbsolutePath(),true),
  87. true);
  88. serverlog(newDate()+":/tserverstart/n");
  89. }catch(IOExceptione){
  90. e.printStackTrace();
  91. serverlog(newDate()+":/t"+e.getMessage()+"/n");
  92. }
  93. }
  94. /**
  95. *
  96. *@see#getinstance(String)获取连接池的实例
  97. *@see#path配置文件路径.
  98. *@return#Connectionpool类的实例.
  99. */
  100. publicsynchronizedstaticConnectionpoolgetinstance(Stringpath){
  101. Connectionpool.path=path;
  102. if(instance==null){
  103. instance=newConnectionpool();
  104. }
  105. returninstance;
  106. }
  107. /**
  108. *
  109. *@return连接池中的连接.
  110. */
  111. publicsynchronizedConnectiongetconnection(){
  112. if(pool.size()>0){
  113. con=(Connection)pool.get(0);
  114. pool.remove(0);
  115. checkpool(pool);
  116. returncon;
  117. }else{
  118. returnnull;
  119. }
  120. }
  121. /**
  122. *
  123. *@paramcon用户当前使用的连接
  124. */
  125. publicsynchronizedvoidreleaseconnection(Connectioncon){
  126. pool.add(con);
  127. }
  128. /**
  129. *@see#closeconnectionpool(Connection)关闭连接池.
  130. *@paramcon用户当前使用的连接
  131. *@throwsSQLException
  132. */
  133. publicsynchronizedvoidcloseconnectionpool(){
  134. for(inti=0;i<pool.size();i++){
  135. try{
  136. ((Connection)pool.get(i)).close();
  137. pool.remove(i);
  138. serverlog(newDate()+":/tserverclose/n");
  139. }catch(SQLExceptione){
  140. e.printStackTrace();
  141. serverlog(newDate()+":/t"+e.getMessage()+"/n");
  142. }
  143. }
  144. }
  145. /**
  146. *@see#addConnection()连接池初始化,为连接池创建连接.
  147. */
  148. privatevoidaddConnection(){
  149. for(inti=0;i<minconnection;i++){
  150. try{
  151. Class.forName(driverclass);
  152. con=DriverManager.getConnection(url,username,password);
  153. pool.add(con);
  154. }catch(ClassNotFoundExceptione){
  155. //TODOAuto-generatedcatchblock
  156. e.printStackTrace();
  157. serverlog(newDate()+":/tclassnotfound"+e.getMessage()
  158. +"/n");
  159. }catch(SQLExceptione){
  160. e.printStackTrace();
  161. serverlog(newDate()+":/t"+e.getMessage()+"/n");
  162. }
  163. }
  164. }
  165. /**
  166. *@see#readconfig(String)读取配置文件.<br>
  167. *初始化数据库连接数据.
  168. *@sincehttp://blog.csdn.net/mak0000
  169. *<ahref="http://blog.csdn.net/mak0000">更多信息</a>
  170. *@throwspath为配置文件路径,文件路径错误会抛出FileNotFoundException异常
  171. */
  172. privatevoidreadconfig(Stringpath){
  173. DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
  174. try{
  175. DocumentBuilderbuilder=factory.newDocumentBuilder();
  176. Documentdocument=builder.parse(Connectionpool.path);
  177. NodeListnodelist=document.getElementsByTagName("dbmsdriver");
  178. for(inti=0;i<nodelist.getLength();i++){
  179. Elementelement=(Element)nodelist.item(i);
  180. driverclass=element.getElementsByTagName("driverclass").item(
  181. 0).getFirstChild().getNodeValue();
  182. url=element.getElementsByTagName("url").item(0)
  183. .getFirstChild().getNodeValue();
  184. username=element.getElementsByTagName("username").item(0)
  185. .getFirstChild().getNodeValue();
  186. password=element.getElementsByTagName("password").item(0)
  187. .getFirstChild().getNodeValue();
  188. maxconnection=Integer
  189. .parseInt(element.getElementsByTagName("maxconnection")
  190. .item(0).getFirstChild().getNodeValue());
  191. minconnection=Integer
  192. .parseInt(element.getElementsByTagName("minconnection")
  193. .item(0).getFirstChild().getNodeValue());
  194. logpath=element.getElementsByTagName("logpath").item(0)
  195. .getFirstChild().getNodeValue();
  196. }
  197. }catch(ParserConfigurationExceptione){
  198. //TODOAuto-generatedcatchblock
  199. e.printStackTrace();
  200. serverlog(newDate()+":/t"+e.getMessage()+"/n");
  201. }catch(SAXExceptione){
  202. e.printStackTrace();
  203. serverlog(newDate()+":/t"+e.getMessage()+"/n");
  204. }catch(IOExceptione){
  205. e.printStackTrace();
  206. serverlog(newDate()+":/t"+e.getMessage()+"/n");
  207. }
  208. }
  209. /**
  210. *@see#checkpool(Vector)检查连接池,并适当的为连接池添加一定数量的连接.回收无用的连接.
  211. *@parampool
  212. *连接池.
  213. */
  214. privatevoidcheckpool(Vectorpool){
  215. Vectorcheckpool=null;
  216. checkpool=pool;
  217. if(checkpool.size()<minconnection){
  218. while(pool.size()<maxconnection){
  219. addConnection();
  220. }
  221. }
  222. }
  223. /**
  224. *@see#serverlog(String)记录连接池运行信息.
  225. *@parammsg
  226. *异常信息.
  227. */
  228. publicvoidserverlog(Stringmsg){
  229. log.println(msg);
  230. log.close();
  231. }
  232. }
  1. packagecom.mdj.test;
  2. importjava.sql.Connection;
  3. importjava.sql.SQLException;
  4. importcom.mdj.connectionpool.Connectionpool;
  5. publicclassTest{
  6. publicTest(Stringpath){
  7. Connectionpoolpool=Connectionpool.getinstance(path);
  8. //请正确修改配置文件.
  9. Connectioncon=pool.getconnection();
  10. /**
  11. *你可以在这里书写的SQL语句.
  12. *
  13. */
  14. //释放连接.将连接返回给连接池。
  15. pool.releaseconnection(con);
  16. //关闭连接池.关闭服务器时使用.
  17. pool.closeconnectionpool(con);
  18. }
  19. publicstaticvoidmain(String[]args){
  20. Stringpath=System.getProperty("user.dir")+"//sysconfig.xml";
  21. newTest(path);
  22. }
  23. }

你可能感兴趣的:(Connection)