KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
28
29 import org.continuent.sequoia.common.exceptions.UnreachableBackendException;
30 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
31
32 /**
33  * This connection manager creates a new <code>Connection</code> every time
34  * the {@link #getConnection}method is called.
35  *
36  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
38  * @version 1.0
39  */

40 public class SimpleConnectionManager extends AbstractConnectionManager
41 {
42   private int nbOfConnections = 0;
43
44   /**
45    * Creates a new <code>SimpleConnectionManager</code> instance.
46    *
47    * @param backendUrl URL of the <code>DatabaseBackend</code> owning this
48    * connection manager.
49    * @param backendName name of the <code>DatabaseBackend</code> owning this
50    * connection manager.
51    * @param login backend connection login to be used by this connection
52    * 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    */

58   public SimpleConnectionManager(String JavaDoc backendUrl, String JavaDoc backendName,
59       String JavaDoc login, String JavaDoc password, String JavaDoc driverPath, String JavaDoc driverClassName)
60   {
61     super(backendUrl, backendName, login, password, driverPath, driverClassName);
62   }
63
64   /**
65    * @see java.lang.Object#clone()
66    */

67   protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
68   {
69     return new SimpleConnectionManager(backendUrl, backendName, rLogin,
70         rPassword, driverPath, driverClassName);
71   }
72    
73   /**
74    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#clone(String,
75    * String)
76    */

77   public AbstractConnectionManager clone(String JavaDoc rLogin, String JavaDoc rPassword)
78   {
79     return new SimpleConnectionManager(backendUrl, backendName, rLogin,
80         rPassword, driverPath, driverClassName);
81   }
82
83   /**
84    * Does nothing.
85    *
86    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#doConnectionInitialization()
87    */

88   protected void doConnectionInitialization() throws SQLException JavaDoc
89   {
90     initialized = true;
91     if (idlePersistentConnectionPingInterval > 0)
92     {
93       persistentConnectionPingerThread = new IdlePersistentConnectionsPingerThread(
94           backendName, this);
95       persistentConnectionPingerThread.start();
96       idlePersistentConnectionPingRunning = true;
97     }
98   }
99
100   /**
101    * Does nothing.
102    *
103    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#doConnectionFinalization()
104    */

105   protected void doConnectionFinalization() throws SQLException JavaDoc
106   {
107     initialized = false;
108   }
109
110   /**
111    * Gets a new connection from the underlying driver.
112    *
113    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#getConnection()
114    */

115   public PooledConnection getConnection() throws UnreachableBackendException
116   {
117     if (!initialized)
118     {
119       logger
120           .error("Requesting a connection from a non-initialized connection manager");
121       return null;
122     }
123     if (isShutdown)
124     {
125       return null;
126     }
127     addConnection();
128     Connection JavaDoc c = getConnectionFromDriver();
129     if (c == null)
130     {
131       removeConnection();
132       logger.error("Unable to get connection from " + backendUrl);
133       if (nbOfConnections == 0)
134       {
135         logger.error("Backend '" + backendUrl + "' is considered unreachable. "
136             + "(No active connection and none can be opened)");
137         throw new UnreachableBackendException();
138       }
139     }
140     return new PooledConnection(c);
141   }
142
143   /**
144    * Closes the connection.
145    *
146    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#releaseConnection(PooledConnection)
147    */

148   public void releaseConnection(PooledConnection connection)
149   {
150     removeConnection();
151     try
152     {
153       connection.getConnection().close();
154     }
155     catch (SQLException JavaDoc e)
156     {
157       logger.error("Failed to close connection for '" + backendUrl + "'", e);
158     }
159   }
160
161   /**
162    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#deleteConnection(PooledConnection)
163    */

164   public void deleteConnection(PooledConnection c)
165   {
166   }
167
168   /**
169    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#flagAllConnectionsForRenewal()
170    */

171   public void flagAllConnectionsForRenewal()
172   {
173     // Nothing to do here, all connections are always renewed
174
}
175
176   /**
177    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#getCurrentNumberOfConnections()
178    */

179   public int getCurrentNumberOfConnections()
180   {
181     return nbOfConnections;
182   }
183
184   private synchronized void addConnection()
185   {
186     nbOfConnections++;
187   }
188
189   private synchronized void removeConnection()
190   {
191     nbOfConnections--;
192   }
193
194   /**
195    * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#getXmlImpl()
196    */

197   public String JavaDoc getXmlImpl()
198   {
199     return "<" + DatabasesXmlTags.ELT_SimpleConnectionManager + "/>";
200   }
201
202 }
Popular Tags