KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > connection > FailFastPoolConnectionManager


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): ______________________.
22  */

23
24 package org.continuent.sequoia.controller.connection;
25
26 import java.sql.Connection JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28
29 import org.continuent.sequoia.common.exceptions.UnreachableBackendException;
30 import org.continuent.sequoia.common.i18n.Translate;
31 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
32
33 /**
34  * This connection manager returns <code>null</code> when the pool is empty.
35  * Therefore all requests fail fast until connections are freed.
36  *
37  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

41 public class FailFastPoolConnectionManager
42     extends AbstractPoolConnectionManager
43 {
44
45   /**
46    * Creates a new <code>FailFastPoolConnectionManager</code> instance.
47    *
48    * @param backendUrl URL of the <code>DatabaseBackend</code> owning this
49    * connection manager
50    * @param backendName name of the <code>DatabaseBackend</code> owning this
51    * connection manager
52    * @param login backend connection login to be used by this connection manager
53    * @param password backend connection password to be used by this connection
54    * manager
55    * @param driverPath path for driver
56    * @param driverClassName class name for driver
57    * @param poolSize size of the connection pool
58    */

59   public FailFastPoolConnectionManager(String JavaDoc backendUrl, String JavaDoc backendName,
60       String JavaDoc login, String JavaDoc password, String JavaDoc driverPath, String JavaDoc driverClassName,
61       int poolSize)
62   {
63     super(backendUrl, backendName, login, password, driverPath,
64         driverClassName, poolSize);
65   }
66
67   /**
68    * Gets a connection from the pool. Returns <code>null</code> if the pool is
69    * empty.
70    *
71    * @return a connection from the pool or <code>null</code> if the pool is
72    * exhausted
73    * @throws UnreachableBackendException if the backend must be disabled
74    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#getConnection()
75    */

76   public synchronized PooledConnection getConnection()
77       throws UnreachableBackendException
78   {
79     if (!initialized)
80     {
81       logger.error(Translate.get("connection.request.not.initialized"));
82       return null;
83     }
84     if (isShutdown)
85     {
86       return null;
87     }
88     try
89     { // Both freeConnections and activeConnections are synchronized
90
PooledConnection c = (PooledConnection) freeConnections.removeLast();
91       activeConnections.add(c);
92       return c;
93     }
94     catch (NoSuchElementException JavaDoc e)
95     { // No free connection
96
int missing = poolSize
97           - (activeConnections.size() + freeConnections.size());
98       if (missing > 0)
99       { // Re-allocate missing connections
100
logger.info(Translate.get("connection.reallocate.missing", missing));
101         PooledConnection connectionToBeReturned = null;
102         while (missing > 0)
103         {
104           Connection JavaDoc c = getConnectionFromDriver();
105           if (c == null)
106           {
107             if (missing == poolSize)
108             {
109               logger.error(Translate.get("connection.backend.unreachable",
110                   backendName));
111               throw new UnreachableBackendException();
112             }
113             logger.warn(Translate.get("connection.reallocate.failed", missing));
114             break;
115           }
116           else
117           {
118             if (connectionToBeReturned == null)
119               connectionToBeReturned = new PooledConnection(c);
120             else
121               freeConnections.addLast(new PooledConnection(c));
122           }
123           missing--;
124         }
125         return connectionToBeReturned;
126       }
127       if (logger.isWarnEnabled())
128         logger.warn(Translate.get("connection.backend.out.of.connections",
129             new String JavaDoc[]{backendName, String.valueOf(poolSize)}));
130       return null;
131     }
132   }
133
134   /**
135    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#releaseConnection(org.continuent.sequoia.controller.connection.PooledConnection)
136    */

137   public synchronized void releaseConnection(PooledConnection c)
138   {
139     if (!initialized)
140     {
141       closeConnection(c);
142       return; // We probably have been disabled
143
}
144
145     if (activeConnections.remove(c))
146       freeConnections.addLast(c);
147     else
148       logger.error(Translate.get("connection.release.failed", c.toString()));
149   }
150
151   /**
152    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#deleteConnection(org.continuent.sequoia.controller.connection.PooledConnection)
153    */

154   public synchronized void deleteConnection(PooledConnection c)
155   {
156     closeConnection(c);
157     if (!initialized)
158       return; // We probably have been disabled
159

160     if (activeConnections.remove(c))
161     {
162       Connection JavaDoc newConnection = getConnectionFromDriver();
163       if (newConnection == null)
164       {
165         if (logger.isDebugEnabled())
166           logger.error(Translate
167               .get("connection.replaced.failed", c.toString()));
168       }
169       else
170       {
171         freeConnections.addLast(newConnection);
172         if (logger.isDebugEnabled())
173           logger.debug(Translate.get("connection.replaced.success", c
174               .toString()));
175       }
176     }
177     else
178       logger.error(Translate.get("connection.replaced.failed.exception", c
179           .toString()));
180   }
181
182   /**
183    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#getXmlImpl()
184    */

185   public String JavaDoc getXmlImpl()
186   {
187     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
188     info.append("<" + DatabasesXmlTags.ELT_FailFastPoolConnectionManager + " "
189         + DatabasesXmlTags.ATT_poolSize + "=\"" + poolSize / 1000 + "\"/>");
190     return info.toString();
191   }
192
193   /**
194    * @see java.lang.Object#clone()
195    */

196   protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
197   {
198     return new FailFastPoolConnectionManager(backendUrl, backendName, rLogin,
199         rPassword, driverPath, driverClassName, poolSize);
200   }
201
202   /**
203    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#clone(String,
204    * String)
205    */

206   public AbstractConnectionManager clone(String JavaDoc rLogin, String JavaDoc rPassword)
207   {
208     return new FailFastPoolConnectionManager(backendUrl, backendName, rLogin,
209         rPassword, driverPath, driverClassName, poolSize);
210   }
211
212 }
213
Popular Tags