KickJava   Java API By Example, From Geeks To Geeks.

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


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.exceptions.*;
28
29 /**
30  * <p>
31  * <b>Purpose</b>:The read connection pool is used for read access through the server session.
32  * Any of the connection pools can be used for the read pool however this is the default.
33  * This pool allows for concurrent reads against the same JDBC connection and requires that
34  * the JDBC connection support concurrent read access.
35  */

36 public class ReadConnectionPool extends ConnectionPool {
37
38     /**
39      * PUBLIC:
40      * Build a new read connection pool.
41      */

42     public ReadConnectionPool() {
43         super();
44     }
45
46     /**
47      * PUBLIC:
48      * Build a new read connection pool.
49      */

50     public ReadConnectionPool(String JavaDoc name, Login login, int minNumberOfConnections, int maxNumberOfConnections, ServerSession owner) {
51         super(name, login, minNumberOfConnections, maxNumberOfConnections, owner);
52     }
53
54     /**
55      * INTERNAL:
56      * Wait until a connection is avaiable and allocate the connection for the client.
57      */

58     public synchronized Accessor acquireConnection() throws ConcurrencyException {
59         Accessor leastBusyConnection = null;
60
61         // Search for an unused connection, also find the least busy incase all are used.
62
for (Enumeration connectionsEnum = getConnectionsAvailable().elements();
63                  connectionsEnum.hasMoreElements();) {
64             Accessor connection = (Accessor)connectionsEnum.nextElement();
65             if (connection.getCallCount() == 0) {
66                 connection.incrementCallCount(getOwner());
67                 return connection;
68             }
69             if ((leastBusyConnection == null) || (leastBusyConnection.getCallCount() > connection.getCallCount())) {
70                 leastBusyConnection = connection;
71             }
72         }
73
74         // If still not at max, add a new connection.
75
if (getTotalNumberOfConnections() < getMaxNumberOfConnections()) {
76             Accessor connection = buildConnection();
77             getConnectionsAvailable().addElement(connection);
78             connection.incrementCallCount(getOwner());
79             return connection;
80         }
81
82         // Use the least busy connection.
83
leastBusyConnection.incrementCallCount(getOwner());
84         return leastBusyConnection;
85     }
86
87     /**
88      * INTERNAL:
89      * Concurrent reads are supported.
90      */

91     public boolean hasConnectionAvailable() {
92         return true;
93     }
94
95     /**
96      * INTERNAL:
97      * Because connections are not exclusive nothing is required.
98      */

99     public synchronized void releaseConnection(Accessor connection) throws DatabaseException {
100         connection.decrementCallCount();
101     }
102 }
103
Popular Tags