javajohn

金色年华

apache dbcp数据库连接池的使用

 

  1 public   class  DaoUtil  {
  2
  3      /**
  4      * 数据库连接池
  5      * 
  6      *  @see   http://jakarta.apache.org/commons/dbcp/index.html
  7       */

  8      private   static  PoolingDriver driver  =   null ;
  9
 10      /**
 11      * 设置一个数据库连接池
 12      * 
 13      *  @param  name
 14      *            连接池的名称
 15      *  @param  url
 16      *            数据源
 17      *  @throws  SQLException
 18       */

 19      private   static   void  setUpDriverPool(String name, String url)
 20              throws  SQLException  {
 21          if  ((driver  ==   null ||  driver.getPoolNames().length  <   2 {
 22              try   {
 23                  /**
 24                  * 首先创建一个对象池来保存数据库连接 使用 commons.pool 的 GenericObjectPool对象
 25                   */

 26                 ObjectPool connectionPool  =   new  GenericObjectPool();
 27                  /**
 28                  * 创建一个 DriverManagerConnectionFactory对象 连接池将用它来获取一个连接
 29                   */

 30                 ConnectionFactory connectionFactory  =   new  DriverManagerConnectionFactory(
 31                         url,  null );
 32                  /**
 33                  * 创建一个PoolableConnectionFactory 对象。
 34                   */

 35                 PoolableConnectionFactory poolableConnectionFactory  =   new  PoolableConnectionFactory(
 36                         connectionFactory, connectionPool,  null null false ,
 37                          true );
 38                  /**
 39                  * 注册PoolingDriver。
 40                   */

 41                 Class.forName( " org.apache.commons.dbcp.PoolingDriver " );
 42                 driver  =  (PoolingDriver) DriverManager.getDriver( " jdbc:apache:commons:dbcp: " );
 43                 driver.registerPool(name, connectionPool);
 44             }
  catch  (ClassNotFoundException e)  {
 45                  throw   new  RuntimeException(e);
 46             }

 47         }

 48     }

 49
 50      /**
 51      * 关闭所有数据库连接池
 52      * 
 53       */

 54      public   static   void  shutDownDriver()  {
 55
 56          try   {
 57             PoolingDriver driver  =  (PoolingDriver) DriverManager
 58                     .getDriver( " jdbc:apache:commons:dbcp: " );
 59             String[] poolNames  =  driver.getPoolNames();
 60              if (poolNames.length  >   1 ) {
 61                  for  ( int  i  =   0 ; i  <  poolNames.length; i ++ {
 62                     driver.closePool( " pool " );
 63                 }

 64             }

 65         }
  catch  (SQLException sqle)  {
 66              throw   new  RuntimeException(sqle);
 67         }

 68     }

 69
 70      /**
 71      * 取得一个数据库连接对象。
 72      * 
 73      * 因为可能使用两个不同的数据库, 所以依据report的值来确定使用那个数据库。
 74      * 
 75      *  @param  report
 76      *  @return
 77       */

 78      public   static  Connection getConnection()  {
 79         Connection con  =   null ;
 80          try   {
 81             ReadConfig readConfig  =   new  ReadConfig();
 82             readConfig.init( null );
 83              //  装载mysql的jdbc驱动
 84             String driver  =  readConfig.getDBDriver();
 85             String url  =  readConfig.getDBUrl();
 86             String poolName  =   " pool " ;
 87             Class.forName(driver);
 88             setUpDriverPool(poolName, url);
 89             con  =  DriverManager.getConnection( " jdbc:apache:commons:dbcp: "
 90                      +  poolName);
 91              return  con;
 92         }
  catch  (ClassNotFoundException cnfe)  {
 93              throw   new  RuntimeException( " 无法装入数据库引擎 " );
 94         }
  catch  (SQLException sqle)  {
 95              throw   new  RuntimeException( " 无法打开数据库连接 " );
 96         }

 97     }

 98
 99      /**
100      * 执行清理过程
101      * <li>关闭数据库连接</li>
102      * <li>关闭语句对象</li>
103      * <li>关闭结果集</li>
104      * 
105      *  @param  con
106      *  @param  s
107      *  @param  rs
108       */

109      public   static   void  closeAll(Connection con, Statement s, ResultSet rs)  {
110          try   {
111              if  (rs  !=   null {
112                 rs.close();
113                 rs  =   null ;
114             }

115              if  (s  !=   null {
116                 s.close();
117                 s  =   null ;
118             }

119              if  (con  !=   null {
120                 con.close();
121                 con  =   null ;
122             }

123         }
  catch  (SQLException sqle)  {
124              //  nothing to do, forget it;
125         }

126     }

127
128      public   static   void  main(String[] args)  {
129 //         DaoUtil daoUtil = new DaoUtil();
130 //         Connection connection = null;
131 //         Statement statement = null;
132 //         connection = daoUtil.getConnection();
133 //         ResultSet rs = null;
134 //         try {
135 //             statement = connection.createStatement();
136 //             rs = statement.executeQuery("select * from admin");
137 //             while(rs.next()){
138 //                 System.out.println(rs.getString("adminName"));
139 //             }
140 //         } catch (SQLException e) {
141 //             e.printStackTrace();
142 //         }
143
144     }

145
146 }

posted on 2006-07-17 11:19 javajohn 阅读(3823) 评论(1)  编辑  收藏 所属分类: 数据库

Feedback

# re: apache dbcp数据库连接池的使用 2007-04-15 17:11 clover

很不错,我正在学习数据库连接池,需要参照借鉴你的思想和代码,非常感谢。希望以后还能看到你的杰作。对我们初学者很有益。再次表示感谢!  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

常用链接

留言簿(7)

随笔分类(36)

随笔档案(39)

classmate

good blog

企业管理网站

好友

站点收藏

搜索

最新评论

阅读排行榜

评论排行榜