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

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

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

    隨筆-314  評論-209  文章-0  trackbacks-0

    這幾天花了點時間弄了個 db4o 連接池,比較簡單,連接池原型是論壇上面的一篇文章。很簡單,歡迎拍磚。

    從 servlet 開始,在這里初始化連接池:

    package  com;

    import  java.io.File;
    import  java.util.Enumeration;

    import  javax.servlet.ServletConfig;
    import  javax.servlet.ServletException;
    import  javax.servlet.http.HttpServlet;


    public   class  ConnectionPollServlet  extends  HttpServlet {
        
        
    private   static   final  String XML_FILE_PROPERTY  =   " xmlFile " ;
        
        
    /**
         * servlet init
         
    */
        
    public   void  init(ServletConfig servletConfig)  throws  ServletException{
            
    super .init(servletConfig);
            String appDir 
    =  servletConfig.getServletContext().getRealPath( " / " );
            Enumeration names 
    =  servletConfig.getInitParameterNames();
            
    while (names.hasMoreElements()){
                String name 
    =  (String) names.nextElement();
                String value 
    =  servletConfig.getInitParameter(name);
                
    if  (name.equals(XML_FILE_PROPERTY)) {
                    File file 
    =   new  File(value);
                    
    if  (file.isAbsolute()) {
                        XMLReader.configure(value);
                    } 
    else  {
                        XMLReader.configure(appDir 
    +  File.separator  +  value);
                    }
                }
            }
        }

        
    /**
         * servlet destroy
         
    */
        
    public   void  destroy() {
            
    super .destroy();
            ConnectionPoll.destroy();
        }
    }

    然后是 XML 解析類:
    package com;

    import java.io.File;

    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;

    public class XMLReader {

        
    /**
         * parse XML file
         * 
    @param xmlFileName
         
    */
        
    public static void configure(String xmlFileName) {
            
    try {
                File file 
    = new File(xmlFileName);
                SAXReader reader 
    = new SAXReader();
                Document doc 
    = reader.read(file);
                Element root 
    = doc.getRootElement();

                String fileName 
    = file.getParent()+"\\"
                    
    +root.elementText("fileName");
                String sport 
    = root.elementText("port");
                String sminConn 
    = root.elementText("minConn");
                String sidelTime 
    = root.elementText("idelTime");

                
    int port = Integer.parseInt(sport);
                
    int minConn = Integer.parseInt(sminConn);
                
    int idelTime = Integer.parseInt(sidelTime);

                ConnectionPoll.init(fileName,port,minConn,idelTime);
            } 
    catch (DocumentException e) {
                e.printStackTrace();
            }
        }
    }


    連接池類:
    package com;

    import java.util.concurrent.ConcurrentLinkedQueue;

    import com.db4o.Db4o;
    import com.db4o.ObjectContainer;
    import com.db4o.ObjectServer;

    public class ConnectionPoll {

        
    private static int idelTime;
        
        
    private static ConcurrentLinkedQueue<ObjectContainer> connectionQueue;
        
        
    private ConnectionPoll(){
        }
        
        
    /**
         * init pool
         
    */
        
    protected static void init(String fileName,int port,int minConn,int it) {
            idelTime
    =it;
            ObjectServer objectServer 
    = Db4o.openServer(fileName,port);
            connectionQueue 
    = new ConcurrentLinkedQueue<ObjectContainer>();
            
    for (int i = 0; i<minConn; i++) {
                connectionQueue.offer(objectServer.openClient());
            }
        }

        
    /**
         * get connection
         * 
    @return ObjectContainer
         * 
    @throws ConnectionTimeoutException 
         * 
    @throws InterruptedException
         
    */
        
    public static synchronized ObjectContainer getConnection() throws ConnectionTimeoutException{
            
    long expiration = System.currentTimeMillis() + idelTime;
            
    while (connectionQueue.isEmpty())
            {
                
    if (expiration < System.currentTimeMillis())
                {
                    
    throw new ConnectionTimeoutException("connection timeout!");
                }
            }
            ObjectContainer objectContainer 
    = connectionQueue.poll();
            
    return objectContainer;
            }

        
    /**
         * release connection
         * 
    @return ObjectContainer
         * 
    @throws InterruptedException
         
    */
        
    public static synchronized void releaseConnection(ObjectContainer objectContainer) {
            connectionQueue.offer(objectContainer);
        }
        
        
    /**
         * destroy connection
         *
         
    */
        
    protected static void destroy() {
            
    while (connectionQueue.iterator().hasNext()){
                ObjectContainer objectContainer 
    = connectionQueue.poll();
                objectContainer.close();
            }
        }
    }

    超時異常類:
    package com;

    public class ConnectionTimeoutException extends Exception{

        
    public ConnectionTimeoutException()
        {
        }

        
    public ConnectionTimeoutException(String s)
        {
            
    super(s);
        }
    }

    XML 配置文件,從上到下依次是,數據庫文件名、端口、初始連接數、等待時間:
    <?xml version="1.0" encoding="utf-8"?>
      
    <config>
        
    <fileName>auto.yap</fileName>
        
    <port>1010</port>
        
    <minConn>10</minConn>
        
    <idelTime>1000</idelTime>
      
    </config>

    web.xml 用于初始化的時候加載:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
      
    <servlet>
        
    <servlet-name>ConnectionPoll</servlet-name>
        
    <servlet-class>com.ConnectionPollServlet</servlet-class>
          
    <init-param>
            
    <param-name>xmlFile</param-name>
            
    <param-value>WEB-INF/poolConfig.xml</param-value>
          
    </init-param>
        
    <load-on-startup>1</load-on-startup>
      
    </servlet>
    </web-app>


    數據庫文件和參數配置文件都放在 WEB-INF 文件夾下。這個連接池還未實現 maxConn(最大連接數)和對多數據庫文件的支持以及日志等。

    posted on 2006-09-28 17:19 xzc 閱讀(265) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲国产精品13p| ZZIJZZIJ亚洲日本少妇JIZJIZ| 免费网站看v片在线香蕉| 国产成人免费ā片在线观看| 亚洲精品人成无码中文毛片| 亚洲中文久久精品无码| 亚洲美女中文字幕| 亚洲一日韩欧美中文字幕在线| 国产亚洲情侣久久精品| 欧洲人免费视频网站在线| 久久这里只有精品国产免费10| 好爽好紧好大的免费视频国产| 亚洲精品中文字幕无码蜜桃 | 四虎影院在线免费播放| 亚洲欧洲中文日韩久久AV乱码| 亚洲国产精品一区二区久久| 亚洲人成色77777在线观看| 高清永久免费观看| 久久精品免费全国观看国产| 亚洲日韩中文在线精品第一| 337p日本欧洲亚洲大胆精品555588| 亚洲成在人线aⅴ免费毛片| a毛片免费在线观看| 国产成人免费爽爽爽视频| 亚洲最大av无码网址| 亚洲中文无码线在线观看| 暖暖免费中文在线日本| 57pao一国产成永久免费| 又黄又爽无遮挡免费视频| 91亚洲国产成人精品下载| 羞羞视频免费网站入口| 99热这里只有精品免费播放| 免费在线观看日韩| 亚洲综合激情视频| www免费插插视频| 日本zzzzwww大片免费| 久久精品国产精品亚洲下载| 亚洲午夜在线播放| 青青青国产手机频在线免费观看| 国产高清在线精品免费软件| 噜噜噜亚洲色成人网站∨|