dbcp连接池实现

文章原地址:http://www.iteye.com/topic/117225

DBCP是Apache的一个开源项目:
commons.dbcp.jar
http://jakarta.apache.org/commons/dbcp/index.html

DBCP依赖Apache的另外2个开源项目
commons.collections.jar和commons.pool.jar

下载这些包并将这些包的路径添加到classpath中就可以使用dbcp做为项目中的数据库连接池使用了。

以下是我的连接池

java 代码
  1. package selfservice;   
  2.   
  3. import java.io.FileNotFoundException;   
  4. import java.io.IOException;   
  5. import java.sql.Connection;   
  6. import java.sql.DriverManager;   
  7. import java.sql.ResultSet;   
  8. import java.sql.SQLException;   
  9. import java.sql.Statement;   
  10.   
  11. import org.apache.commons.dbcp.ConnectionFactory;   
  12. import org.apache.commons.dbcp.DriverManagerConnectionFactory;   
  13. import org.apache.commons.dbcp.PoolableConnectionFactory;   
  14. import org.apache.commons.dbcp.PoolingDriver;   
  15. import org.apache.commons.pool.ObjectPool;   
  16. import org.apache.commons.pool.impl.GenericObjectPool;   
  17.   
  18.   
  19. public class PoolManager {     
  20.     private static String    
  21.             driver="oracle.jdbc.driver.OracleDriver",//驱动   
  22.             url = "jdbc:oracle:thin:@192.168.0.40:1521:drcom",//URL   
  23.             Name="drcom",//用户名   
  24.             Password="drcom";//密码   
  25.        
  26.     private static Class driverClass = null;   
  27.     private static ObjectPool connectionPool = null;   
  28.   
  29.     public PoolManager(){          
  30.     }      
  31.        
  32.     /**  
  33.      * 装配配置文件  
  34.      * initProperties      
  35.      */  
  36.     private static void loadProperties(){   
  37.         try {   
  38.             java.io.InputStream stream = new java.io.FileInputStream("config.properties");   
  39.             java.util.Properties props = new java.util.Properties();   
  40.             props.load(stream);   
  41.                
  42.             driver = props.getProperty("ORACLE_DRIVER");   
  43.             url = props.getProperty("ORACLE_URL");   
  44.             Name = props.getProperty("ORACLE_LOGIN_NAME");   
  45.             Password = props.getProperty("ORACLE_LOGIN_PASSWORD");   
  46.                
  47.         } catch (FileNotFoundException e) {   
  48.             System.out.println("读取配置文件异常");            
  49.         } catch(IOException ie){   
  50.             System.out.println("读取配置文件时IO异常");   
  51.         }   
  52.     }   
  53.        
  54.     /**   
  55.      * 初始化数据源   
  56.      */    
  57.     private static synchronized void initDataSource() {            
  58.         if (driverClass == null) {    
  59.             try {    
  60.                 driverClass = Class.forName(driver);    
  61.             } catch (ClassNotFoundException e) {    
  62.                 e.printStackTrace();   
  63.             }    
  64.         }    
  65.     }    
  66.     
  67.     /**   
  68.      * 连接池启动   
  69.      * @throws Exception   
  70.      */    
  71.     public static void StartPool() {   
  72.         loadProperties();   
  73.         initDataSource();    
  74.         if (connectionPool != null) {    
  75.             ShutdownPool();    
  76.         }     
  77.         try {    
  78.             connectionPool = new GenericObjectPool(null);    
  79.             ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, Name, Password);    
  80.             PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, nullnullfalsetrue);    
  81.             Class.forName("org.apache.commons.dbcp.PoolingDriver");    
  82.             PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");    
  83.             driver.registerPool("dbpool", connectionPool);                 
  84.             System.out.println("装配连接池OK");    
  85.         } catch (Exception e) {    
  86.             e.printStackTrace();   
  87.         }    
  88.     }    
  89.     
  90.     /**   
  91.      * 释放连接池   
  92.      */    
  93.     public static void ShutdownPool() {    
  94.         try {    
  95.             PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");    
  96.             driver.closePool("dbpool");   
  97.         } catch (SQLException e) {    
  98.             e.printStackTrace();   
  99.         }    
  100.     }        
  101.     
  102.     /**   
  103.      * 取得连接池中的连接   
  104.      * @return   
  105.      */    
  106.     public static Connection getConnection() {    
  107.         Connection conn = null;    
  108.         if(connectionPool == null)    
  109.             StartPool();    
  110.         try {    
  111.             conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:dbpool");    
  112.         } catch (SQLException e) {    
  113.             e.printStackTrace();   
  114.         }    
  115.         return conn;    
  116.     }    
  117.        
  118.     /**  
  119.      * 获取连接  
  120.      * getConnection  
  121.      * @param name  
  122.      * @return  
  123.      */  
  124.     public static Connection getConnection(String name){   
  125.         return getConnection();   
  126.     }   
  127.     /**  
  128.      * 释放连接  
  129.      * freeConnection  
  130.      * @param conn  
  131.      */  
  132.     public static void freeConnection(Connection conn){   
  133.         if(conn != null){   
  134.             try {   
  135.                 conn.close();   
  136.             } catch (SQLException e) {                 
  137.                 e.printStackTrace();   
  138.             }   
  139.         }   
  140.     }   
  141.     /**  
  142.      * 释放连接  
  143.      * freeConnection  
  144.      * @param name  
  145.      * @param con  
  146.      */  
  147.     public static void freeConnection (String name,Connection con){   
  148.         freeConnection(con);   
  149.     }   
  150.        
  151.     /**  
  152.      * 例子  
  153.      * main  
  154.      * @param args  
  155.      */  
  156.     public static void main(String[] args) {           
  157.         try {   
  158.             Connection conn = PoolManager.getConnection();   
  159.             if(conn != null){   
  160.                 Statement statement = conn.createStatement();   
  161.                 ResultSet rs = statement.executeQuery("select * from tblgxinterface");   
  162.                 int c = rs.getMetaData().getColumnCount();   
  163.                 while(rs.next()){                      
  164.                     System.out.println();   
  165.                     for(int i=1;i<=c;i++){   
  166.                         System.out.print(rs.getObject(i));   
  167.                     }   
  168.                 }   
  169.                 rs.close();   
  170.             }   
  171.             PoolManager.freeConnection(conn);   
  172.         } catch (SQLException e) {             
  173.             e.printStackTrace();   
  174.         }   
  175.   
  176.     }   
  177.   
  178. }   

以上创建一个连接池,并从连接池中得到连接,连接池会管理每个连接,以上测试通过。

猜你喜欢

转载自jijiaa12345.iteye.com/blog/1744663