<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Edzy_Java

      BlogJava :: 首頁 ::  ::  ::  :: 管理 ::
      58 隨筆 :: 12 文章 :: 11 評論 :: 0 Trackbacks
    關鍵字: Java, JDBC, Connection Pool, Database, 數據庫連接池, sourcecode

    ??????? 雖然 J2EE 程序員一般都有現成的應用服務器所帶的JDBC 數據庫連接池,不過對于開發一般的 Java Application 、 Applet 或者 JSP、velocity 時,我們可用的JDBC 數據庫連接池并不多,并且一般性能都不好。
    ??????? Java 程序員都很羨慕 Windows ADO ,只需要 new Connection 就可以直接從數據庫連接池中返回 Connection。并且 ADO Connection 是線程安全的,多個線程可以共用一個 Connection, 所以 ASP 程序一般都把 getConnection 放在 Global.asa 文件中,在 IIS 啟動時建立數據庫連接。ADO 的 Connection 和 Result 都有很好的緩沖,并且很容易使用。

    ??????? 其實我們可以自己寫一個JDBC 數據庫連接池。寫 JDBC connection pool 的注意事項有:

    1. 有一個簡單的函數從連接池中得到一個 Connection。
    2. close 函數必須將 connection 放回 數據庫連接池。
    3. 當數據庫連接池中沒有空閑的 connection, 數據庫連接池必須能夠自動增加 connection 個數。
    4. 當數據庫連接池中的 connection 個數在某一個特別的時間變得很大,但是以后很長時間只用其中一小部分,應該可以自動將多余的 connection 關閉掉。
    5. 如果可能,應該提供debug 信息報告沒有關閉的 new Connection 。

    ??????? 如果要 new Connection 就可以直接從數據庫連接池中返回 Connection, 可以這樣寫( Mediator pattern ) (以下代碼中使用了中文全角空格):

    public class EasyConnection implements java.sql.Connection{
    private Connection m_delegate = null;
    public EasyConnection(){
    m_delegate = getConnectionFromPool();
    }
    public void close(){
    putConnectionBackToPool(m_delegate);
    }
    public PreparedStatement prepareStatement(String sql) throws SQLException{
    m_delegate.prepareStatement(sql);
    }
    //...... other method
    }

    ??????? 看來并不難。不過不建議這種寫法,因為應該盡量避免使用 Java Interface, 關于 Java Interface 的缺點我另外再寫文章討論。大家關注的是 Connection Pool 的實現方法。下面給出一種實現方法。

    package connectionpool;

    import java.sql.*;
    import java.lang.reflect.*;
    import java.util.*;
    import java.io.*;

    public class SimpleConnetionPool {
    ?private static LinkedList<Object> m_notUsedConnection = new LinkedList<Object>();
    ?private static HashSet<Object> m_usedUsedConnection = new HashSet<Object>();
    ?private static String m_url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=campusblog";
    ?private static String m_user = "sa";
    ?private static String m_password = "sa";
    ?static final boolean DEBUG = true;
    ?static private long m_lastClearClosedConnection = System.currentTimeMillis();
    ?public static long CHECK_CLOSED_CONNECTION_TIME = 4 * 60 * 60 * 1000; //4 hours

    ?static {
    ??initDriver();
    ??}

    ?private SimpleConnetionPool() {?
    ?}

    ?private static void initDriver() {
    ??Driver driver = null;
    ??//load mysql driver
    ??try {
    ???driver = (Driver) Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    ???installDriver(driver);
    ??}
    ??catch (Exception e) {}
    ? ////////////////////////////////////////////////////////////////////////////////////////////////////////////////選擇使用
    ??//load postgresql driver
    ??//try {
    ???//driver = (Driver) Class.forName("org.postgresql.Driver").newInstance();
    ???//installDriver(driver);
    ??//}
    ??//catch (Exception e){}
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////選擇使用

    ?}

    ?public static void installDriver(Driver driver) {
    ??try {
    ???DriverManager.registerDriver(driver);
    ??}
    ??catch (Exception e) {
    ???e.printStackTrace();
    ??}
    ?}


    ?public static synchronized Connection getConnection() {
    ??clearClosedConnection();
    ??while (m_notUsedConnection.size() > 0) {
    ???try{
    ????ConnectionWrapper wrapper = (ConnectionWrapper) m_notUsedConnection.removeFirst();
    ????if (wrapper.connection.isClosed()) {
    ?????continue;
    ????}
    ????m_usedUsedConnection.add(wrapper);
    ????if(DEBUG){
    ?????wrapper.debugInfo = new Throwable("Connection initial statement");
    ????}
    ???return wrapper.connection;
    ???}catch (Exception e){}
    ??}
    ??int newCount = getIncreasingConnectionCount();
    ??LinkedList<Object> list = new LinkedList<Object>();
    ??ConnectionWrapper wrapper = null;
    ??for (int i = 0; i < newCount; i++) {
    ???wrapper = getNewConnection();
    ???if (wrapper != null) {
    ????list.add(wrapper);
    ???}
    ??}
    ??if (list.size() == 0) {
    ???return null;
    ??}
    ??wrapper = (ConnectionWrapper) list.removeFirst();
    ??m_usedUsedConnection.add(wrapper);
    ??m_notUsedConnection.addAll(list);
    ??list.clear();
    ??return wrapper.connection;
    ?}
    ?
    ?private static ConnectionWrapper getNewConnection() {
    ??try{
    ???Connection con = DriverManager.getConnection(m_url, m_user, m_password);
    ???ConnectionWrapper wrapper = new ConnectionWrapper(con);
    ???return wrapper;
    ??}
    ??catch (Exception e) {
    ???e.printStackTrace();
    ??}
    ??return null;
    ?}
    ?
    ?static synchronized void pushConnectionBackToPool(ConnectionWrapper con) {
    ??boolean exist = m_usedUsedConnection.remove(con);
    ??if (exist) {
    ???m_notUsedConnection.addLast(con);
    ??}
    ?}
    ?
    ?public static int close() {
    ??int count = 0;
    ??Iterator iterator = m_notUsedConnection.iterator();
    ??while (iterator.hasNext()) {
    ???try{
    ????((ConnectionWrapper) iterator.next()).close();
    ????count++;
    ???}
    ???catch (Exception e) {}
    ??}
    ??m_notUsedConnection.clear();
    ??iterator = m_usedUsedConnection.iterator();
    ??while (iterator.hasNext()) {
    ???try{
    ????ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next();
    ????wrapper.close();
    ????if (DEBUG) {
    ?????wrapper.debugInfo.printStackTrace();
    ????}
    ????count++;
    ???}catch (Exception e){}
    ??}
    ??m_usedUsedConnection.clear();
    ??return count;
    ?}
    ?
    ?private static void clearClosedConnection() {
    ??long time = System.currentTimeMillis();
    ??//sometimes user change system time,just return
    ??if (time < m_lastClearClosedConnection) {
    ???time = m_lastClearClosedConnection;
    ???return;
    ??}
    ??//no need check very often
    ??if (time - m_lastClearClosedConnection < CHECK_CLOSED_CONNECTION_TIME) {
    ???return;
    ???}
    ??m_lastClearClosedConnection = time;
    ??//begin check
    ??Iterator iterator = m_notUsedConnection.iterator();
    ??while (iterator.hasNext()) {
    ???ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next();
    ???try {
    ????if (wrapper.connection.isClosed()) {
    ?????iterator.remove();
    ????}
    ????} catch (Exception e) {
    ?????iterator.remove();
    ?????if (DEBUG) {
    ??????System.out.println("connection is closed, this connection initial StackTrace");
    ??????wrapper.debugInfo.printStackTrace();
    ?????}
    ????}
    ???}
    ??//make connection pool size smaller if too big
    ??int decrease = getDecreasingConnectionCount();
    ??if (m_notUsedConnection.size() < decrease) {
    ???return;
    ???}
    ??while (decrease-- > 0) {
    ???ConnectionWrapper wrapper = (ConnectionWrapper) m_notUsedConnection.removeFirst();
    ???try {
    ????wrapper.connection.close();
    ????} catch (Exception e){}
    ???}
    ??}
    ?
    ?/**
    ? * * get increasing connection count, not just add 1 connection
    ? * * @return count
    ? * */
    ?public static int getIncreasingConnectionCount(){
    ??int count = 1;
    ??int current = getConnectionCount();
    ??count = current / 4;
    ??if (count < 1) {
    ???count = 1;
    ???}
    ??return count;
    ?}
    ?
    ?/**
    ? * * get decreasing connection count, not just remove 1 connection
    ? * * @return count
    ? * */
    ?public static int getDecreasingConnectionCount(){
    ??//int count = 0;
    ??int current = getConnectionCount();
    ??if (current < 10){
    ???return 0;
    ??}
    ??return current / 3;
    ?}
    ?
    ?public synchronized static void printDebugMsg(){
    ??printDebugMsg(System.out);
    ?}
    ?
    ?public synchronized static void printDebugMsg(PrintStream out){
    ??if (DEBUG == false) {
    ???return;
    ??}
    ??StringBuffer msg = new StringBuffer();
    ??msg.append("debug message in " + SimpleConnetionPool.class.getName());
    ??msg.append("\r\n");
    ??msg.append("total count is connection pool: " + getConnectionCount());
    ??msg.append("\r\n");
    ??msg.append("not used connection count: " + getNotUsedConnectionCount());
    ??msg.append("\r\n");
    ??msg.append("used connection, count: " + getUsedConnectionCount());
    ??out.println(msg);
    ??Iterator iterator = m_usedUsedConnection.iterator();
    ??while (iterator.hasNext()){
    ???ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next();
    ???wrapper.debugInfo.printStackTrace(out);
    ??}
    ??out.println();
    ?}
    ?
    ?public static synchronized int getNotUsedConnectionCount(){
    ??return m_notUsedConnection.size();
    ?}
    ?
    ?public static synchronized int getUsedConnectionCount(){
    ??return m_usedUsedConnection.size();
    ?}
    ?
    ?public static synchronized int getConnectionCount(){
    ??return m_notUsedConnection.size() + m_usedUsedConnection.size();
    ?}
    }

    class ConnectionWrapper implements InvocationHandler{
    ?private final static String CLOSE_METHOD_NAME = "close";
    ?public Connection connection = null;
    ?private Connection m_originConnection = null;
    ?public long lastAccessTime = System.currentTimeMillis();
    ?Throwable debugInfo = new Throwable("Connection initial statement");
    ?
    ?ConnectionWrapper(Connection con){
    ??Class[] interfaces = {java.sql.Connection.class};
    ??this.connection = (Connection) Proxy.newProxyInstance(con.getClass().getClassLoader(),interfaces, this);
    ??m_originConnection = con;
    ?}
    ?
    ?void close() throws SQLException {
    ??m_originConnection.close();
    ?}
    ?
    ?public Object invoke(Object proxy, Method m, Object[] args) throws Throwable{
    ??Object obj = null;
    ??if (CLOSE_METHOD_NAME.equals(m.getName())) {
    ???SimpleConnetionPool.pushConnectionBackToPool(this);
    ??}
    ??else {
    ???obj = m.invoke(m_originConnection, args);
    ??}
    ??lastAccessTime = System.currentTimeMillis();
    ??return obj;
    ?}
    }

    ??????? 使用方法

    public class TestConnectionPool{
      public static void main(String[] args) {
        SimpleConnetionPool.setUrl(DBTools.getDatabaseUrl());
        SimpleConnetionPool.setUser(DBTools.getDatabaseUserName());
        SimpleConnetionPool.setPassword(DBTools.getDatabasePassword());

        Connection con = SimpleConnetionPool.getConnection();
        Connection con1 = SimpleConnetionPool.getConnection();
        Connection con2 = SimpleConnetionPool.getConnection();

        //do something with con ...

        try {
          con.close();
        } catch (Exception e) {}

        try {
          con1.close();
        } catch (Exception e) {}

        try {
          con2.close();
        } catch (Exception e) {}

        con = SimpleConnetionPool.getConnection();
        con1 = SimpleConnetionPool.getConnection();
        try {
          con1.close();
        } catch (Exception e) {}

        con2 = SimpleConnetionPool.getConnection();
        SimpleConnetionPool.printDebugMsg();

      }
    }

    posted on 2006-11-15 17:09 lbfeng 閱讀(308) 評論(0)  編輯  收藏 所屬分類: JSP&Servlet技術
    主站蜘蛛池模板: 精品国产麻豆免费人成网站| 亚洲欧好州第一的日产suv| 一级毛片aaaaaa视频免费看| 国产伦精品一区二区三区免费迷| 亚洲一欧洲中文字幕在线| 国产免费看JIZZ视频| 亚洲成aⅴ人片在线观| 国产在线观看片a免费观看| 亚洲三级在线观看| 性做久久久久免费观看| 国产亚洲美女精品久久久久| av无码东京热亚洲男人的天堂| 深夜福利在线免费观看| 亚洲成av人片在线观看无码不卡| 日韩精品无码免费专区午夜不卡| 国产亚洲精品a在线无码| 免费无码中文字幕A级毛片| 亚洲午夜电影在线观看高清 | 亚洲欧洲在线观看| 亚洲成人免费电影| 亚洲人成人伊人成综合网无码| 国产免费观看视频| 亚欧国产一级在线免费| 内射干少妇亚洲69XXX| 好先生在线观看免费播放| 国产亚洲Av综合人人澡精品| 最新亚洲成av人免费看| 9277手机在线视频观看免费| 亚洲精品又粗又大又爽A片| 亚洲精品一级无码中文字幕| 无码成A毛片免费| 亚洲欧美日韩国产精品一区| 国产aⅴ无码专区亚洲av麻豆| 91精品全国免费观看含羞草| 亚洲色无码国产精品网站可下载| 亚洲人成人网站在线观看| 1区2区3区产品乱码免费| 免费高清A级毛片在线播放| 久久亚洲私人国产精品vA| 免费欧洲美女牲交视频| **俄罗斯毛片免费|