KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > connection > FailFastPoolConnectionManager


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

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

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

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

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

135   public synchronized void releaseConnection(Connection JavaDoc c)
136   {
137     if (!initialized)
138       return; // We probably have been disabled
139

140     if (activeConnections.remove(c))
141       freeConnections.addLast(c);
142     else
143       logger.error(Translate.get("connection.release.failed", c.toString()));
144   }
145
146   /**
147    * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager#deleteConnection(Connection)
148    */

149   public synchronized void deleteConnection(Connection JavaDoc c)
150   {
151     if (!initialized)
152       return; // We probably have been disabled
153

154     if (activeConnections.remove(c))
155     {
156       Connection JavaDoc newConnection = getConnectionFromDriver();
157       if (newConnection == null)
158       {
159         if (logger.isDebugEnabled())
160           logger.error(Translate
161               .get("connection.replaced.failed", c.toString()));
162       }
163       else
164       {
165         freeConnections.addLast(newConnection);
166         if (logger.isDebugEnabled())
167           logger.debug(Translate.get("connection.replaced.success", c
168               .toString()));
169       }
170     }
171     else
172       logger.error(Translate.get("connection.replaced.failed.exception", c
173           .toString()));
174   }
175
176   /**
177    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getXmlImpl()
178    */

179   public String JavaDoc getXmlImpl()
180   {
181     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
182     info.append("<" + DatabasesXmlTags.ELT_FailFastPoolConnectionManager + " "
183         + DatabasesXmlTags.ATT_poolSize + "=\"" + poolSize / 1000 + "\"/>");
184     return info.toString();
185   }
186
187   /**
188    * @see java.lang.Object#clone()
189    */

190   protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
191   {
192     return new FailFastPoolConnectionManager(backendUrl, backendName, rLogin,
193         rPassword, driverPath, driverClassName, poolSize);
194   }
195 }
196
Popular Tags