KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
29
30 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
31 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
32
33 /**
34  * This connection manager creates a new <code>Connection</code> every time
35  * the {@link #getConnection}method is called.
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 SimpleConnectionManager extends AbstractConnectionManager
42 {
43   private int nbOfConnections = 0;
44
45   /**
46    * Creates a new <code>SimpleConnectionManager</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
53    * 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    */

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

68   protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
69   {
70     return new SimpleConnectionManager(backendUrl, backendName, rLogin,
71         rPassword, driverPath, driverClassName);
72   }
73
74   /**
75    * Does nothing.
76    *
77    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#initializeConnections()
78    */

79   public void initializeConnections() throws SQLException JavaDoc
80   {
81     initialized = true;
82   }
83
84   /**
85    * Does nothing.
86    *
87    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#finalizeConnections()
88    */

89   public void finalizeConnections() throws SQLException JavaDoc
90   {
91     initialized = false;
92   }
93
94   /**
95    * Gets a new connection from the underlying driver.
96    *
97    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getConnection()
98    */

99   public Connection JavaDoc getConnection() throws UnreachableBackendException
100   {
101     if (!initialized)
102     {
103       logger
104           .error("Requesting a connection from a non-initialized connection manager");
105       return null;
106     }
107
108     addConnection();
109     Connection JavaDoc c = getConnectionFromDriver();
110     if (c == null)
111     {
112       removeConnection();
113       logger.error("Unable to get connection from " + backendUrl);
114       if (nbOfConnections == 0)
115       {
116         logger.error("Backend '" + backendUrl + "' is considered unreachable. "
117             + "(No active connection and none can be opened)");
118         throw new UnreachableBackendException();
119       }
120     }
121     return c;
122   }
123
124   /**
125    * Closes the connection.
126    *
127    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#releaseConnection(Connection)
128    */

129   public void releaseConnection(Connection JavaDoc connection)
130   {
131     removeConnection();
132     try
133     {
134       connection.close();
135     }
136     catch (SQLException JavaDoc e)
137     {
138       logger.error("Failed to close connection for '" + backendUrl + "'", e);
139     }
140   }
141
142   /**
143    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#deleteConnection(Connection)
144    */

145   public void deleteConnection(Connection JavaDoc c)
146   {
147   }
148
149   /**
150    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getCurrentNumberOfConnections()
151    */

152   public int getCurrentNumberOfConnections()
153   {
154     return nbOfConnections;
155   }
156
157   private synchronized void addConnection()
158   {
159     nbOfConnections++;
160   }
161
162   private synchronized void removeConnection()
163   {
164     nbOfConnections--;
165   }
166
167   /**
168    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getXmlImpl()
169    */

170   public String JavaDoc getXmlImpl()
171   {
172     return "<" + DatabasesXmlTags.ELT_SimpleConnectionManager + "/>";
173   }
174
175 }
Popular Tags