ConnectionPool 类提供一个 getConnection() 方法。系统中提供一个ConnectionPool的静态实现,然后所有需要连接数据的地方都通过 ConPool.getConnection() 返回一个Connection对象的实例的引用。然后该怎么用就怎么用,用完之后使用ConnectionPool的回收方法回收,供其它饮用者使用。这里有一篇文章讲这方面的一些基础和设计知识,不妨看看:http://www-900.ibm.com/developerWorks/cn/java/l-pool/index.shtml
----------------------------------------------------
服务器端程序开发中池的概念和使用   
赵晨希池(Pool)的概念池的描述和定义:Pool(池)的概念被广泛的应用在服务器端软件的开发上。使用池结构可以明显的提高你的应用程序的速度,改善效率和降低系统资源的开销。所以在现在的应用服务器端的开发中池的设计和实现是开发工作中的重要一环。那么到底什么是池呢?我们可以简单的想象一下应用运行时的环境,当大量的客户并发的访问应用服务器时我们如何提供服务呢?我们可以为每一个客户提供一个新的服务对象进行服务这种方法看起来简单,在实际应用中如果采用这种实现会有很多问题,显而易见的是不断的创建和销毁新服务对象必将给造成系统资源的巨大开销,导致系统的性能下降。针对这个问题我们采用池的方式。池可以想象成就是一个容器保存着各种我们需要的对象。我们对这些对象进行复用,从而提高系统性能。从结构上看,它应该具有容器对象和具体的元素对象。从使用方法上看,我们可以直接取得池中的元素来用,也可以把我们要做的任务交给它处理。所以从目的上看池应该有两种类型,一种是用于处理客户提交的任务的,我们通常用Thread Pool(线程池)来描述它,另一种是客户从池中获取有关的对象进行使用,我们通常用 Resource Pool(资源池)来描述它。它们可以分别解决不同的问题。以下结合具体的应用进行介绍。... ...
----------------------------------------------------
另外,有很多开方源代码的java项目很值得一看。例如 jive 里面有很多Cache和Pool的例子写得很不错。

解决方案 »

  1.   

    唉,同步加锁很头疼,遇到 java+oracle 一大堆效率问题。不知道是不是我们技术比较弱的原因,我和同伴都要烦死了。
    相比之下,我还是喜欢 SQLServer + COM
      

  2.   

    /**
     * DbConnectionDefaultPool.java
     * July 23, 2000
     *
     * Copyright (C) 2000 CoolServlets.com. All rights reserved.
     *
     * ===================================================================
     * The Apache Software License, Version 1.1
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in
     *    the documentation and/or other materials provided with the
     *    distribution. *
     * 3. The end-user documentation included with the redistribution,
     *    if any, must include the following acknowledgment:
     *       "This product includes software developed by
     *        CoolServlets.com (http://www.coolservlets.com)."
     *    Alternately, this acknowledgment may appear in the software itself,
     *    if and wherever such third-party acknowledgments normally appear.
     *
     * 4. The names "Jive" and "CoolServlets.com" must not be used to
     *    endorse or promote products derived from this software without
     *    prior written permission. For written permission, please
     *    contact [email protected].
     *
     * 5. Products derived from this software may not be called "Jive",
     *    nor may "Jive" appear in their name, without prior written
     *    permission of CoolServlets.com.
     *
     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED.  IN NO EVENT SHALL COOLSERVLETS.COM OR
     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     * SUCH DAMAGE.
     * ====================================================================
     *
     * This software consists of voluntary contributions made by many
     * individuals on behalf of CoolServlets.com. For more information
     * on CoolServlets.com, please see <http://www.coolservlets.com>.
     */package com.jive.forum.database;import java.sql.*;
    import java.util.*;
    import java.io.*;
    import java.text.*;
    import java.util.Date;
    import com.xiaozhu.forum.*;/**
     * Default Jive connection provider. It uses the excellent connection pool
     * available from http://www.javaexchange.com. This connection provider is a
     * a good choice unless you can use a container-managed one.
     */
    public class DbConnectionDefaultPool extends DbConnectionProvider {    private static final String NAME = "Default Connection Pool";
        private static final String DESCRIPTION = "The default connection provider "
            + "that uses the connection pool from javaexchange.com. It works with "
            + "almost any database setup, is customizable, and offers good performance. "
            + "Use this connection provider unless you have your own or can use a "
            + "container managed connection pool.";
        private static final String AUTHOR = "CoolServlets.com";
        private static final int MAJOR_VERSION = 1;
        private static final int MINOR_VERSION = 0;
        private static final boolean POOLED = true;    private ConnectionPool connectionPool = null;
        private Properties props;
        private Properties propDescriptions;    private Object initLock = new Object();    public DbConnectionDefaultPool() {
            //this.manager = manager;
            props = new Properties();
            propDescriptions = new Properties();
            //Initialize all property values
            initializeProperties();
            //Load any existing property values
            loadProperties();
        }    /**
         * Returns a database connection.
         */
        public Connection getConnection() {
            if (connectionPool == null) {
                //block until the init has been done
                synchronized(initLock) {
                    //if still null, something has gone wrong
                    if (connectionPool == null) {
                        System.err.println("Warning: DbConnectionDefaultPool.getConnection() was " +
                        "called when the internal pool has not been initialized.");
                        return null;
                    }
                }
            }
            return new ConnectionWrapper(connectionPool.getConnection(), connectionPool);
        }
      

  3.   

    /**
         * Starts the pool.
         */
        protected void start() {
            //acquire lock so that no connections can be returned.
            synchronized (initLock) {
                //Get properties
                String driver = props.getProperty("driver");
                String server = props.getProperty("server");
                String username = props.getProperty("username");
                String password = props.getProperty("password");
                int minConnections = 0, maxConnections = 0;
                double connectionTimeout = 0.0;
                try {
                    minConnections = Integer.parseInt(props.getProperty("minConnections"));
                    maxConnections = Integer.parseInt(props.getProperty("maxConnections"));
                    connectionTimeout = Double.parseDouble(props.getProperty("connectionTimeout"));
                }
                catch (Exception e) {
                    System.err.println("Error: could not parse default pool properties. " +
                        "Make sure the values exist and are correct.");
                    e.printStackTrace();
                    return;
                }
                String logPath = props.getProperty("logPath");            try {
                    connectionPool = new ConnectionPool(driver, server, username, password,
                        minConnections, maxConnections, logPath, connectionTimeout);
                }
                catch (IOException ioe) {
                    System.err.println("Error starting DbConnectionDefaultPool: " + ioe);
                    ioe.printStackTrace();
                }
            }
        }    /**
         * Restarts the pool to take into account any property changes.
         */
        protected void restart() {
            //Kill off pool.
            destroy();
            //Reload properties.
            loadProperties();
            //Start a new pool.
            start();
        }    /**
         * Destroys the connection pool.
         */
        protected void destroy() {
            if (connectionPool != null) {
                try {
                    connectionPool.destroy(1);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //Release reference to connectionPool
            connectionPool = null;
        }    /**
         * Returns the value of a property of the connection provider.
         *
         * @param name the name of the property.
         * @returns the value of the property.
         */
        public String getProperty(String name) {
            return (String)props.get(name);
        }    /**
         * Returns the description of a property of the connection provider.
         *
         * @param name the name of the property.
         * @return the description of the property.
         */
        public String getPropertyDescription(String name) {
            return (String)propDescriptions.get(name);
        }    /**
         * Returns an enumeration of the property names for the connection provider.
         */
        public Enumeration propertyNames() {
            return props.propertyNames();
        }    /**
         * Sets a property of the connection provider. Each provider has a set number
         * of properties that are determined by the author. Trying to set a non-
         * existant property will result in an IllegalArgumentException. 
         *
         * @param name the name of the property to set.
         * @param value the new value for the property.
         *
         */
        public void setProperty(String name, String value) {
            props.put(name, value);
            saveProperties();
        }    /**
         * Give default values to all the properties and descriptions.
         */
        private void initializeProperties() {
            props.put("driver","");
            props.put("server","");
            props.put("username","");
            props.put("password","");
            props.put("minConnections","");
            props.put("maxConnections","");
            props.put("logPath","");
            props.put("connectionTimeout","");        propDescriptions.put("driver","JDBC driver. e.g. 'oracle.jdbc.driver.OracleDriver'");
            propDescriptions.put("server","JDBC connect string. e.g. 'jdbc:oracle:thin:@203.92.21.109:1526:orcl'");
            propDescriptions.put("username","Database username. e.g. 'Scott'");
            propDescriptions.put("password","Database password. e.g. 'Tiger'");
            propDescriptions.put("minConnections","Minimum # of connections to start with in pool. Three is the recommended minimum");
            propDescriptions.put("maxConnections","Maximum # of connections in dynamic pool. Fifteen should give good performance for an average load.");
            propDescriptions.put("logPath","Absolute path name for log file. e.g. 'c:\\logs\\jiveDbLog.log'");
            propDescriptions.put("connectionTimeout","Time in days between connection resets. e.g. '.5'");
        }    /**
         * Load whatever properties that already exist.
         */
        private void loadProperties() {
            String driver = PropertyManager.getProperty("DbConnectionDefaultPool.driver");
            String server = PropertyManager.getProperty("DbConnectionDefaultPool.server");
            String username = PropertyManager.getProperty("DbConnectionDefaultPool.username");
            String password = PropertyManager.getProperty("DbConnectionDefaultPool.password");
            String minConnections = PropertyManager.getProperty("DbConnectionDefaultPool.minConnections");
            String maxConnections = PropertyManager.getProperty("DbConnectionDefaultPool.maxConnections");
            String logPath = PropertyManager.getProperty("DbConnectionDefaultPool.logPath");
            String connectionTimeout = PropertyManager.getProperty("DbConnectionDefaultPool.connectionTimeout");        if (driver != null) {  props.setProperty("driver", driver);  }
            if (server != null) {  props.setProperty("server", server);  }
            if (username != null) {  props.setProperty("username", username);  }
            if (password != null) {  props.setProperty("password", password);  }
            if (minConnections != null) {  props.setProperty("minConnections", minConnections);  }
            if (maxConnections != null) {  props.setProperty("maxConnections", maxConnections);  }
            if (logPath != null) {  props.setProperty("logPath", logPath);  }
            if (connectionTimeout != null) {  props.setProperty("connectionTimeout", connectionTimeout);  }
        }    private void saveProperties() {
            PropertyManager.setProperty("DbConnectionDefaultPool.driver", props.getProperty("driver"));
            PropertyManager.setProperty("DbConnectionDefaultPool.server", props.getProperty("server"));
            PropertyManager.setProperty("DbConnectionDefaultPool.username", props.getProperty("username"));
            PropertyManager.setProperty("DbConnectionDefaultPool.password", props.getProperty("password"));
            PropertyManager.setProperty("DbConnectionDefaultPool.minConnections", props.getProperty("minConnections"));
            PropertyManager.setProperty("DbConnectionDefaultPool.maxConnections", props.getProperty("maxConnections"));
            PropertyManager.setProperty("DbConnectionDefaultPool.logPath", props.getProperty("logPath"));
            PropertyManager.setProperty("DbConnectionDefaultPool.connectionTimeout", props.getProperty("connectionTimeout"));
        }
      

  4.   

    自己做一个吧,以前有很多的帖子
    google上也可以搜索到很多的