00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 import java.util.Stack;
00031 import java.util.Vector;
00032
00033 import org.objectweb.cjdbc.common.i18n.Translate;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 public abstract class AbstractPoolConnectionManager
00049 extends
00050 AbstractConnectionManager implements Serializable
00051 {
00052
00053
00054
00055
00056
00057
00058 protected transient Stack freeConnections;
00059
00060
00061
00062
00063
00064 protected transient Vector activeConnections;
00065
00066
00067 protected int poolSize;
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 public AbstractPoolConnectionManager(String backendUrl, String backendName,
00089 String login, String password, String driverPath, String driverClassName,
00090 int poolSize)
00091 {
00092 super(backendUrl, backendName, login, password, driverPath, driverClassName);
00093
00094 if (poolSize < 1)
00095 throw new IllegalArgumentException(
00096 "Illegal value for size of the pool connection manager: " + poolSize);
00097
00098 this.poolSize = poolSize;
00099 this.freeConnections = new Stack();
00100 this.activeConnections = new Vector(poolSize);
00101 this.initialized = false;
00102
00103 if (logger.isDebugEnabled())
00104 logger.debug(Translate.get("connection.backend.pool.created",
00105 new String[]{backendName, String.valueOf(poolSize)}));
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115 public synchronized void initializeConnections() throws SQLException
00116 {
00117 initializeConnections(poolSize);
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 public synchronized void initializeConnections(int initPoolSize)
00127 throws SQLException
00128 {
00129 if (initialized)
00130 throw new SQLException("Connection pool for backend '" + backendUrl
00131 + "' already initialized");
00132
00133 if (initPoolSize > poolSize)
00134 {
00135 logger.warn(Translate.get("connection.max.poolsize.reached",
00136 new String[]{String.valueOf(initPoolSize), String.valueOf(poolSize),
00137 String.valueOf(poolSize)}));
00138 initPoolSize = poolSize;
00139 }
00140
00141 Connection c = null;
00142
00143 boolean connectionsAvailable = true;
00144 int i = 0;
00145 while ((i < initPoolSize) && connectionsAvailable)
00146 {
00147 c = getConnectionFromDriver();
00148
00149 if (c == null)
00150 connectionsAvailable = false;
00151
00152 if (!connectionsAvailable)
00153 {
00154 if (i > 0)
00155 {
00156 logger.warn(Translate.get("connection.limit.poolsize", i));
00157 }
00158 else
00159 {
00160 logger.warn(Translate.get("connection.initialize.pool.failed"));
00161 poolSize = 0;
00162 }
00163 }
00164 else
00165 {
00166 freeConnections.push(c);
00167 i++;
00168 }
00169 }
00170
00171 poolSize = i;
00172 initialized = true;
00173
00174 if (poolSize == 0)
00175 logger.error(Translate.get("connection.empty.pool"));
00176 if (logger.isDebugEnabled())
00177 logger.debug(Translate.get("connection.pool.initialized", new String[]{
00178 String.valueOf(initPoolSize), backendUrl}));
00179
00180 }
00181
00182
00183
00184
00185 public synchronized void finalizeConnections() throws SQLException
00186 {
00187 if (!initialized)
00188 {
00189 String msg = Translate.get("connection.pool.not.initialized");
00190 logger.error(msg);
00191 throw new SQLException(msg);
00192 }
00193
00194 Connection c;
00195 boolean error = false;
00196
00197
00198 initialized = false;
00199 int freed = 0;
00200 while (!freeConnections.isEmpty())
00201 {
00202 c = (Connection) freeConnections.pop();
00203 try
00204 {
00205 c.close();
00206 }
00207 catch (SQLException e)
00208 {
00209 error = true;
00210 }
00211 freed++;
00212 }
00213 if (logger.isInfoEnabled())
00214 logger.info(Translate.get("connection.freed.connection", new String[]{
00215 String.valueOf(freed), backendUrl}));
00216
00217
00218 int size = activeConnections.size();
00219 if (size > 0)
00220 {
00221 logger.warn(Translate.get("connection.connections.still.active", size));
00222 for (int i = 0; i < size; i++)
00223 {
00224 c = (Connection) activeConnections.get(i);
00225 try
00226 {
00227 c.close();
00228 }
00229 catch (SQLException e)
00230 {
00231 error = true;
00232 }
00233 }
00234 }
00235
00236
00237
00238 freeConnections.clear();
00239 activeConnections.clear();
00240
00241 if (error)
00242 {
00243 String msg = Translate.get("connection.free.connections.failed");
00244 logger.error(msg);
00245 throw new SQLException(msg);
00246 }
00247 }
00248
00249
00250
00251
00252 public int getCurrentNumberOfConnections()
00253 {
00254 return poolSize;
00255 }
00256
00257 }