(java)数据库连接池

来源:互联网 发布:淘宝代运营公司的套路 编辑:程序博客网 时间:2024/06/05 16:04
  1. ************************配置文件**************************** 
  2. <?xml version="1.0" encoding="gb2312"?> 
  3. <driver xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  4. <userdriver name="华工达梦数据库系统"
  5.   <driverclass>dm.jdbc.driver.DmDriver</driverclass> 
  6.   <url>jdbc:dm://localhost:12345/guest</url> 
  7.   <username>SYSDBA</username> 
  8.   <password>123456</password> 
  9.   <maxconnection>9</maxconnection> 
  10.   <minconnection>5</minconnection> 
  11. </userdriver> 
  12. <!-- this program copyright by mengdejun --> 
  13. </driver> 
  14. ************************************************************ 
  15. Connectionpool.java 
  16. author:孟德军 
  17. ************************************************************ 
  18. package com.mdj.connectionpool; 
  19. import java.io.IOException; 
  20. import java.sql.Connection; 
  21. import java.sql.DriverManager; 
  22. import java.sql.SQLException; 
  23. import java.util.ArrayList; 
  24. import javax.xml.parsers.DocumentBuilder; 
  25. import javax.xml.parsers.DocumentBuilderFactory; 
  26. import javax.xml.parsers.ParserConfigurationException; 
  27. import org.w3c.dom.Document; 
  28. import org.w3c.dom.Element; 
  29. import org.w3c.dom.NodeList; 
  30. import org.xml.sax.SAXException; 
  31. public class Connectionpool { 
  32. private String driverclass = "com.mysql.jdbc.Driver";// 数据库驱动类 
  33. private String username = "root";// 数据库用户名 
  34. private String password = "123456";// 数据库密码 
  35. private String url = "jdbc:mysql://localhost:3306/guest";// url 
  36. private int maxconnection = 10;// 连接池的最大容量 
  37. private int minconnection = 5;// 连接池的初始容量 
  38. private ArrayList pool = null;// 用于放连接池的容器 
  39. private Connection con = null;// 
  40. private static String path = ""
  41. private static Connectionpool instance = null
  42. // 将构造方法私有法,禁止外部通过new语句创建实例.private修饰的构造方法不能被实例化,而只能通过一个方法来返回他的实例. 
  43. private Connectionpool() { 
  44.   init(); 
  45.   addConnection(); 
  46. private void init() { 
  47.   readconfig(); 
  48.   pool = new ArrayList(maxconnection); 
  49. // 创建一个外部实例,通过getinstance方法创建实例. 
  50. public synchronized static Connectionpool getinstance() { 
  51.   if (instance == null) { 
  52.    instance = new Connectionpool(); 
  53.   } 
  54.   return instance; 
  55. // 获取连接池中的连接. 
  56. public synchronized Connection getconnection() { 
  57.   if (pool.size() > 0) { 
  58.    con = (Connection) pool.get(0); 
  59.    pool.remove(0); 
  60.    return con; 
  61.   } else { 
  62.    return null
  63.   } 
  64. public synchronized void releaseconnection(Connection con) { 
  65.   pool.add(con); 
  66. public synchronized void closeconnectionpool(Connection con) { 
  67.   for (int i = 0; i < pool.size(); i++) { 
  68.    try { 
  69.     ((Connection) pool.get(i)).close(); 
  70.     pool.remove(i); 
  71.    } catch (SQLException e) { 
  72.     e.printStackTrace(); 
  73.    } 
  74.   } 
  75. // 向连接池中添加初始连接. 
  76. private void addConnection() { 
  77.   for (int i = 0; i < minconnection; i++) { 
  78.    try { 
  79.     Class.forName(driverclass); 
  80.     con = DriverManager.getConnection(url, username, password); 
  81.     pool.add(con); 
  82.    } catch (ClassNotFoundException e) { 
  83.     e.printStackTrace(); 
  84.    } catch (SQLException e) { 
  85.     e.printStackTrace(); 
  86.    } 
  87.   } 
  88. // 读取配置文件. 
  89. private void readconfig() { 
  90.   path=System.getProperty("user.dir")+ "//sysconfig.xml"
  91.   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
  92.   try { 
  93.    DocumentBuilder builder = factory.newDocumentBuilder(); 
  94.    Document document = builder.parse(path); 
  95.    NodeList nodelist = document.getElementsByTagName("userdriver"); 
  96.    for (int i = 0; i < nodelist.getLength(); i++) { 
  97.     Element element = (Element) nodelist.item(i); 
  98.     driverclass = element.getElementsByTagName("driverclass").item( 
  99.       0).getFirstChild().getNodeValue(); 
  100.     url = element.getElementsByTagName("url").item(0
  101.       .getFirstChild().getNodeValue(); 
  102.     username = element.getElementsByTagName("username").item(0
  103.       .getFirstChild().getNodeValue(); 
  104.     password = element.getElementsByTagName("password").item(0
  105.       .getFirstChild().getNodeValue(); 
  106.     maxconnection = Integer 
  107.       .parseInt(element.getElementsByTagName("maxconnection"
  108.         .item(0).getFirstChild().getNodeValue()); 
  109.     minconnection = Integer 
  110.       .parseInt(element.getElementsByTagName("minconnection"
  111.         .item(0).getFirstChild().getNodeValue()); 
  112.    } 
  113.   } catch (ParserConfigurationException e) { 
  114.    e.printStackTrace(); 
  115.   } catch (SAXException e) { 
  116.    e.printStackTrace(); 
  117.   } catch (IOException e) { 
  118.    e.printStackTrace(); 
  119.   } 
  120. ************************************************************ 
  121. ConnectionpoolTest.java 
  122. author:孟德军 
  123. ************************************************************ 
  124. package com.mdj.test; 
  125. import java.sql.Connection; 
  126. import java.sql.DriverManager; 
  127. import java.sql.ResultSet; 
  128. import java.sql.SQLException; 
  129. import java.sql.Statement; 
  130. import com.mdj.pool.ConnectionPool; 
  131. public class Test { 
  132. public static void main(String[] args) { 
  133.   ConnectionPool pool; 
  134.   Connection con; 
  135.   long start = System.currentTimeMillis(); 
  136.   String sql="select * from userinfo"
  137.   for (int i = 0; i < 100; i++) { 
  138.    pool = ConnectionPool.getinstance(); 
  139.    con = pool.getconnection(); 
  140.    try { 
  141.     Statement statement = con.createStatement(); 
  142.     ResultSet result = statement 
  143.       .executeQuery(sql); 
  144.     while (result.next()) { 
  145.     } 
  146.     result.close(); 
  147.     statement.close(); 
  148.     pool.release(con); 
  149.    } catch (SQLException e) { 
  150.     // TODO Auto-generated catch block 
  151.     e.printStackTrace(); 
  152.    } 
  153.   }System.out.println("连接池连接"); 
  154.   System.out.println(System.currentTimeMillis() - start+"ms"); 
  155.   long start1=System.currentTimeMillis(); 
  156.   for(int i=0;i<100;i++){ 
  157.   try { 
  158.    Class.forName("com.mysql.jdbc.Driver"); 
  159.    Connection con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); 
  160.    Statement stm1=con1.createStatement(); 
  161.    ResultSet result=stm1.executeQuery(sql); 
  162.    while(result.next()){ 
  163.    } 
  164.    result.close(); 
  165.    stm1.close(); 
  166.    con1.close(); 
  167.   } catch (ClassNotFoundException e) { 
  168.    e.printStackTrace(); 
  169.   } catch(SQLException e){ 
  170.    e.printStackTrace(); 
  171.   } 
  172.   } 
  173.   System.out.println("普通连接"); 
  174.   System.out.println(System.currentTimeMillis()-start1+"ms"); 
  175. 当连接池连接和普通连接执行一百次时,结果如下: 
  176. 连接池连接:1723ms 
  177. 普通连接:19041ms 
  178. ******************************************************************************************* 
  179. 效果很明显,这仅仅是我写的一个连接池,诸多因素没有考虑,你可以到网上下载连接池产品.poolman等等. 
  180. 其实国产数据库系统还是蛮好的,支持国产!
原创粉丝点击