KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > threetier > ConnectionPool


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
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 /**
32  * <p>
33  * <b>Purpose</b>: Used to specify how connection should be pooled in a server session.
34  * @see ServerSession
35  */

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 JavaDoc name;
44     protected ServerSession owner;
45
46     /**
47      * PUBLIC:
48      * A connection pool is used to specify how connection should be pooled in a server session.
49      */

50     public ConnectionPool() {
51         this.maxNumberOfConnections = 50;
52         this.minNumberOfConnections = 3;
53         resetConnections();
54     }
55
56     /**
57      * PUBLIC:
58      * A connection pool is used to specify how connection should be pooled in a server session.
59      */

60     public ConnectionPool(String JavaDoc 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     /**
70      * INTERNAL:
71      * Wait until a connection is avaiable and allocate the connection for the client.
72      */

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();// Notify is called when connections are released.
82
} catch (InterruptedException JavaDoc 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 JavaDoc(getConnectionsUsed().size()));
92         return connection;
93     }
94
95     /**
96      * INTERNAL:
97      * Create a new connection, accessors are used as connections.
98      */

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     /**
108      * INTERNAL:
109      * returns the connections currently available for use in the pool
110      */

111     public Vector getConnectionsAvailable() {
112         return connectionsAvailable;
113     }
114
115     /**
116      * Return a list of the connections that are being used.
117      * @return java.util.Vector
118      **/

119     protected Vector getConnectionsUsed() {
120         return connectionsUsed;
121     }
122
123     /**
124      * PUBLIC:
125      * Return the login used to create connections.
126      */

127     public Login getLogin() {
128         return login;
129     }
130
131     /**
132      * PUBLIC:
133      * Return the maximum number of connections allowed.
134      * When the max is reached clients must wait for a connection to become available.
135      */

136     public int getMaxNumberOfConnections() {
137         return maxNumberOfConnections;
138     }
139
140     /**
141      * PUBLIC:
142      * Return the minimum number of connections.
143      * These connection will be create on startup.
144      */

145     public int getMinNumberOfConnections() {
146         return minNumberOfConnections;
147     }
148
149     /**
150      * PUBLIC:
151      * Return the name of this pool.
152      * Pools are identified by name to allow multiple connection pools.
153      */

154     public String JavaDoc getName() {
155         return name;
156     }
157
158     /**
159      * Return the ServerSession that is the owner of this connection pool.
160      * @return oracle.toplink.essentials.threetier.ServerSession
161      */

162     protected ServerSession getOwner() {
163         return owner;
164     }
165
166     /**
167      * INTERNAL:
168      * Return the total number of connections currently in use.
169      */

170     public int getTotalNumberOfConnections() {
171         return getConnectionsUsed().size() + getConnectionsAvailable().size();
172     }
173
174     /**
175      * INTERNAL:
176      * Wait until a connection is avaiable and allocate the connection for the client.
177      */

178     public boolean hasConnectionAvailable() {
179         return !getConnectionsAvailable().isEmpty();
180     }
181
182     /**
183      * INTERNAL:
184      * Return if this pool has been connected to the database.
185      */

186     public boolean isConnected() {
187         return isConnected;
188     }
189
190     /**
191      * INTERNAL:
192      * Checks for a conflict between pool's type and pool's login
193      */

194     public boolean isThereConflictBetweenLoginAndType() {
195         return getLogin().shouldUseExternalConnectionPooling();
196     }
197
198     /**
199      * INTERNAL:
200      * Add the connection as single that a new connection is available.
201      */

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     /**
215      * INTERNAL:
216      * Reset the connections on shutDown and when the pool is started.
217      */

218     public void resetConnections() {
219         this.connectionsUsed = new Vector();
220         this.connectionsAvailable = new Vector();
221     }
222
223     /**
224      * INTERNAL:
225      * Set this list of connections available
226      * @param java.util.Vector
227      */

228     protected void setConnectionsAvailable(Vector connectionsAvailable) {
229         this.connectionsAvailable = connectionsAvailable;
230     }
231
232     /**
233      * INTERNAL:
234      * Set the list of connections being used.
235      * @param java.util.Vector
236      */

237     protected void setConnectionsUsed(Vector connectionsUsed) {
238         this.connectionsUsed = connectionsUsed;
239     }
240
241     /**
242      * INTERNAL:
243      * Set if this pool has been connected to the database.
244      */

245     public void setIsConnected(boolean isConnected) {
246         this.isConnected = isConnected;
247     }
248
249     /**
250      * PUBLIC:
251      * Set the login used to create connections.
252      */

253     public void setLogin(Login login) {
254         this.login = login;
255     }
256
257     /**
258      * PUBLIC:
259      * Set the maximum number of connections allowed.
260      * When the max is reached clients must wait for a connection to become available.
261      */

262     public void setMaxNumberOfConnections(int maxNumberOfConnections) {
263         this.maxNumberOfConnections = maxNumberOfConnections;
264     }
265
266     /**
267      * PUBLIC:
268      * Set the minimum number of connections.
269      * These connection will be create on startup.
270      */

271     public void setMinNumberOfConnections(int minNumberOfConnections) {
272         this.minNumberOfConnections = minNumberOfConnections;
273     }
274
275     /**
276      * PUBLIC:
277      * Set the name of this pool.
278      * Pools are identified by name to allow multiple connection pools.
279      */

280     public void setName(String JavaDoc name) {
281         this.name = name;
282     }
283
284     /**
285      * Set the ServerSession that owns this connection pool
286      * @param oracle.toplink.essentials.threetier.ServerSession
287      */

288     protected void setOwner(ServerSession owner) {
289         this.owner = owner;
290     }
291
292     /**
293      * INTERNAL:
294      * Disconnect all connections.
295      */

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                 // Ignore.
305
}
306         }
307
308         for (Enumeration usedEnum = getConnectionsUsed().elements(); usedEnum.hasMoreElements();) {
309             try {
310                 ((Accessor)usedEnum.nextElement()).disconnect(getOwner());
311             } catch (DatabaseException exception) {
312                 // Ignore.
313
}
314         }
315         resetConnections();
316     }
317
318     /**
319      * INTERNAL:
320      * Allocate the minimum connections.
321      */

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     /**
331      * INTERNAL:
332      * return a string representation of this connection pool
333      */

334     public String JavaDoc toString() {
335         Object JavaDoc[] args = { new Integer JavaDoc(getMinNumberOfConnections()), new Integer JavaDoc(getMaxNumberOfConnections()) };
336         return Helper.getShortClassName(getClass()) + ToStringLocalization.buildMessage("min_max", args);
337     }
338 }
339
Popular Tags