1 21 package oracle.toplink.essentials.threetier; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.databaseaccess.*; 26 import oracle.toplink.essentials.sessions.Login; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.internal.localization.*; 30 31 36 public class ConnectionPool { 37 protected boolean isConnected; 38 protected int maxNumberOfConnections; 39 protected int minNumberOfConnections; 40 protected Vector connectionsAvailable; 41 protected Vector connectionsUsed; 42 protected Login login; 43 protected String name; 44 protected ServerSession owner; 45 46 50 public ConnectionPool() { 51 this.maxNumberOfConnections = 50; 52 this.minNumberOfConnections = 3; 53 resetConnections(); 54 } 55 56 60 public ConnectionPool(String name, Login login, int minNumberOfConnections, int maxNumberOfConnections, ServerSession owner) { 61 this.login = login; 62 this.owner = owner; 63 this.name = name; 64 this.maxNumberOfConnections = maxNumberOfConnections; 65 this.minNumberOfConnections = minNumberOfConnections; 66 resetConnections(); 67 } 68 69 73 public synchronized Accessor acquireConnection() throws ConcurrencyException { 74 while (!hasConnectionAvailable()) { 75 if (getTotalNumberOfConnections() < getMaxNumberOfConnections()) { 76 Accessor connection = buildConnection(); 77 getConnectionsUsed().addElement(connection); 78 return connection; 79 } 80 try { 81 wait(); } catch (InterruptedException exception) { 83 throw ConcurrencyException.waitFailureOnClientSession(exception); 84 } 85 } 86 87 Accessor connection = (Accessor)getConnectionsAvailable().firstElement(); 88 getConnectionsAvailable().removeElement(connection); 89 getConnectionsUsed().addElement(connection); 90 91 getOwner().updateProfile(getName(), new Integer (getConnectionsUsed().size())); 92 return connection; 93 } 94 95 99 protected Accessor buildConnection() { 100 Login localLogin = (Login)getLogin().clone(); 101 Accessor connection = localLogin.buildAccessor(); 102 connection.connect(localLogin, getOwner()); 103 104 return connection; 105 } 106 107 111 public Vector getConnectionsAvailable() { 112 return connectionsAvailable; 113 } 114 115 119 protected Vector getConnectionsUsed() { 120 return connectionsUsed; 121 } 122 123 127 public Login getLogin() { 128 return login; 129 } 130 131 136 public int getMaxNumberOfConnections() { 137 return maxNumberOfConnections; 138 } 139 140 145 public int getMinNumberOfConnections() { 146 return minNumberOfConnections; 147 } 148 149 154 public String getName() { 155 return name; 156 } 157 158 162 protected ServerSession getOwner() { 163 return owner; 164 } 165 166 170 public int getTotalNumberOfConnections() { 171 return getConnectionsUsed().size() + getConnectionsAvailable().size(); 172 } 173 174 178 public boolean hasConnectionAvailable() { 179 return !getConnectionsAvailable().isEmpty(); 180 } 181 182 186 public boolean isConnected() { 187 return isConnected; 188 } 189 190 194 public boolean isThereConflictBetweenLoginAndType() { 195 return getLogin().shouldUseExternalConnectionPooling(); 196 } 197 198 202 public synchronized void releaseConnection(Accessor connection) throws DatabaseException { 203 getConnectionsUsed().removeElement(connection); 204 205 if (getTotalNumberOfConnections() < getMinNumberOfConnections()) { 206 getConnectionsAvailable().addElement(connection); 207 } else { 208 connection.disconnect(getOwner()); 209 } 210 211 notify(); 212 } 213 214 218 public void resetConnections() { 219 this.connectionsUsed = new Vector(); 220 this.connectionsAvailable = new Vector(); 221 } 222 223 228 protected void setConnectionsAvailable(Vector connectionsAvailable) { 229 this.connectionsAvailable = connectionsAvailable; 230 } 231 232 237 protected void setConnectionsUsed(Vector connectionsUsed) { 238 this.connectionsUsed = connectionsUsed; 239 } 240 241 245 public void setIsConnected(boolean isConnected) { 246 this.isConnected = isConnected; 247 } 248 249 253 public void setLogin(Login login) { 254 this.login = login; 255 } 256 257 262 public void setMaxNumberOfConnections(int maxNumberOfConnections) { 263 this.maxNumberOfConnections = maxNumberOfConnections; 264 } 265 266 271 public void setMinNumberOfConnections(int minNumberOfConnections) { 272 this.minNumberOfConnections = minNumberOfConnections; 273 } 274 275 280 public void setName(String name) { 281 this.name = name; 282 } 283 284 288 protected void setOwner(ServerSession owner) { 289 this.owner = owner; 290 } 291 292 296 public synchronized void shutDown() { 297 setIsConnected(false); 298 299 for (Enumeration avaiableEnum = getConnectionsAvailable().elements(); 300 avaiableEnum.hasMoreElements();) { 301 try { 302 ((Accessor)avaiableEnum.nextElement()).disconnect(getOwner()); 303 } catch (DatabaseException exception) { 304 } 306 } 307 308 for (Enumeration usedEnum = getConnectionsUsed().elements(); usedEnum.hasMoreElements();) { 309 try { 310 ((Accessor)usedEnum.nextElement()).disconnect(getOwner()); 311 } catch (DatabaseException exception) { 312 } 314 } 315 resetConnections(); 316 } 317 318 322 public synchronized void startUp() { 323 for (int index = getMinNumberOfConnections(); index > 0; index--) { 324 getConnectionsAvailable().addElement(buildConnection()); 325 } 326 327 setIsConnected(true); 328 } 329 330 334 public String toString() { 335 Object [] args = { new Integer (getMinNumberOfConnections()), new Integer (getMaxNumberOfConnections()) }; 336 return Helper.getShortClassName(getClass()) + ToStringLocalization.buildMessage("min_max", args); 337 } 338 } 339 | Popular Tags |