uopspop

Untitled

Oct 6th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3.  
  4. public class ConnPool {
  5.     private static final int defaultMaxConnections = 3;
  6.  
  7.     private Vector<Connection> freeConnections;             //JDK1.5 Generic
  8.     private Hashtable<Thread, Connection> boundConnections; //JDK1.5 Generic
  9.     private String driverName;
  10.     private String jdbcURL;
  11.     private String username;
  12.     private String password;
  13.     private int maxConnections;
  14.  
  15.     public ConnPool() {
  16.         this(defaultMaxConnections);
  17.     }
  18.  
  19.     public ConnPool(int numConnections) {
  20.         maxConnections = numConnections;
  21.         boundConnections = null;
  22.         freeConnections = null;
  23.         driverName = "";
  24.         jdbcURL = "";
  25.         username = "";
  26.         password = "";
  27.     }
  28.  
  29.     public void setDriverName(String drvName) {
  30.         driverName = drvName;
  31.     }
  32.  
  33.     public void setJdbcURL(String url) {
  34.         jdbcURL = url;
  35.     }
  36.  
  37.     public void setUserName(String uname) {
  38.         username = uname;
  39.     }
  40.  
  41.     public void setPassword(String passwd) {
  42.         password = passwd;
  43.     }
  44.  
  45.     public void setMaxConnections(int numConnections) {
  46.         maxConnections = numConnections;
  47.     }
  48.  
  49.     public void setConnectionSwitch(String on_off) throws SQLException {
  50.         if (on_off.equalsIgnoreCase("ON"))
  51.             openDB(driverName, jdbcURL, username, password);
  52.         else if (on_off.equalsIgnoreCase("OFF"))
  53.             closeDB();
  54.     }
  55.  
  56.     public void openDB(String drvName, String url, String uname, String passwd)
  57.             throws SQLException {
  58.         try {
  59.             boundConnections = new Hashtable<Thread, Connection>();
  60.             freeConnections = new Vector<Connection>();
  61.             Class.forName(drvName);
  62.             for (int i = 0; i < maxConnections; i++)
  63.                 freeConnections.add(DriverManager.getConnection(url, uname, passwd));
  64.         } catch (Exception ex) {
  65.             boundConnections = null;
  66.             freeConnections = null;
  67.             throw new SQLException(ex.toString());
  68.         }
  69.     }
  70.  
  71.     public void closeDB() throws SQLException {
  72.         if (boundConnections != null) {
  73.             for (Connection conn : boundConnections.values()) { //JDK1.5 for-each
  74.                 conn.close();
  75.             }
  76.             boundConnections.clear();
  77.             boundConnections = null;
  78.         }
  79.  
  80.         if (freeConnections != null) {
  81.             for (Connection conn : freeConnections) { //JDK1.5 for-each
  82.                 conn.close();
  83.             }
  84.             freeConnections.removeAllElements();
  85.             freeConnections = null;
  86.         }
  87.     }
  88.  
  89.     public synchronized Connection getConnection() throws SQLException {
  90.         if (freeConnections == null)
  91.             throw new SQLException(
  92.                     "The conection pool has not been established yet.");
  93.         if (boundConnections.get(Thread.currentThread()) != null)
  94.             throw new SQLException(
  95.                     "Cannot get connections over once for this current running thread.");
  96.         try {
  97.             if (freeConnections.size() == 0) {
  98.                 System.out
  99.                         .println(Thread.currentThread().getName()
  100.                                 + " is waiting..............................................");
  101.                 wait();
  102.             }
  103.         } catch (InterruptedException ex) {
  104.             throw new SQLException(ex.toString());
  105.         }
  106.         Connection conn = freeConnections.firstElement();
  107.         freeConnections.removeElement(conn);
  108.         boundConnections.put(Thread.currentThread(), conn);
  109.  
  110.         return conn;
  111.     }
  112.  
  113.     public synchronized void returnConnection() throws SQLException {
  114.         Connection conn = boundConnections.remove(Thread.currentThread());
  115.         if (conn == null)
  116.             throw new SQLException(
  117.                     "The connection which this current running thread got is not found.");
  118.         freeConnections.add(conn);
  119.         notify();
  120.     }
  121.  
  122. }
Add Comment
Please, Sign In to add comment