Easyconnectionpool1.0

    1. <?xml version="1.0" encoding="gb2312"?>
    2. <driver xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    3.     <dbmsdriver name="華工達夢數據庫系統">
    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.     <!-- this program copyright by mengdejun -->
    13. </driver>
    package com.mdj.connectionpool;
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.util.Date;
  9. import java.util.Vector;
  10. import javax.xml.parsers.DocumentBuilder;
  11. import javax.xml.parsers.DocumentBuilderFactory;
  12. import javax.xml.parsers.ParserConfigurationException;
  13. import org.w3c.dom.Document;
  14. import org.w3c.dom.Element;
  15. import org.w3c.dom.NodeList;
  16. import org.xml.sax.SAXException;
  17. /**
  18.  * @see Connectionpool java連接池.
  19.  * @author 武漢軟件工程職業學院<br>
  20.  *         孟德軍<br>
  21.  *         2009-01-01
  22.  * @version 1.0
  23.  */
  24. public class Connectionpool {
  25.     /**
  26.      * @see #logpath 日誌文件存放的路徑.
  27.      */
  28.     private String logpath = "//server.log";
  29.     /**
  30.      * @see #log 記錄連接池異常信息.
  31.      */
  32.     private static PrintWriter log;
  33.     /**
  34.      * @see #file 日誌文件
  35.      */
  36.     private File file;
  37.     /**
  38.      * @see #driverclass 數據庫驅動類,默認mysql5.0
  39.      */
  40.     private String driverclass = "com.mysql.jdbc.Driver";
  41.     /**
  42.      * @see #url 數據庫連接地址,默認:jdbc:mysql://localhost:3306/guest
  43.      */
  44.     private String url = "jdbc:mysql://localhost:3306/guest";
  45.     /**
  46.      * @see #username 數據庫用戶名,默認:root
  47.      */
  48.     private String username = "root";
  49.     /**
  50.      * @see #password 數據庫密碼,默認:123456
  51.      */
  52.     private String password = "123456";
  53.     /**
  54.      * @see #maxconnection 連接池的最大容量,默認:10
  55.      */
  56.     private int maxconnection = 10;
  57.     /**
  58.      * @see #minconnection 連接池的最大容量,默認:5
  59.      */
  60.     private int minconnection = 5;
  61.     /**
  62.      * @see #pool 連接池容器
  63.      */
  64.     private Vector pool = null;// 用於放連接池的容器
  65.     private Connection con = null;//
  66.     private static String path = "";
  67.     /**
  68.      * @see #instance connectionpool:唯一實例,可通過getinstance獲得.
  69.      */
  70.     private static Connectionpool instance = null;
  71.     /**
  72.      * @see #Connectionpool() 私有構造方法.
  73.      */
  74.     private Connectionpool() {
  75.         init();
  76.         addConnection();
  77.     }
  78.     /**
  79.      * @see #readconfig
  80.      */
  81.     private void init() {
  82.         readconfig(path);
  83.         pool = new Vector(maxconnection);
  84.         try {
  85.             file = new File(logpath);
  86.             log = new PrintWriter(new FileWriter(file.getAbsolutePath(), true),
  87.                     true);
  88.             serverlog(new Date() + ":/t server start /n");
  89.         } catch (IOException e) {
  90.             e.printStackTrace();
  91.             serverlog(new Date() + ":/t" + e.getMessage() + "/n");
  92.         }
  93.     }
  94.     /**
  95.      * 
  96.      * @see #getinstance(String) 獲取連接池的實例
  97.      * @see #path 配置文件路徑.
  98.      * @return #Connectionpool 類的實例.
  99.      */
  100.     public synchronized static Connectionpool getinstance(String path) {
  101.         Connectionpool.path = path;
  102.         if (instance == null) {
  103.             instance = new Connectionpool();
  104.         }
  105.         return instance;
  106.     }
  107.     /**
  108.      * 
  109.      * @return 連接池中的連接.
  110.      */
  111.     public synchronized Connection getconnection() {
  112.         if (pool.size() > 0) {
  113.             con = (Connection) pool.get(0);
  114.             pool.remove(0);
  115.             checkpool(pool);
  116.             return con;
  117.         } else {
  118.             return null;
  119.         }
  120.     }
  121.     /**
  122.      * 
  123.      * @param con 用戶當前使用的連接
  124.      */
  125.     public synchronized void releaseconnection(Connection con) {
  126.         pool.add(con);
  127.     }
  128.     /**
  129.      * @see #closeconnectionpool(Connection) 關閉連接池.
  130.      * @param con 用戶當前使用的連接
  131.      * @throws SQLException
  132.      */
  133.     public synchronized void closeconnectionpool() {
  134.         for (int i = 0; i < pool.size(); i++) {
  135.             try {
  136.                 ((Connection) pool.get(i)).close();
  137.                 pool.remove(i);
  138.                 serverlog(new Date() + ": /tserver close /n");
  139.             } catch (SQLException e) {
  140.                 e.printStackTrace();
  141.                 serverlog(new Date() + ":/t" + e.getMessage() + "/n");
  142.             }
  143.         }
  144.     }
  145.     /**
  146.      * @see #addConnection() 連接池初始化,爲連接池創建連接.
  147.      */
  148.     private void addConnection() {
  149.         for (int i = 0; i < minconnection; i++) {
  150.             try {
  151.                 Class.forName(driverclass);
  152.                 con = DriverManager.getConnection(url, username, password);
  153.                 pool.add(con);
  154.             } catch (ClassNotFoundException e) {
  155.                 // TODO Auto-generated catch block
  156.                 e.printStackTrace();
  157.                 serverlog(new Date() + ":/t class not found" + e.getMessage()
  158.                         + "/n");
  159.             } catch (SQLException e) {
  160.                 e.printStackTrace();
  161.                 serverlog(new Date() + ":/t" + e.getMessage() + "/n");
  162.             }
  163.         }
  164.     }
  165.     /**
  166.      * @see #readconfig(String) 讀取配置文件.<br>
  167.      *      初始化數據庫連接數據.
  168.      * @since http://blog.csdn.net/mak0000
  169.      * <a href="http://blog.csdn.net/mak0000">更多信息</a>
  170.      * @throws path 爲配置文件路徑,文件路徑錯誤會拋出FileNotFoundException異常
  171.      */
  172.     private void readconfig(String path) {
  173.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  174.         try {
  175.             DocumentBuilder builder = factory.newDocumentBuilder();
  176.             Document document = builder.parse(Connectionpool.path);
  177.             NodeList nodelist = document.getElementsByTagName("dbmsdriver");
  178.             for (int i = 0; i < nodelist.getLength(); i++) {
  179.                 Element element = (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 (ParserConfigurationException e) {
  198.             // TODO Auto-generated catch block
  199.             e.printStackTrace();
  200.             serverlog(new Date() + ":/t" + e.getMessage() + "/n");
  201.         } catch (SAXException e) {
  202.             e.printStackTrace();
  203.             serverlog(new Date() + ":/t" + e.getMessage() + "/n");
  204.         } catch (IOException e) {
  205.             e.printStackTrace();
  206.             serverlog(new Date() + ":/t" + e.getMessage() + "/n");
  207.         }
  208.     }
  209.     /**
  210.      * @see #checkpool(Vector) 檢查連接池,並適當的爲連接池添加一定數量的連接.回收無用的連接.
  211.      * @param pool
  212.      *            連接池.
  213.      */
  214.     private void checkpool(Vector pool) {
  215.         Vector checkpool = 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.      * @param msg
  226.      *            異常信息.
  227.      */
  228.     public void serverlog(String msg) {
  229.         log.println(msg);
  230.         log.close();
  231.     }
  232. }
  1. package com.mdj.test;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import com.mdj.connectionpool.Connectionpool;
  5. public class Test {
  6.     
  7.     public Test(String path) {
  8.         Connectionpool pool = Connectionpool.getinstance(path);
  9.         //請正確修改配置文件.
  10.         Connection con = pool.getconnection();
  11.         /**
  12.         *你可以在這裏書寫的SQL語句.
  13.         *
  14.         */
  15.         //釋放連接.將連接返回給連接池。
  16.         pool.releaseconnection(con);
  17.         //關閉連接池.關閉服務器時使用.
  18.         pool.closeconnectionpool(con);
  19.     }
  20.     public static void main(String[] args) {
  21.         String path = System.getProperty("user.dir") + "//sysconfig.xml";
  22.         new Test(path);
  23.     }
  24. }

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章