c3p0连接池

突然想用一下c3p0连接池,在网上找了一下,要么根本不能用,要么就是达不到自己的要求。 

不过后来想起以前有人写过这样的一个类,于是拿出来试了一下,果然能用,而且功能也非常完善。 

环境: 
eclipse3.5 
mysql-connector-java-5.1.7-bin.jar 
c3p0-0.9.1.2.jar 
commons-logging-1.1.1.jar 


JAVA类代码如下: 

Java代码   收藏代码
  1. public class ConnectionPool {  
  2.   
  3.     public static void cleanUp(Connection con) {  
  4.         _instance._cleanUp(con);  
  5.     }  
  6.     public static void cleanUp(Connection con, Statement s) {  
  7.         _instance._cleanUp(con, s);  
  8.     }  
  9.     public static void cleanUp(Connection con, Statement s, ResultSet rs) {  
  10.         _instance._cleanUp(con, s, rs);  
  11.     }  
  12.     public static void destroy() throws SQLException {  
  13.         _instance._destroy();  
  14.     }  
  15.   
  16.     public static Connection getConnection() throws SQLException {  
  17.         return _instance._getConnection();  
  18.     }  
  19.       
  20.     public static Properties getProperties() {  
  21.         return _instance._props;  
  22.     }  
  23.   
  24.     private ConnectionPool() {  
  25.         try {  
  26.   
  27.             // Properties  
  28.   
  29.             ClassLoader classLoader = getClass().getClassLoader();  
  30.   
  31.             _props = new Properties();  
  32.   
  33.             _props.load(classLoader.getResourceAsStream(  
  34.                 "connection-pool.properties"));  
  35.   
  36.             _props.list(System.out);  
  37.   
  38.             // Pooled data source  
  39.   
  40.             String driverClass = _props.getProperty("driver.class");  
  41.             String jdbcUrl = _props.getProperty("jdbc.url");  
  42.             String user = _props.getProperty("user");  
  43.             String password = _props.getProperty("password");  
  44.   
  45.             int minPoolSize = 5;  
  46.   
  47.             try {  
  48.                 minPoolSize = Integer.parseInt(  
  49.                     _props.getProperty("min.pool.size"));  
  50.             }  
  51.             catch (Exception e) {  
  52.             }  
  53.   
  54.             int maxPoolSize = 5;  
  55.   
  56.             try {  
  57.                 maxPoolSize = Integer.parseInt(  
  58.                     _props.getProperty("max.pool.size"));  
  59.             }  
  60.             catch (Exception e) {  
  61.             }  
  62.   
  63.             int acquireIncrement = 5;  
  64.   
  65.             try {  
  66.                 acquireIncrement = Integer.parseInt(  
  67.                     _props.getProperty("acquire.increment"));  
  68.             }  
  69.             catch (Exception e) {  
  70.             }  
  71.   
  72.             _cpds = new ComboPooledDataSource();  
  73.   
  74.             _cpds.setDriverClass(driverClass);  
  75.             _cpds.setJdbcUrl(jdbcUrl);  
  76.             _cpds.setUser(user);  
  77.             _cpds.setPassword(password);  
  78.   
  79.             _cpds.setMinPoolSize(minPoolSize);  
  80.             _cpds.setMaxPoolSize(maxPoolSize);  
  81.             _cpds.setAcquireIncrement(acquireIncrement);  
  82.         }  
  83.         catch (Exception e) {  
  84.             _log.error(e);  
  85.         }  
  86.     }  
  87.   
  88.     private void _cleanUp(Connection con) {  
  89.         _cleanUp(con, nullnull);  
  90.     }  
  91.   
  92.     private void _cleanUp(Connection con, Statement s) {  
  93.         _cleanUp(con, s, null);  
  94.     }  
  95.   
  96.     private void _cleanUp(Connection con, Statement s, ResultSet rs) {  
  97.         try {  
  98.             if (rs != null) {  
  99.                 rs.close();  
  100.             }  
  101.         }  
  102.         catch (SQLException sqle) {  
  103.             _log.error(sqle);  
  104.         }  
  105.   
  106.         try {  
  107.             if (s != null) {  
  108.                 s.close();  
  109.             }  
  110.         }  
  111.         catch (SQLException sqle) {  
  112.             _log.error(sqle);  
  113.         }  
  114.   
  115.         try {  
  116.             if (con != null) {  
  117.                 con.close();  
  118.             }  
  119.         }  
  120.         catch (SQLException sqle) {  
  121.             _log.error(sqle);  
  122.         }  
  123.     }  
  124.   
  125.     private void _destroy() throws SQLException {  
  126.         DataSources.destroy(_cpds);  
  127.     }  
  128.   
  129.     private Connection _getConnection() throws SQLException {  
  130.         return _cpds.getConnection();  
  131.     }  
  132.   
  133.     private static Log _log = LogFactory.getLog(ConnectionPool.class);  
  134.   
  135.     private static ConnectionPool _instance = new ConnectionPool();  
  136.   
  137.     private Properties _props;  
  138.     private ComboPooledDataSource _cpds;  
  139.   
  140. }  


当然还得在包的根目录下建一个配置文件connection-pool.properties,内容如下: 

driver.class=com.mysql.jdbc.Driver 
jdbc.url= 
user= 
password= 

min.pool.size=5 
max.pool.size=20 
acquire.increment=5 

在配置文件中填上相应的值,以后就可以直接使用此类获得数据库连接了。需要注意的是用完之后要手动关闭连接。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值