java之編寫數據庫連接池實現連接的復用


    一般我們在操作數據庫時候,需要頻繁的打開和關閉連接,而創建數據庫連接往往開銷比較大,因而我們需要避免這種情況的發生,在這里我們可以創建一個連接池,當操作數據的時候,我們從連接池中取出連接,操作完畢后再將連接放回到池中。

    在這里我們需要用到集合,大家知道ArrayList結合其實是一個數組,它讀取數據的時候速度比較快,而LinkedList集合在操作的時候要比ArrayList要快的多,所以這里我們選擇集合LinkedList。

1.編寫一個連接池的類

 1 package cn.mycast.bank.db;
 2 
 3 import java.sql.Connection;
 4 import java.util.LinkedList;
 5 import cn.mycast.bank.util.JdbcUtil;
 6 public class MyDatabasePool {
 7     private LinkedList<Connection> connPool=new LinkedList<Connection>();//存放連接
 8     public MyDatabasePool(){
 9         for(int i=0;i<10;i++)//連接池中存放10個連接
10         {
11             this.connPool.addLast(this.CreateConnection());//每次添加到集合最后面
12         }
13     }
14     public Connection CreateConnection(){//獲得連接
15         return JdbcUtil.getConnection();
16     }
17     public Connection GetConnection(){
18         return connPool.removeFirst();//取出最上面的一個連接
19     }
20     public void FreeConnection(Connection conn){//將用完后的連接放回到集合中
21         this.connPool.addLast(conn);
22     }
23 }

2.編寫一個操作數據庫連接的工具類

 1 package cn.mycast.bank.util;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 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 import java.util.Properties;
11 
12 import cn.mycast.bank.db.MyDatabasePool;
13 public class JdbcUtil {
14     private static String driver;
15     private static String url;
16     private static String username;
17     private static String password;
18     private static MyDatabasePool connPool;//連接池類
19     static{
20         // 通過類加載器獲得資源,並以流的方式進行操作
21         InputStream is=JdbcUtil.class.getClassLoader().getResourceAsStream("cn/mycast/bank/util/Mysqldb.properties");
22         Properties properties=new Properties();
23         try {
24             properties.load(is);
25             url=properties.getProperty("url");
26             driver=properties.getProperty("driver");
27             username=properties.getProperty("username");
28             password=properties.getProperty("password");
29         } catch (IOException e) {
30             e.printStackTrace();
31         }
32     }
33     static{
34         try {
35             Class.forName(driver);//注冊驅動
36             connPool=new MyDatabasePool();
37         } catch (ClassNotFoundException e) {
38             e.printStackTrace();
39         }
40     }
41     
42     public static Connection getConnection(){
43         Connection conn=null;
44         try {
45             conn=DriverManager.getConnection(url, username, password);//獲得連接
46         } catch (SQLException e) {
47             e.printStackTrace();
48         }finally{
49             
50         }
51         return conn;
52     }
53     public static void close(Connection conn){
54         if(conn!=null)
55             connPool.FreeConnection(conn); //將連接放回集合
56     }
57     public static void close(Statement stm){
58         try {
59             if(stm!=null)
60                 stm.close();
61         } catch (SQLException e) {
62             e.printStackTrace();
63         }
64     }
65     public static void close(ResultSet rs){
66         try {
67             if(rs!=null)
68                 rs.close();
69         } catch (SQLException e) {
70             e.printStackTrace();
71         }
72     }
73 }

后面只需調用該工具類的相關方法即可

其實可以注意到,在獲取數據庫連接的時候,如果是多個線程並發的獲取的話,當連接池中沒有連接的時候,那么這候再獲取的時候就會報錯,而且該操作屬於非線程安全的,這里

需要加上同步代碼塊,避免多個線程獲取同一個連接。

 1 public Connection CreateConnection(){//獲得連接
 2       synchronized(connectionsPool){
 3             if(this.connectionsPool.size()>0){
 4                    return JdbcUtil.getConnection();
 5             }
 6             if(this.currentCount<MaxCount){
 7                    this.currentCount++;
 8                    return this.CreateConnection();
 9             }
10         }    
11 } 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM