uopspop

Untitled

Oct 7th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package connectionPool;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7.  
  8. public class ConnectionPool {
  9.     private static int maxConnectionCount = 3;
  10.     private ArrayList<Connection> connectionPool;
  11.     private HashMap<Thread, Connection> connectionInUse;
  12.     private String url = "jdbc:oracle:thin:@localhost:1521:xe";
  13.     private String account = "user08";
  14.     private String password = "1111";
  15.     static{
  16.         try {
  17.             Class.forName("oracle.jdbc.driver.OracleDriver");
  18.         } catch (ClassNotFoundException e) {
  19.             e.printStackTrace();
  20.         }
  21.     }
  22.     public ConnectionPool(){}
  23.    
  24.     public void openDB(int aConnectionCount){
  25.         connectionPool = new ArrayList<>();
  26.         connectionInUse = new HashMap<>();
  27.         addConnection(aConnectionCount);
  28.  
  29.     }
  30.    
  31.     public void closeDB(){
  32.         if (connectionInUse != null) {
  33.             for (Connection conn : connectionInUse.values()) { //JDK1.5 for-each
  34.                 try {
  35.                     conn.close();
  36.                 } catch (SQLException e) {
  37.                     e.printStackTrace();
  38.                 }
  39.             }
  40.             connectionInUse.clear();
  41.             connectionInUse = null;
  42.         }
  43.  
  44.         if (connectionPool != null) {
  45.             for (Connection conn : connectionPool) { //JDK1.5 for-each
  46.                 try {
  47.                     conn.close();
  48.                     System.out.println("Connection in pool closed.");
  49.                 } catch (SQLException e) {
  50.                     e.printStackTrace();
  51.                 }
  52.             }
  53.             connectionPool.clear();
  54.             connectionPool = null;
  55.         }
  56.     }
  57.    
  58.    
  59.     public synchronized Connection getConnection(){
  60.         Connection con = null;
  61.         int totalConnections = connectionPool.size() + connectionInUse.size();
  62.        
  63.         // dynamically create Connection object until it reaches its maximum limit
  64.         if (connectionPool.size() == 0 && totalConnections < maxConnectionCount){
  65.             addConnection(1);
  66.             System.out.println("create new Connection.");
  67.         }else if (connectionPool.size() == 0 && !(totalConnections < maxConnectionCount)){
  68.             System.out.println("reach the maximum number of Connections. Can't create Connection anymore.");
  69.         }
  70.        
  71.         // if no connection available now, tell the current Thread to wait
  72.         if (connectionPool.size() == 0){
  73.             System.out.println("This Thread is waiting**************************************************");
  74.             try {
  75.                 wait(); // look for notifyAll() - to see when will this code keeps going down.
  76.             } catch (InterruptedException e) {
  77.                 // TODO Auto-generated catch block
  78.                 e.printStackTrace();
  79.             }
  80.             System.out.println("This Thread gets Connection.**************************************************");
  81.         }
  82.        
  83.         // if connection available, give one to the current Thread
  84.         if (connectionPool.size() > 0){
  85.             con = connectionPool.remove(connectionPool.size()-1);                      
  86.             connectionInUse.put(Thread.currentThread(), con);
  87.         }
  88.         return con;
  89.     }
  90.    
  91.     public synchronized void returnConnection(Connection con){
  92.         connectionPool.add(con);
  93.         connectionInUse.remove(Thread.currentThread());
  94.         //notifyAll();
  95.         notify(); //一次叫一個Thread起來就好,不然會出事,因為若同時兩個Thread起來一起往下跑,會只有一個人拿到Connection,那另一個人就會NullPointerException
  96.     }
  97.    
  98.     private synchronized void addConnection(int aConnectionCount){
  99.  
  100.         try {
  101.             for(int i = 0; i < aConnectionCount; i++){
  102.                 Connection con =  DriverManager.getConnection(url, account, password);
  103.                 connectionPool.add(con);               
  104.             }
  105.         } catch (SQLException e) {
  106.             // TODO Auto-generated catch block
  107.             e.printStackTrace();
  108.         }
  109.     }
  110.    
  111.     public int size(){
  112.         return this.connectionPool.size();
  113.     }
  114.  
  115.    
  116. }
Add Comment
Please, Sign In to add comment