Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7316993
  • 博文数量: 444
  • 博客积分: 10593
  • 博客等级: 上将
  • 技术积分: 3852
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-09 15:26
文章分类

全部博文(444)

文章存档

2014年(1)

2013年(10)

2012年(18)

2011年(35)

2010年(125)

2009年(108)

2008年(52)

2007年(72)

2006年(23)

分类: Java

2007-12-14 18:58:57

package zpxx;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

//建立DBConnectionManager
public class DBConnectionManager
{
 static private DBConnectionManager instance;
 static private int clients;

 private Vector drivers=new Vector();
 private PrintWriter log;
 private Hashtable pools=new Hashtable();

 //返回唯一的实列
 static synchronized public DBConnectionManager getInstance()
 {
  if(instance==null)
  {
   instance=new DBConnectionManager();
  }
  clients++;
  return instance;
 }

 //构造函数!
 private DBConnectionManager()
 {
  init();
 }
 //结束构造函数
 //释放一个连接
 public void freeConnection(String name,Connection con)
 {
  DBConnectionPool pool=(DBConnectionPool)pools.get(name);
  if(pool!=null)
  {
   pool.freeConnection(con);
  }
 } 
 //结束释放一个连接

 //取得一个连接
 public Connection getConnection(String name)
 {
  DBConnectionPool pool=(DBConnectionPool)pools.get(name);
  if(pool!=null)
  {
   return pool.getConnection();
  }
  return null;
 }

 public Connection getConnection(String name,String customer)
 {
  DBConnectionPool pool=(DBConnectionPool)pools.get(name);
  if(pool!=null)
  {
   return pool.getConnection(customer);
  }
  return null;
 }

 public Connection getConnection(String name,long time)
 {
  DBConnectionPool pool=(DBConnectionPool)pools.get(name);
  if(pool!=null)
  {
   return pool.getConnection(time);
  }
  return null;
 }
 //结束getconnection
 //关闭所有连接
 public synchronized void release()
 {
//  if(--clients!=0)
//   return;

  Enumeration allPools=pools.elements();
  while(allPools.hasMoreElements())
  {
   DBConnectionPool pool=(DBConnectionPool)allPools.nextElement();
   pool.release();
  }
  Enumeration allDrivers=drivers.elements();
  while(allDrivers.hasMoreElements())
  {
   Driver driver=(Driver)allDrivers.nextElement();
   try
   {
    DriverManager.deregisterDriver(driver);
    log("撤消JDBC驱动程序"+driver.getClass().getName());
   }
   catch(SQLException e)
   {
    log(e,"无法撤消JDBC驱动程序的注册"+driver.getClass().getName());
   }
  }
 }

 public synchronized void release(String customer)
 {
//  if(--clients!=0)
//   return;

  Enumeration allPools=pools.elements();
  while(allPools.hasMoreElements())
  {
   DBConnectionPool pool=(DBConnectionPool)allPools.nextElement();
   pool.release(customer);
  }
  Enumeration allDrivers=drivers.elements();
  while(allDrivers.hasMoreElements())
  {
   Driver driver=(Driver)allDrivers.nextElement();
   try
   {
    DriverManager.deregisterDriver(driver);
    log(customer+"撤消JDBC驱动程序"+driver.getClass().getName());
   }
   catch(SQLException e)
   {
    log(e,"无法撤消JDBC驱动程序的注册"+driver.getClass().getName());
   }
  }
 }
 private void createPools(Properties props)
 {
  Enumeration propNames=props.propertyNames();
  while(propNames.hasMoreElements())
  {
   String name=(String) propNames.nextElement();
   if(name.endsWith(".url"))
   {
    String poolName=name.substring(0,name.lastIndexOf("."));
    String url=props.getProperty(poolName+".url");
    if(url==null)
    {
     log("没有连接池"+poolName+"指定的URL");
     continue;
    }
    String user=props.getProperty(poolName+".user");
    String password=props.getProperty(poolName+".password");
    String maxconn= props.getProperty(poolName+".maxconn","0");
    int max;
    try
    {
     max=Integer.valueOf(maxconn).intValue();
    }
    catch(NumberFormatException e)
    {
     log("错误的最大连接数:"+maxconn+".连接池"+poolName);
     max=0;
    }
    DBConnectionPool pool=new DBConnectionPool(poolName,url,user,password,max);
    pools.put(poolName,pool);
    log("成功创建连接池"+poolName);
   }
  }
 }

 private void init()
 {
  InputStream is=getClass().getResourceAsStream("db.properties");
  Properties dbProps=new Properties();
  try
  {
   dbProps.load(is);
  }
  catch(Exception e)
  {
   System.err.println("不能读取属性文件。请确保db.properties在你的CLASSPATH中");
   return;
  }
  String logFile=dbProps.getProperty("logfile","DBConnectionManager.log");
  try
  {
   log=new PrintWriter(new FileWriter(logFile,true),true);
  }
  catch(IOException e)
  {
   System.err.println("无法打开日志文件:"+logFile);
   log=new PrintWriter(System.err);
  }
  loadDriver(dbProps);
  createPools(dbProps);
 }

 private void loadDriver(Properties props)
 {
  String driverClasses=props.getProperty("drivers");
  StringTokenizer st=new StringTokenizer(driverClasses);
  while(st.hasMoreElements())
  {
   String driverClassName=st.nextToken().trim();
   try
   {
    Driver driver=(Driver)Class.forName(driverClassName).newInstance();
    DriverManager.registerDriver(driver);
    drivers.addElement(driver);
    log("成功注册驱动程序"+driverClassName);
   }
   catch(Exception e)
   {
    log("无法注册驱动程序:"+driverClassName+",错误"+e);
   }
  }
 }

 private void log(String msg)
 {
  log.println(new Date()+":"+msg);
 }
 private void log(Throwable e,String msg)
 {
  log.println(new Date()+":"+msg);
  e.printStackTrace(log);
 }
 class DBConnectionPool
 {
  private int checkOut;
  private Vector freeConnections=new Vector();
  private int maxconn;
  private String name;
  private String password;
  private String URL;
  private String user;

  public DBConnectionPool(String name,String URL,String user,String password,int maxconn)
  {
   this.name=name;
   this.URL=URL;
   this.password=password;
   this.user=user;
   this.maxconn=maxconn;
  }
  public synchronized void freeConnection(Connection con)
  {
   freeConnections.addElement(con);
   checkOut--;
   notifyAll();
  }
  public synchronized Connection getConnection()
  {
   Connection con=null;
   if(freeConnections.size()>0)
   {
    con=(Connection)freeConnections.firstElement();
    freeConnections.removeElementAt(0);
    try
    {
     if(con.isClosed())
     {
      log("从连接池"+name+"删除一个连接");
      con=getConnection();
     }
    }
    catch(SQLException e)
    {
     log("从连接池"+name+"删除一个连接");
     con=getConnection();
    }
   }
   else if(maxconn==0||checkOut   {
    con=newConnection();
   }
   if(con!=null)
   {
    checkOut++;
   }
   return con;
  }

  public synchronized Connection getConnection(String customer)
  {
   Connection con=null;
   if(freeConnections.size()>0)
   {
    con=(Connection)freeConnections.firstElement();
    freeConnections.removeElementAt(0);
    try
    {
     if(con.isClosed())
     {
      log("从连接池"+name+"删除一个连接");
      con=getConnection();
     }
    }
    catch(SQLException e)
    {
     log("从连接池"+name+"删除一个连接");
     con=getConnection();
    }
   }
   else if(maxconn==0||checkOut   {
    con=newConnection(customer);
   }
   if(con!=null)
   {
    checkOut++;
   }
   return con;
  }

  public synchronized Connection getConnection(long timeout)
  {
   long startTime=new Date().getTime();
   Connection con;
   while((con=getConnection())==null)
   {
    try
    {
     wait(timeout);
    }
    catch(InterruptedException e)
    {}
    if((new Date().getTime()-startTime)>=timeout)
    {
     return null;
    }
   }
   return con;
  }
  public void release()
  {
   Enumeration allConnections=freeConnections.elements();
   while(allConnections.hasMoreElements())
   {
    Connection con=(Connection)allConnections.nextElement();
    try
    {
     con.close();
     log("关闭连接池"+name+"中的连接");
    }
    catch(SQLException e)
    {
     log(e,"无法关闭连接池"+name+"中的连接");
    }
   }
   freeConnections.removeAllElements();
  }
  public void release(String customer)
  {
   Enumeration allConnections=freeConnections.elements();
   while(allConnections.hasMoreElements())
   {
    Connection con=(Connection)allConnections.nextElement();
    try
    {
     con.close();
     log(customer+"关闭连接池"+name+"中的连接");
    }
    catch(SQLException e)
    {
     log(e,"无法关闭连接池"+name+"中的连接");
    }
   }
   freeConnections.removeAllElements();
  }
  private Connection newConnection()
  {
   Connection con=null;
   try
   {
    con=DriverManager.getConnection(URL,user,password);
    log("连接池"+name+"创建一个新的连接");
   }
   catch(SQLException e)
   {
    log(e,"无法创建下列URL的连接"+URL);
    return null;
   }
   return con;
  }

  private Connection newConnection(String customer)
  {
   Connection con=null;
   try
   {
    con=DriverManager.getConnection(URL,user,password);
    log(customer+"从连接池"+name+"创建一个新的连接");
   }
   catch(SQLException e)
   {
    log(e,"无法创建下列URL的连接"+URL);
    return null;
   }
   return con;
  }
 }
}

阅读(2497) | 评论(6) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-27 17:22:41

2010 new UGG boots on sale,buy Cheap UGG boots enter:>> http://www.uggvipshop.com 2010 new MBT shoes on sale,buy Cheap MBT shoes enter:>> http://www.MBTflying.com P90X workout help you lose weight,buy Cheap P90X,buy Baby Signing Time,BeachBody,Cartoon DVD,etc enter: >> http://www.P90Xkicks.com 2010 Christian Louboutin shoes,buy Cheap Christian Louboutin shoes, buy cheap Yves Saint Laurent shoes, buy Cheap Jimmy Choo shoes, enter:>> http://www.sexyshoeskicks.com Air Jordan shoes o