Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

SimpleConnectionManager.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.connection;
00026 
00027 import java.io.Serializable;
00028 import java.sql.Connection;
00029 import java.sql.SQLException;
00030 
00031 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
00032 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00033 
00034 /**
00035  * This connection manager creates a new <code>Connection</code> every time
00036  * the {@link #getConnection}method is called.
00037  * 
00038  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00039  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00040  * @version 1.0
00041  */
00042 public class SimpleConnectionManager extends AbstractConnectionManager
00043     implements
00044       Serializable
00045 {
00046   private int nbOfConnections = 0;
00047 
00048   /**
00049    * Creates a new <code>SimpleConnectionManager</code> instance.
00050    * 
00051    * @param backendUrl URL of the <code>DatabaseBackend</code> owning this
00052    *          connection manager.
00053    * @param backendName name of the <code>DatabaseBackend</code> owning this
00054    *          connection manager.
00055    * @param login backend connection login to be used by this connection
00056    *          manager.
00057    * @param password backend connection password to be used by this connection
00058    *          manager.
00059    * @param driverPath path for driver
00060    * @param driverClassName class name for driver
00061    */
00062   public SimpleConnectionManager(String backendUrl, String backendName,
00063       String login, String password, String driverPath, String driverClassName)
00064   {
00065     super(backendUrl, backendName, login, password, driverPath, driverClassName);
00066   }
00067 
00068   /**
00069    * @see java.lang.Object#clone()
00070    */
00071   protected Object clone() throws CloneNotSupportedException
00072   {
00073     return new SimpleConnectionManager(backendUrl, backendName, rLogin,
00074         rPassword, driverPath, driverClassName);
00075   }
00076 
00077   /**
00078    * Does nothing.
00079    * 
00080    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#initializeConnections()
00081    */
00082   public void initializeConnections() throws SQLException
00083   {
00084     initialized = true;
00085   }
00086 
00087   /**
00088    * Does nothing.
00089    * 
00090    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#finalizeConnections()
00091    */
00092   public void finalizeConnections() throws SQLException
00093   {
00094     initialized = false;
00095   }
00096 
00097   /**
00098    * Gets a new connection from the underlying driver.
00099    * 
00100    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getConnection()
00101    */
00102   public Connection getConnection() throws UnreachableBackendException
00103   {
00104     if (!initialized)
00105     {
00106       logger
00107           .error("Requesting a connection from a non-initialized connection manager");
00108       return null;
00109     }
00110 
00111     addConnection();
00112     Connection c = getConnectionFromDriver();
00113     if (c == null)
00114     {
00115       removeConnection();
00116       logger.error("Unable to get connection from " + backendUrl);
00117       if (nbOfConnections == 0)
00118       {
00119         logger.error("Backend '" + backendUrl + "' is considered unreachable. "
00120             + "(No active connection and none can be opened)");
00121         throw new UnreachableBackendException();
00122       }
00123     }
00124     return c;
00125   }
00126 
00127   /**
00128    * Closes the connection.
00129    * 
00130    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#releaseConnection(Connection)
00131    */
00132   public void releaseConnection(Connection connection)
00133   {
00134     removeConnection();
00135     try
00136     {
00137       connection.close();
00138     }
00139     catch (SQLException e)
00140     {
00141       logger.error("Failed to close connection for '" + backendUrl + "'", e);
00142     }
00143   }
00144 
00145   /**
00146    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#deleteConnection(Connection)
00147    */
00148   public void deleteConnection(Connection c)
00149   {
00150   }
00151 
00152   /**
00153    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getCurrentNumberOfConnections()
00154    */
00155   public int getCurrentNumberOfConnections()
00156   {
00157     return nbOfConnections;
00158   }
00159 
00160   private synchronized void addConnection()
00161   {
00162     nbOfConnections++;
00163   }
00164 
00165   private synchronized void removeConnection()
00166   {
00167     nbOfConnections--;
00168   }
00169 
00170   /**
00171    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getXmlImpl()
00172    */
00173   public String getXmlImpl()
00174   {
00175     return "<" + DatabasesXmlTags.ELT_SimpleConnectionManager + "/>";
00176   }
00177 
00178 }

Generated on Mon Apr 11 22:01:34 2005 for C-JDBC by  doxygen 1.3.9.1