您的位置:软件 > 开发者网络 > 开发工具 > Java > 正文
数据库连接池Java实现小结
[文章信息]
作者:
时间:2003-09-18
出处:503room
责任编辑:方舟
[文章导读]
本文对Java连结池的设计和管理进行了一个比较全面的小节..
advertisement
热点推荐
· 天极网软件频道改版调查
· 在ASP.NET程序中创建唯一序号
· 用JVM工具接口创建调试和分析代理
· Win 2000如何安装配置防火墙
· 防范Windows消息钩子的侵入
[正文]

上一页  1 2 3  

  ConnectionParam.java

 package scut.ailab.connectionpool;
import java.io.Serializable;
/**
 * @author youyongming
 * 实现数据库连接的参数类
 */
public class ConnectionParam implements Serializable {
 private String driver;    //数据库驱动程序
 private String url;   //数据连接的URL
 private String user;    //数据库用户名
 private String password;   //数据库密码
 
 /**
  * 唯一的构造函数,需要指定连接的四个必要参数
  * @param driver 数据驱动
  * @param url  数据库连接url
  * @param user  用户名
  * @param password 密码
  */
 public ConnectionParam(String driver,String url,String user,String password)
 {
  this.driver = driver;
  this.url = url;
  this.user = user;
  this.password = password;
 }
 public String getDriver() {
  return driver;
 }
 public String getPassword() {
  return password;
 }
 public String getUrl() {
  return url;
 }
 public String getUser() {
  return user;
 }
 public void setDriver(String driver) {
  this.driver = driver;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public void setUrl(String url) {
  this.url = url;
 }
 public void setUser(String user) {
  this.user = user;
 }
 /**
  * @see java.lang.Object#clone()
  */
 public Object clone(){
  ConnectionParam param = new ConnectionParam(driver,url,user,password);
  return param;
 }
 /**
  * @see java.lang.Object#equals(java.lang.Object)
  */
 public boolean equals(Object obj) {
  if(obj instanceof ConnectionParam){
   ConnectionParam param = (ConnectionParam)obj;
   return ((driver.compareToIgnoreCase(param.getDriver()) == 0)&&
   (url.compareToIgnoreCase(param.getUrl()) == 0)&&
   (user.compareToIgnoreCase(param.getUser()) == 0)&&
   (password.compareToIgnoreCase(param.getPassword()) == 0));
  }
  return false;
 }
}

  FactoryMangeThread.java

 /*
 * Created on 2003-5-13
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package scut.ailab.connectionpool;
/**
 * @author youyongming
 *
 */
//连接池调度线程
public class FactoryMangeThread implements Runnable {
 ConnectionFactory cf = null;
 long delay = 1000;
 public FactoryMangeThread(ConnectionFactory obj)
 {
  cf = obj;
 }
 /* (non-Javadoc)
  * @see java.lang.Runnable#run()
  */
 public void run() {
  while(true){
   try{
    Thread.sleep(delay);
   }
   catch(InterruptedException e){}
   System.out.println("eeeee");
   //判断是否已经关闭了工厂,那就退出监听
   if (cf.isCreate())
    cf.schedule();
   else
    System.exit(1);
  }
 }
}

  FactoryParam.java

 /*
 * Created on 2003-5-13
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package scut.ailab.connectionpool;
/**
 * @author youyongming
 *
 */
//连接池工厂参数
public class FactoryParam {
 //最大连接数
 private int MaxConnectionCount = 4;
 //最小连接数
 private int MinConnectionCount = 2;
 //回收策略
 private int ManageType = 0;
 
 public FactoryParam() {
 }
 
 /**
  * 构造连接池工厂参数的对象
  * @param max 最大连接数
  * @param min 最小连接数
  * @param type 管理策略
  */
 public FactoryParam(int max, int min, int type)
 {
  this.ManageType = type;
  this.MaxConnectionCount = max;
  this.MinConnectionCount = min;
 }
 
 /**
  * 设置最大的连接数
  * @param value
  */
 public void setMaxConn(int value)
 {
  this.MaxConnectionCount = value;
 }
 /**
  * 获取最大连接数
  * @return
  */
 public int getMaxConn()
 {
  return this.MaxConnectionCount;
 }
 /**
  * 设置最小连接数
  * @param value
  */
 public void setMinConn(int value)
 {
  this.MinConnectionCount = value;
 }
 /**
  * 获取最小连接数
  * @return
  */
 public int getMinConn()
 {
  return this.MinConnectionCount;
 }
 public int getType()
 {
  return this.ManageType;
 }
}

  testmypool.java

 /*
 * Created on 2003-5-13
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package scut.ailab.connectionpool;
/**
 * @author youyongming
 *
 */
import java.sql.*;
public class testmypool {
 public void test1()
 {
  String user = "DevTeam";
  String password = "DevTeam";
  String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  String url = "jdbc:odbc:gfqh2";
  ConnectionParam param = new ConnectionParam(driver,url,user,password);
  ConnectionFactory cf = null;//new ConnectionFactory(param, new FactoryParam());
  try{
   cf = new ConnectionFactory(param,new FactoryParam());
   Connection conn1 = cf.getFreeConnection();
   Connection conn2 = cf.getFreeConnection();
   Connection conn3 = cf.getFreeConnection();
   Statement stmt = conn1.createStatement();
   ResultSet rs = stmt.executeQuery("select * from requests");
   if (rs.next())
   {
    System.out.println("conn1 y");  
   }
   else
   {
    System.out.println("conn1 n");  
   } 
   stmt.close();
   conn1.close();  
   Connection conn4 = cf.getFreeConnection();
   Connection conn5 = cf.getFreeConnection();
   stmt = conn5.createStatement();
   rs = stmt.executeQuery("select * from requests");
   if (rs.next())
   {
    System.out.println("conn5 y");  
   }
   else
   {
    System.out.println("conn5 n");  
   } 
   conn2.close();
   conn3.close();
   conn4.close();
   conn5.close();
   }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  finally{
   try{
    cf.close();
   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
  } 
 }
 public static void main(String[] args) {
  String user = "DevTeam";
  String password = "DevTeam";
  String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  String url = "jdbc:odbc:gfqh2";
  ConnectionParam param = new ConnectionParam(driver,url,user,password);
  ConnectionFactory cf = null;//new ConnectionFactory(param,new FactoryParam());
  try{
   cf = new ConnectionFactory(param,new FactoryParam());
   ConnectionFactory cf1= new ConnectionFactory(param,new FactoryParam());
   Connection conn1 = null;
   long time = System.currentTimeMillis();
   for (int i=0; i <10;i++)
   {
    conn1 = cf.getFreeConnection();
    Statement stmt = conn1.createStatement();
    ResultSet rs = stmt.executeQuery("select * from requests");
    if (rs.next())
    {
     System.out.println("conn1 y");  
    }
    else
    {
     System.out.println("conn1 n");  
    } 
    conn1.close();  
   }
   System.out.println("pool:" + (System.currentTimeMillis()-time));
   time = System.currentTimeMillis();
   Class.forName(param.getDriver()).newInstance();
   for (int i=0; i <10;i++)
   {
    conn1 = DriverManager.getConnection(param.getUrl(),
      param.getUser(), param.getPassword());   
    Statement stmt = conn1.createStatement();
    ResultSet rs = stmt.executeQuery("select * from requests");
    if (rs.next())
    {
     System.out.println("conn1 y");  
    }
    else
    {
     System.out.println("conn1 n");  
    } 
    conn1.close();  
   }   
   System.out.println("no pool:" + (System.currentTimeMillis()-time));
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  finally{
   try{
    cf.close();
   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
  }
 }
}


上一页  1 2 3  

·"WAP天极之IT新闻资讯,50万元等你拿"    ·天极WAP之游戏狂图,50万元等你下载


发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
相关内容阅读排行榜
  • Java网络编程之传输控制协议(一)
  • 用Java动态代理类实现记忆功能
  • Servlet容器工作原理
  • WebSphere SAP适配器编程基础
  • 在Java中使用正则表达式
  • 主板超频特色技术GIGA技嘉篇
  • Win 2000如何安装配置防火墙
  • 防范Windows消息钩子的侵入
  • 性价比之选 AGP显卡的告别秀
  • 大学校园生活之聊天也疯狂
  • Fireworks制作GIF动画Banner
  • 金山打击外挂 遭网络游戏玩家起诉
  • 完全公测《开天》掀起滔滔巨浪
  • Advertisement