1 package org.jahia.tools.db; 2 3 import java.sql.*; 4 import java.util.*; 5 6 14 15 public class ConnectionPool implements Runnable { 16 private String driver, url, username, password; 17 private int maxConnections; 18 private boolean waitIfBusy; 19 private Vector availableConnections, busyConnections; 20 private boolean connectionPending = false; 21 22 public ConnectionPool(String driver, String url, 23 String username, String password, 24 int initialConnections, 25 int maxConnections, 26 boolean waitIfBusy) 27 throws SQLException { 28 this.driver = driver; 29 this.url = url; 30 this.username = username; 31 this.password = password; 32 this.maxConnections = maxConnections; 33 this.waitIfBusy = waitIfBusy; 34 if (initialConnections > maxConnections) { 35 initialConnections = maxConnections; 36 } 37 availableConnections = new Vector(initialConnections); 38 busyConnections = new Vector(); 39 for(int i=0; i<initialConnections; i++) { 40 availableConnections.addElement(makeNewConnection()); 41 } 42 } 43 44 public synchronized Connection getConnection() 45 throws SQLException { 46 if (!availableConnections.isEmpty()) { 47 Connection existingConnection = 48 (Connection)availableConnections.lastElement(); 49 int lastIndex = availableConnections.size() - 1; 50 availableConnections.removeElementAt(lastIndex); 51 if (existingConnection.isClosed()) { 57 notifyAll(); return(getConnection()); 59 } else { 60 busyConnections.addElement(existingConnection); 61 return(existingConnection); 62 } 63 } else { 64 65 77 if ((totalConnections() < maxConnections) && 78 !connectionPending) { 79 makeBackgroundConnection(); 80 } else if (!waitIfBusy) { 81 throw new SQLException("Connection limit reached"); 82 } 83 try { 87 wait(); 88 } catch(InterruptedException ie) {} 89 return(getConnection()); 91 } 92 } 93 94 102 private void makeBackgroundConnection() { 103 connectionPending = true; 104 try { 105 Thread connectThread = new Thread (this); 106 connectThread.start(); 107 } catch(OutOfMemoryError oome) { 108 } 110 } 111 112 public void run() { 113 try { 114 Connection connection = makeNewConnection(); 115 synchronized(this) { 116 availableConnections.addElement(connection); 117 connectionPending = false; 118 notifyAll(); 119 } 120 } catch(Exception e) { } 124 } 125 126 130 private Connection makeNewConnection() 131 throws SQLException { 132 try { 133 Class.forName(driver); 135 Connection connection = 137 DriverManager.getConnection(url, username, password); 138 return(connection); 139 } catch(ClassNotFoundException cnfe) { 140 throw new SQLException("Can't find class for driver: " + 143 driver); 144 } 145 } 146 147 public synchronized void free(Connection connection) { 148 busyConnections.removeElement(connection); 149 availableConnections.addElement(connection); 150 notifyAll(); 152 153 } 154 155 public synchronized int totalConnections() { 156 return(availableConnections.size() + 157 busyConnections.size()); 158 } 159 160 168 169 public synchronized void closeAllConnections() { 170 closeConnections(availableConnections); 171 availableConnections = new Vector(); 172 closeConnections(busyConnections); 173 busyConnections = new Vector(); 174 } 175 176 private void closeConnections(Vector connections) { 177 try { 178 for(int i=0; i<connections.size(); i++) { 179 Connection connection = 180 (Connection)connections.elementAt(i); 181 if (!connection.isClosed()) { 182 connection.close(); 183 } 184 } 185 } catch(SQLException sqle) { 186 } 188 } 189 190 public synchronized String toString() { 191 String info = 192 "ConnectionPool(" + url + "," + username + ")" + 193 ", available=" + availableConnections.size() + 194 ", busy=" + busyConnections.size() + 195 ", max=" + maxConnections; 196 return(info); 197 } 198 } 199 | Popular Tags |