主题:Tomcat连接池原理讨论

 
  1. package com.dalong.connectionpool;   
  2. import java.util.HashMap;   
  3. import java.util.Vector;   
  4. import java.sql.Connection;   
  5. import java.sql.DriverManager;   
  6. import java.sql.*;   
  7. /**  
  8.  * <p>Title: </p>  
  9.  * <p>Description: test</p>  
  10.  * <p>Copyright: Copyright (c); 2003</p>  
  11.  * <p>Company: home</p>  
  12.  * @author dalong  
  13.  * @version 1.0  
  14.  */  
  15.   
  16. public class ConnectionPool {   
  17.   
  18.   //连接池的管理器,首先初始化,仅仅有一个对象,管理连接池   
  19.   private static HashMap connectionPoolManager=new HashMap();;   
  20.   //没有用过的连接池,用vector实现同步   
  21.   private static Vector noUseConnectionPool;   
  22.   //没有用过的连接池   
  23.   private  static HashMap nowUseConnectionPool;   
  24.   
  25.   private  static String dbDriver="odbc:jdbc:OdbcJdbcDriver";   
  26.   private  static String dbUrl="dalong@XX";   
  27.   private  static String userName="dalong";   
  28.   private  static String userPassword="dalong";   
  29.   
  30.   //默认为100个连接池   
  31.   private  static int MAX_POOL=100;   
  32.   
  33.  //singleTon 设计模式   
  34.   private ConnectionPool(String driver,String url,String name,String password,int max);   
  35.      throws ClassNotFoundException {   
  36.       Class.forName(driver);;   
  37.       dbUrl=url;   
  38.       userName=name;   
  39.       userPassword=password;   
  40.       MAX_POOL=max;   
  41.  }   
  42.  public static ConnectionPool getConnManagerInstance(String poolName);   
  43.       throws ClassNotFoundException{   
  44.      ConnectionPool tempPool=(ConnectionPool);connectionPoolManager.get(poolName);;   
  45.      if(tempPool==null);{   
  46.         tempPool=new ConnectionPool(dbDriver,dbUrl,userName,userPassword,MAX_POOL);;   
  47.         connectionPoolManager.put(poolName,tempPool);;   
  48.         return tempPool;   
  49.      }else  
  50.        return tempPool;   
  51.  }   
  52.   
  53.  //通过连接池获得真正的链接   
  54.  public static Connection getConnection(); throws java.sql.SQLException{   
  55.     Connection conn=null;   
  56.     synchronized(noUseConnectionPool);{   
  57.        if(noUseConnectionPool.size();>0);{   
  58.          conn=(Connection);noUseConnectionPool.firstElement();;   
  59.          noUseConnectionPool.remove(conn);;   
  60.         return conn;   
  61.        }   
  62.     }   
  63.     //如果数据库连接池没有链接了,自己创建一个   
  64.     if(conn==null);{   
  65.        conn=createConnection(dbDriver,dbUrl,userName,userPassword);;   
  66.     }else if(conn.isClosed(););{   
  67.        nowUseConnectionPool.remove(conn);;   
  68.        conn=createConnection(dbDriver,dbUrl,userName,userPassword);;   
  69.       }   
  70.     conn.setAutoCommit(false);;   
  71.     nowUseConnectionPool.put(conn,conn);;   
  72.     return conn;   
  73.  }   
  74.   
  75.  //如果连接池没有链接了,就需要产生一个链接   
  76.  private static Connection createConnection(String driver,String url,String user,String password);   
  77.        throws java.sql.SQLException{   
  78.      Connection conn=DriverManager.getConnection(url,user,password);;   
  79.      return conn;   
  80.  }   
  81.  public static void releaseConnection(Connection conn,boolean isCommit);   
  82.     throws java.sql.SQLException{   
  83.     if(isCommit);   
  84.        conn.commit();;   
  85.     else  
  86.        conn.rollback();;   
  87.     nowUseConnectionPool.remove(conn);;   
  88.        if(noUseConnectionPool.size(); + nowUseConnectionPool.size();<MAX_POOL);{   
  89.           synchronized(noUseConnectionPool);{   
  90.             noUseConnectionPool.add(conn);;   
  91.           }   
  92.        }else{   
  93.          conn.close();;   
  94.        }   
  95.  }   
  96.   
  97.  public static void main(String[] args); {   
  98.   //测试模拟10个客户   
  99.    for (int i = 0; i < 10; i++); {   
  100.      try {   
  101.        //xxxx 一般为属性文件读取   
  102.        ConnectionPool pool = ConnectionPool.getConnManagerInstance("xxxx");;   
  103.        Connection conn = pool.getConnection();;   
  104.   
  105.      }catch (SQLException ex1); {   
  106.         //处理异常   
  107.       }   
  108.      catch (ClassNotFoundException ex); {   
  109.         //处理异常   
  110.      }   
  111.    }   
  112.  }   
  113. }  

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值