001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Emmanuel Cecchet.
021: * Contributor(s): ______________________.
022: */package org.continuent.sequoia.controller.connection;
023:
024: import java.sql.Connection;
025: import java.sql.SQLException;
026:
027: import org.continuent.sequoia.common.exceptions.UnreachableBackendException;
028: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
029:
030: /**
031: * This connection manager creates a new <code>Connection</code> every time
032: * the {@link #getConnection}method is called.
033: *
034: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
035: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
036: * @version 1.0
037: */
038: public class SimpleConnectionManager extends AbstractConnectionManager {
039: private int nbOfConnections = 0;
040:
041: /**
042: * Creates a new <code>SimpleConnectionManager</code> instance.
043: *
044: * @param backendUrl URL of the <code>DatabaseBackend</code> owning this
045: * connection manager.
046: * @param backendName name of the <code>DatabaseBackend</code> owning this
047: * connection manager.
048: * @param login backend connection login to be used by this connection
049: * manager.
050: * @param password backend connection password to be used by this connection
051: * manager.
052: * @param driverPath path for driver
053: * @param driverClassName class name for driver
054: */
055: public SimpleConnectionManager(String backendUrl,
056: String backendName, String login, String password,
057: String driverPath, String driverClassName) {
058: super (backendUrl, backendName, login, password, driverPath,
059: driverClassName);
060: }
061:
062: /**
063: * @see java.lang.Object#clone()
064: */
065: protected Object clone() throws CloneNotSupportedException {
066: return new SimpleConnectionManager(backendUrl, backendName,
067: rLogin, rPassword, driverPath, driverClassName);
068: }
069:
070: /**
071: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#clone(String,
072: * String)
073: */
074: public AbstractConnectionManager clone(String rLogin,
075: String rPassword) {
076: return new SimpleConnectionManager(backendUrl, backendName,
077: rLogin, rPassword, driverPath, driverClassName);
078: }
079:
080: /**
081: * Does nothing.
082: *
083: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#doConnectionInitialization()
084: */
085: protected void doConnectionInitialization() throws SQLException {
086: initialized = true;
087: if (idlePersistentConnectionPingInterval > 0) {
088: persistentConnectionPingerThread = new IdlePersistentConnectionsPingerThread(
089: backendName, this );
090: persistentConnectionPingerThread.start();
091: idlePersistentConnectionPingRunning = true;
092: }
093: }
094:
095: /**
096: * Does nothing.
097: *
098: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#doConnectionFinalization()
099: */
100: protected void doConnectionFinalization() throws SQLException {
101: initialized = false;
102: }
103:
104: /**
105: * Gets a new connection from the underlying driver.
106: *
107: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#getConnection()
108: */
109: public PooledConnection getConnection()
110: throws UnreachableBackendException {
111: if (!initialized) {
112: logger
113: .error("Requesting a connection from a non-initialized connection manager");
114: return null;
115: }
116: if (isShutdown) {
117: return null;
118: }
119: addConnection();
120: Connection c = getConnectionFromDriver();
121: if (c == null) {
122: removeConnection();
123: logger.error("Unable to get connection from " + backendUrl);
124: if (nbOfConnections == 0) {
125: logger
126: .error("Backend '"
127: + backendUrl
128: + "' is considered unreachable. "
129: + "(No active connection and none can be opened)");
130: throw new UnreachableBackendException();
131: }
132: }
133: return new PooledConnection(c);
134: }
135:
136: /**
137: * Closes the connection.
138: *
139: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#releaseConnection(PooledConnection)
140: */
141: public void releaseConnection(PooledConnection connection) {
142: removeConnection();
143: try {
144: connection.close();
145: } catch (SQLException e) {
146: logger.error("Failed to close connection for '"
147: + backendUrl + "'", e);
148: }
149: }
150:
151: /**
152: * Closes the persistent connection.
153: *
154: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#releasePersistentConnection(PooledConnection)
155: */
156: protected void releasePersistentConnection(PooledConnection c) {
157: releaseConnection(c);
158: }
159:
160: /**
161: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#deleteConnection(PooledConnection)
162: */
163: public void deleteConnection(PooledConnection c) {
164: }
165:
166: /**
167: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#flagAllConnectionsForRenewal()
168: */
169: public void flagAllConnectionsForRenewal() {
170: // Nothing to do here, all connections are always renewed
171: }
172:
173: /**
174: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#getCurrentNumberOfConnections()
175: */
176: public int getCurrentNumberOfConnections() {
177: return nbOfConnections;
178: }
179:
180: private synchronized void addConnection() {
181: nbOfConnections++;
182: }
183:
184: private synchronized void removeConnection() {
185: nbOfConnections--;
186: }
187:
188: /**
189: * @see org.continuent.sequoia.controller.connection.AbstractConnectionManager#getXmlImpl()
190: */
191: public String getXmlImpl() {
192: return "<" + DatabasesXmlTags.ELT_SimpleConnectionManager
193: + "/>";
194: }
195:
196: }
|