成都心情

  BlogJava :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  98 随笔 :: 2 文章 :: 501 评论 :: 1 Trackbacks

这几天花了点时间弄了个 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(最大连接数)和对多数据库文件的支持以及日志等。


请注意!引用、转贴本文应注明原作者:Rosen Jiang 以及出处:http://www.blogjava.net/rosen
posted on 2006-09-27 15:26 Rosen 阅读(3868) 评论(8)  编辑  收藏 所属分类: Versant db4o 中文项目

评论

# re: db4o 连接池 2006-09-30 15:47 sandy
不错,帮忙顶!!!  回复  更多评论
  

# re: db4o 连接池 2006-11-15 17:27 Rosen
issue track:
取connection时有问题
sleep,或者使用wait和notify

只能是jdk1.5以上才能用;不好;
另外,限制了只能使用本地模式,无法防伪远程的;  回复  更多评论
  

# re: db4o 连接池 2006-11-23 09:26 虎.无名
把我在QQ群上的留言转到这儿来了呀。上面有些地方没写太清楚。
1,使用sleep或者wait/notify是为了避免循环忙等待,这个非常消耗CPU资源,否则不适合用于生产环境。
2,限制在Jdk1.5以上版本了,并非必要。
3,用户应该可以选择本地模式,还是远程模式;对于后者,需要远程服务器和端口,同时,在代码上,就无需Db4o.openServer了。  回复  更多评论
  

# re: db4o 连接池 2006-11-23 12:25 Rosen
@虎.无名
目前正在改造连接池:
对 jdk 1.2 以上版本的兼容;加入 apache pool 实现;可选择的本地、远程模式。如果你有兴趣,我们可以共同完成,的确,连接池的呼声越来越高了。  回复  更多评论
  

# re: db4o 连接池 2006-11-24 18:08 虎.无名
新的实现,发布在javaeye上了,看看还有什么问题?
http://zeroliu.javaeye.com/  回复  更多评论
  

# re: db4o 连接池 2006-11-24 22:01 Rosen
又见 javaeye,我是这种表情 @-@ 。
今天和 andrew 闲谈,跟着他的思路走,一下子开阔了许多,我有种非常极端的想法:C/S 模式,在不考虑网络的因素下 server 端应该自带连接池,并且,这种性能比获取 JDBC 的开销小 N 倍。这样才是以性能著称的 db4o 的风格!!!  回复  更多评论
  

# re: db4o 连接池 2007-12-05 17:32 vvvic
如何使用这个连接池呢?  回复  更多评论
  

# re: db4o 连接池 2007-12-05 20:16 Rosen
@vvvic
今年一月在香港开会的时候,大多数开发者不支持这种连接池方案,我也只好作罢。另外,你可以看看db4o与spring的集成类库,工具叫做spring-modules。我用过一下,其没有提供连接池的机制。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: