自己写的数据库连接池

package  database;  
import  java.sql.DriverManager;  
import  java.sql.Connection;  
import  java.sql.SQLException;  
 
import  org.apache.commons.dbcp.ConnectionFactory;  
import  org.apache.commons.dbcp.DriverManagerConnectionFactory;  
import  org.apache.commons.dbcp.PoolableConnectionFactory;  
import  org.apache.commons.dbcp.PoolingDriver;  
import  org.apache.commons.pool.ObjectPool;  
import  org.apache.commons.pool.impl.GenericObjectPool;  
 
public  class  ConnectionPool  {  
                         
       private  final  String  DBDriver  =    "com.mysql.jdbc.Driver  ";  
       private  final  String  schema  =    "examsystem  ";  
       private  final  String  user  =    "root  ";  
       private  final  String  password  =    "comeon  ";  
       private  final  static  String  poolName  =    "connectionpool  ";  
         
       private  final  String  driverInfo    
               =    "jdbc:mysql://localhost/  "  +  schema  +    "?user=  "  +  user  +    "&password=  "  +  password  
               +    "&useUnicode=true&characterEncoding=gb2312  ";      
         
       private  static  ConnectionPool  connectionPool;  
         
       private  ConnectionPool(){  
               System.out.println(  "Loading  underlying  JDBC  driver.  ");  
               try  {    
                       Class.forName(DBDriver);  
               }  catch  (ClassNotFoundException  e)  {  
                       e.printStackTrace();  
               }  
               System.out.println(  "Done.  ");  
                 
               System.out.println(  "Setting  up  driver.  ");  
               try  {  
                       setupDriver(  driverInfo  );  
               }  catch  (Exception  e)  {  
                       e.printStackTrace();  
               }  
               System.out.println(  "Done.  ");  
       }  
         
       public  static  ConnectionPool  getInstance  ()  {  
                   
               if  (  connectionPool  ==  null)  {  
                       connectionPool  =  new  ConnectionPool();  
               }  
                 
               return  connectionPool;  
       }  
         
       public  Connection  getConnection()  {  
               System.out.println(  "Creating  connection.  ");  
               try  {  
                       Connection  conn  =  DriverManager.getConnection(  "jdbc:apache:commons:dbcp:connectionpool  ");  
                       System.out.println(  "得到一个新的连接:/r/n  "  +  conn.toString());  
                       return  conn;  
               }  catch  (SQLException  e)  {  
                       //  TODO  Auto-generated  catch  block  
                       e.printStackTrace();  
                       return  null;  
               }  
       }  
             
       public  static  void  setupDriver(String  connectURI)  throws  Exception  {  
               //  
               //  First,  we'll  need  a  ObjectPool  that  serves  as  the  
               //  actual  pool  of  connections.  
               //  
               //  We'll  use  a  GenericObjectPool  instance,  although  
               //  any  ObjectPool  implementation  will  suffice.  
               //GenericObjectPool(org.apache.commons.pool.PoolableObjectFactory  factory,    
               //                            int  maxActive,    
               //                            byte  whenExhaustedAction,    
               //                            long  maxWait,    
               //                            int  maxIdle,    
               //                            int  minIdle,    
               //                            boolean  testOnBorrow,    
               //                            boolean  testOnReturn,    
               //                            long  timeBetweenEvictionRunsMillis,    
               //                            int  numTestsPerEvictionRun,    
               //                            long  minEvictableIdleTimeMillis,    
               //                            boolean  testWhileIdle)  
               ObjectPool  connectionPool  =  new  GenericObjectPool(null,  
                               100,  
                               GenericObjectPool.WHEN_EXHAUSTED_GROW,  
                               10000,  
                               50);  
 
               //  
               //  Next,  we'll  create  a  ConnectionFactory  that  the  
               //  pool  will  use  to  create  Connections.  
               //  We'll  use  the  DriverManagerConnectionFactory,  
               //  using  the  connect  string  passed  in  the  command  line  
               //  arguments.  
               //  
               ConnectionFactory  connectionFactory  =  new  DriverManagerConnectionFactory(connectURI,null);  
 
               //  
               //  Now  we'll  create  the  PoolableConnectionFactory,  which  wraps  
               //  the    "real  "  Connections  created  by  the  ConnectionFactory  with  
               //  the  classes  that  implement  the  pooling  functionality.  
               //  
               PoolableConnectionFactory  poolableConnectionFactory  =  new  PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);  
 
               //  
               //  Finally,  we  create  the  PoolingDriver  itself...  
               //  
               Class.forName(  "org.apache.commons.dbcp.PoolingDriver  ");  
               PoolingDriver  driver  =  (PoolingDriver)  DriverManager.getDriver(  "jdbc:apache:commons:dbcp:  ");  
 
               //  
               //  ...and  register  our  pool  with  it.  
               //  
               driver.registerPool(poolName,connectionPool);  
 
               //  
               //  Now  we  can  just  use  the  connect  string    "jdbc:apache:commons:dbcp:example  "  
               //  to  access  our  pool  of  Connections.  
               //  
       }  
 
       public  static  void  printDriverStats()  throws  Exception  {  
               PoolingDriver  driver  =  (PoolingDriver)  DriverManager.getDriver(  "jdbc:apache:commons:dbcp:  ");  
               ObjectPool  connectionPool  =  driver.getConnectionPool(poolName);  
                 
               System.out.println(  "NumActive:    "  +  connectionPool.getNumActive());  
               System.out.println(  "NumIdle:    "  +  connectionPool.getNumIdle());  
       }  
 
       public  static  void  shutdownDriver()  throws  Exception  {  
               PoolingDriver  driver  =  (PoolingDriver)  DriverManager.getDriver(  "jdbc:apache:commons:dbcp:  ");  
               driver.closePool(poolName);  
       }  
         
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值