标签:
连接池的管理用了了享元模式,这里对连接池进行简单设计。
一、设计思路
     1.连接池配置属性DBbean:里面存放可以配置的一些属性
     2.连接池接口IConnectionPool:里面定义一些基本的获取连接的一些方法
     3.接口实现ConnectionPool:对上面操作进行实现,并加入一些其他方法
     4.连接池管理ConnectionPoolManager:管理所有的不同的连接池,所有的连接都能通过这里进行获得连接
     5.另外还有几个测试类,和连接信息模拟的类,这里就不进行xml 和配置文件信息的读取了
 
- package pool;  
-   
- import java.sql.Connection;  
- import java.sql.SQLException;  
-   
- public interface IConnectionPool {  
-     
-     public Connection  getConnection();  
-     
-     public Connection getCurrentConnecton();  
-     
-     public void releaseConn(Connection conn) throws SQLException;  
-     
-     public void destroy();  
-     
-     public boolean isActive();  
-     
-     public void cheackPool();  
- }  
 
 
 
- package pool;  
-   
- import java.sql.Connection;  
- import java.sql.SQLException;  
- import java.util.Hashtable;  
- public class ConnectionPoolManager {  
-       
-       
-     
-     public Hashtable<String,IConnectionPool> pools = new Hashtable<String, IConnectionPool>();  
-       
-     
-     private ConnectionPoolManager(){  
-         init();  
-     }  
-     
-     public static ConnectionPoolManager getInstance(){  
-         return Singtonle.instance;  
-     }  
-     private static class Singtonle {  
-         private static ConnectionPoolManager instance =  new ConnectionPoolManager();  
-     }  
-       
-       
-     
-     public void init(){  
-         for(int i =0;i<DBInitInfo.beans.size();i++){  
-             DBbean bean = DBInitInfo.beans.get(i);  
-             ConnectionPool pool = new ConnectionPool(bean);  
-             if(pool != null){  
-                 pools.put(bean.getPoolName(), pool);  
-                 System.out.println("Info:Init connection successed ->" +bean.getPoolName());  
-             }  
-         }  
-     }  
-       
-     
-     public Connection  getConnection(String poolName){  
-         Connection conn = null;  
-         if(pools.size()>0 && pools.containsKey(poolName)){  
-             conn = getPool(poolName).getConnection();  
-         }else{  
-             System.out.println("Error:Can‘t find this connecion pool ->"+poolName);  
-         }  
-         return conn;  
-     }  
-       
-     
-     public void close(String poolName,Connection conn){  
-             IConnectionPool pool = getPool(poolName);  
-             try {  
-                 if(pool != null){  
-                     pool.releaseConn(conn);  
-                 }  
-             } catch (SQLException e) {  
-                 System.out.println("连接池已经销毁");  
-                 e.printStackTrace();  
-             }  
-     }  
-       
-     
-     public void destroy(String poolName){  
-         IConnectionPool pool = getPool(poolName);  
-         if(pool != null){  
-             pool.destroy();  
-         }  
-     }  
-       
-     
-     public IConnectionPool getPool(String poolName){  
-         IConnectionPool pool = null;  
-         if(pools.size() > 0){  
-              pool = pools.get(poolName);  
-         }  
-         return pool;  
-     }  
- }  
 
 
- package pool;  
-   
- import java.util.ArrayList;  
- import java.util.List;  
- public class DBInitInfo {  
-     public  static List<DBbean>  beans = null;  
-     static{  
-         beans = new ArrayList<DBbean>();  
-         
-         
-         DBbean beanOracle = new DBbean();  
-         beanOracle.setDriverName("oracle.jdbc.driver.OracleDriver");  
-         beanOracle.setUrl("jdbc:oracle:thin:@7MEXGLUY95W1Y56:1521:orcl");  
-         beanOracle.setUserName("mmsoa");  
-         beanOracle.setPassword("password1234");  
-           
-         beanOracle.setMinConnections(5);  
-         beanOracle.setMaxConnections(100);  
-           
-         beanOracle.setPoolName("testPool");  
-         beans.add(beanOracle);  
-     }  
- }  
 
 
    测试:
   
- package pool;  
-   
- import java.sql.Connection;  
- public class ThreadConnection implements Runnable{  
-     private IConnectionPool pool;  
-     @Override  
-     public void run() {  
-         pool = ConnectionPoolManager.getInstance().getPool("testPool");  
-     }  
-       
-     public Connection getConnection(){  
-         Connection conn = null;  
-         if(pool != null && pool.isActive()){  
-             conn = pool.getConnection();  
-         }  
-         return conn;  
-     }  
-       
-     public Connection getCurrentConnection(){  
-         Connection conn = null;  
-         if(pool != null && pool.isActive()){  
-             conn = pool.getCurrentConnecton();  
-         }  
-         return conn;  
-     }  
- }  
 
 
- package pool;  
-   
-   
-   
- public class Client {  
-     public static void main(String[] args) throws InterruptedException {  
-         
-         Thread t = init();  
-         t.start();  
-         t.join();  
-           
-         ThreadConnection a = new ThreadConnection();  
-         ThreadConnection b = new ThreadConnection();  
-         ThreadConnection c = new ThreadConnection();  
-         Thread t1 = new Thread(a);  
-         Thread t2 = new Thread(b);  
-         Thread t3 = new Thread(c);  
-           
-           
-         
-         
-         t1.setPriority(10);  
-         t2.setPriority(10);  
-         t3.setPriority(10);  
-         t1.start();  
-         t2.start();  
-         t3.start();  
-           
-         System.out.println("线程A-> "+a.getConnection());  
-         System.out.println("线程B-> "+b.getConnection());  
-         System.out.println("线程C-> "+c.getConnection());  
-     }  
-   
-     
-     public static Thread init() {  
-         Thread t = new Thread(new Runnable() {  
-             @Override  
-             public void run() {  
-                 IConnectionPool  pool = initPool();  
-                 while(pool == null || !pool.isActive()){  
-                     pool = initPool();  
-                 }  
-             }  
-         });  
-         return t;  
-     }  
-       
-     public static IConnectionPool initPool(){  
-         return ConnectionPoolManager.getInstance().getPool("testPool");  
-     }  
-   
- }  
 
 
小结 :
          1.连接池诞生原因是,如果每次都从数据库获得连接,时间比较长,因此我们提前做建立一些连接,放在连接池里面,每次都从里面取
          2.上面仅仅写了连接池基本原理,关于多线程下连接池的管理没写,后面对多线程操作熟练了添加吧
本文转自http://greemranqq.iteye.com/blog/1969273 感谢作者
JAVA数据库连接池实现(转)
标签:
原文地址:http://www.cnblogs.com/panxuejun/p/5970855.html