自己实现数据库连接池

1 数据库配置

配置项的值来自一个叫mysql_en.properties的文件(该文件应该放到对应的CLASSPATH的目录。

mysql_en.properties

 

[xhtml] view plain copy
  1. driver=com.mysql.jdbc.Driver  
  2. url=jdbc:mysql://localhost:3306/dfs  
  3. user=root  
  4. password=123456  
  5. characterEncoding=utf8  
  6. minPoolSize = 3  
  7. maxPoolSize = 10  
 

 

 

假设我们用来读取配置文件的class叫ResourceManager,我们使用jdk的ResourceBundle来读取配置文件。

 

  1. package dbConnectionPool;  
  2. import java.util.Locale;  
  3. import java.util.ResourceBundle;  
  4. public class ResourceManager {  
  5.     private static ResourceBundle r;  
  6.     static {  
  7.         r = ResourceBundle.getBundle("mysql", Locale.ENGLISH);  
  8.     }  
  9.     public static String getDriverClass() {  
  10.         return r.getString("driver");  
  11.     }  
  12.     public static String getUrl() {  
  13.         return r.getString("url");  
  14.     }  
  15.     public static String getUsername() {  
  16.         return r.getString("user");  
  17.     }  
  18.     public static String getPassword() {  
  19.         return r.getString("password");  
  20.     }  
  21.     public static String getCharacterEncoding() {  
  22.         return r.getString("characterEncoding");  
  23.     }  
  24.     public static int getMinPoolSize() {  
  25.         int poolSize = Integer.valueOf(r.getString("minPoolSize"));  
  26.         return poolSize;  
  27.     }  
  28.     public static int getMaxPoolSize() {  
  29.         int poolSize = Integer.valueOf(r.getString("maxPoolSize"));  
  30.         return poolSize;  
  31.     }  
  32.     public static void refresh() {  
  33.         r = ResourceBundle.getBundle("resourceBundle");  
  34.     }  
  35. }  
 

 

只需要告诉ResourceBundle文件名是"mysql"就足够了。下划线和后面的"en"表示的是本地化信息。这里的en代表"ENGLISH",后缀properties是默认的。

 

2  连接池代码

 

  1. package dbConnectionPool;  
  2. import java.sql.Connection;  
  3. import java.sql.DriverManager;  
  4. import java.sql.SQLException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. public class DbConnectionPool {  
  8.     private List<Connection> pool;  
  9.     private int minSize = 3;  
  10.     private int maxSize = 10;  
  11.     private static DbConnectionPool instance = null;  
  12.     /** 
  13.      *得到当前连接池的一个实例 
  14.      */  
  15.     public static DbConnectionPool getInstance() {  
  16.         if (instance == null) {  
  17.             instance = new DbConnectionPool();  
  18.         }  
  19.         return instance;  
  20.     }  
  21.     /** 
  22.      *单例模式私有构造方法,获得本类的对象,通过getIstance方法。 
  23.      */  
  24.     private DbConnectionPool() {  
  25.         pool = new ArrayList<Connection>();  
  26.         int min_Size = ResourceManager.getMinPoolSize();  
  27.         int max_Size = ResourceManager.getMaxPoolSize();  
  28.         if (min_Size > 0 && max_Size > 0 && min_Size < max_Size) {  
  29.             this.minSize = min_Size;  
  30.             this.maxSize = max_Size;  
  31.         }  
  32.         System.out.println(this.minSize);  
  33.         System.out.println(this.maxSize);  
  34.         initPool();  
  35.     }  
  36.     /** 
  37.      *连接池初始化,生成最小数目的连接 
  38.      */  
  39.     private void initPool() {  
  40.         Connection conn = null;  
  41.         for (int i = 0; i < minSize; i++) {  
  42.             conn = createConnection();  
  43.             if (conn != null)  
  44.                 pool.add(conn);  
  45.         }  
  46.     }  
  47.     /** 
  48.      *创建连接 
  49.      */  
  50.     private Connection createConnection() {  
  51.         Connection conn = null;  
  52.         try {  
  53.             Class.forName(ResourceManager.getDriverClass());  
  54.             conn = DriverManager.getConnection(ResourceManager.getUrl()  
  55.                     + "?characterEncoding="  
  56.                     + ResourceManager.getCharacterEncoding(), ResourceManager  
  57.                     .getUsername(), ResourceManager.getPassword());  
  58.         } catch (ClassNotFoundException e) {  
  59.             e.printStackTrace();  
  60.         } catch (SQLException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.         return conn;  
  64.     }  
  65.     /** 
  66.      *得到连接池中的一个连接 
  67.      */  
  68.     public synchronized Connection getConnection() {  
  69.         if (pool.size() > 0) {  
  70.             Connection conn = pool.get(0);  
  71.             pool.remove(conn);  
  72.             return conn;  
  73.         } else {  
  74.             return createConnection();  
  75.         }  
  76.     }  
  77.     /** 
  78.      *用完将连接放回到连接池中 
  79.      */  
  80.     public synchronized void releaseConnection(Connection conn) {  
  81.         if (pool.size() < maxSize)  
  82.             pool.add(conn);  
  83.         else  
  84.             try {  
  85.                 conn.close();  
  86.                 conn = null;  
  87.             } catch (SQLException e) {  
  88.                 e.printStackTrace();  
  89.             }  
  90.     }  
  91.     /** 
  92.      *关闭连接池中的所有连接 
  93.      */  
  94.     public synchronized void closePool() {  
  95.         Connection conn = null;  
  96.         for (int i = 0; i < pool.size(); i++) {  
  97.             try {  
  98.                 conn = (Connection) pool.get(i);  
  99.                 conn.close();  
  100.                 pool.remove(i);  
  101.             } catch (SQLException e) {  
  102.                 e.printStackTrace();  
  103.             }  
  104.         }  
  105.     }  
  106.       
  107.     public synchronized int getConnectionNum(){  
  108.         return pool.size();  
  109.     }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值