数据库连接池

来源:互联网 发布:西门子840d攻丝编程 编辑:程序博客网 时间:2024/06/06 03:03

一个数据库连接池,顾名思义。就是放一堆连接,在需要时就从其中找到空闲的连接,并且使用;而使用完毕后,就可以将连接返回到数据库连接池中。

SqlHelper为获得连接的方法:

[java] view plaincopy
  1. import java.sql.*;  
  2. import java.sql.Connection;  
  3. /** 
  4.  * 
  5.  * @author Administrator 
  6.  */  
  7. //返回Connection  
  8. public class SqlHelper {  
  9.   
  10.     public static Connection getConnection()  
  11.     {  
  12.         Connection conn=null;  
  13.         String url1="com.microsoft.jdbc.sqlserver.SQLServerDriver";  
  14.         String url2="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";  
  15.         String username="sa";  
  16.         String pwd="";  
  17.         try  
  18.         {  
  19.             Class.forName(url1);  
  20.             conn=DriverManager.getConnection(url2,username,pwd);  
  21.         }  
  22.         catch(Exception ex)  
  23.         {  
  24.             ex.printStackTrace();  
  25.         }  
  26.         return conn;  
  27.     }  
  28. }  

ConnectionPool为数据库连接池:

[java] view plaincopy
  1. import java.sql.*;  
  2. import java.util.Hashtable;  
  3. import java.util.logging.Level;  
  4. import java.util.logging.Logger;  
  5. /** 
  6.  * 
  7.  * @author Administrator 
  8.  */  
  9. public class ConnectionPool {  
  10.   
  11.     //可用连接数为10  
  12.     private static final int CONNECTIONPOOL_SIZE=10;  
  13.     //Hash表,用于存放连接  
  14.     private Hashtable connectionPool=null;  
  15.     //用于存放连接的使用情况,true为已经被使用,而false表示尚未被使用  
  16.     private boolean[] connectionPool_status=null;  
  17.     //唯一的一个ConnectionPool  
  18.     private static ConnectionPool self=null;  
  19.   
  20.     //初始化  
  21.     public synchronized static void init()  
  22.     {  
  23.         self=new ConnectionPool();  
  24.         self.connectionPool=new Hashtable();  
  25.         self.connectionPool_status=new boolean[CONNECTIONPOOL_SIZE];  
  26.         buildConnection();  
  27.     }  
  28.     //建立所有连接  
  29.     public synchronized static void buildConnection()  
  30.     {  
  31.          if(self==null)  
  32.              init();  
  33.   
  34.          for(int i=0;i<CONNECTIONPOOL_SIZE;i++)  
  35.          {  
  36.              Connection conn=SqlHelper.getConnection();  
  37.              self.connectionPool.put(i, conn);  
  38.              self.connectionPool_status[i]=false;  
  39.          }  
  40.     }  
  41.     //释放所有连接  
  42.     public synchronized static void releaseAllConnection()  
  43.     {  
  44.         if(self==null)  
  45.             init();  
  46.   
  47.         for(int i=0;i<CONNECTIONPOOL_SIZE;i++)  
  48.         {  
  49.             try {  
  50.                 Connection conn = (Connection) self.connectionPool.get(i);  
  51.                 conn.close();  
  52.             } catch (SQLException ex) {  
  53.                 Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);  
  54.             }  
  55.         }  
  56.   
  57.         System.out.println("已经释放所有连接");  
  58.     }  
  59.     //获得连接  
  60.     public static Connection getConnection()  
  61.     {  
  62.         boolean tag;  
  63.         Connection conn=null;  
  64.         int i;  
  65.         if(self==null)  
  66.             init();  
  67.           
  68.         for( i=0;i<CONNECTIONPOOL_SIZE;i++)  
  69.         {  
  70.             if((tag=self.connectionPool_status[i])!=true)  
  71.             {  
  72.                 conn=(Connection)self.connectionPool.get(i);  
  73.                 self.connectionPool_status[i]=true;  
  74.                 break;  
  75.             }  
  76.         }  
  77.         if(i>=CONNECTIONPOOL_SIZE)  
  78.         {  
  79.             System.out.println("没有可用连接");  
  80.         }  
  81.         return conn;  
  82.     }  
  83.     //返还连接  
  84.     public static void returnConnection(Connection conn)  
  85.     {  
  86.         if(self==null)  
  87.             init();  
  88.   
  89.         for(int i=0;i<CONNECTIONPOOL_SIZE;i++)  
  90.         {  
  91.             if(self.connectionPool.get(i).equals(conn))  
  92.             {  
  93.                 self.connectionPool_status[i]=false;  
  94.                 System.out.println("返还连接");  
  95.                 break;  
  96.             }  
  97.         }  
  98.     }  
  99.     //重建立连接  
  100.     public static void rebuildConnection(Connection conn)  
  101.     {  
  102.         if(self==null)  
  103.             init();  
  104.   
  105.          for(int i=0;i<CONNECTIONPOOL_SIZE;i++)  
  106.         {  
  107.             if(self.connectionPool.get(i).equals(conn))  
  108.             {  
  109.                 Connection conn1=SqlHelper.getConnection();  
  110.                 self.connectionPool.put(i, conn1);  
  111.                 self.connectionPool_status[i]=true;  
  112.                 System.out.println("重建连接");  
  113.                 break;  
  114.             }  
  115.         }  
  116.     }  
  117.     //重置数据库连接池  
  118.     public synchronized static void reset()  
  119.     {  
  120.         self=null;  
  121.         init();  
  122.     }  
  123. }  

http://blog.csdn.net/rongyongfeikai2/article/details/5946834
0 0
原创粉丝点击