00001
00025
package org.objectweb.cjdbc.controller.connection;
00026
00027
import java.io.Serializable;
00028
import java.sql.Connection;
00029
import java.util.EmptyStackException;
00030
00031
import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
00032
import org.objectweb.cjdbc.common.i18n.Translate;
00033
import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00034
00043 public class FailFastPoolConnectionManager
00044
extends
00045
AbstractPoolConnectionManager implements Serializable
00046 {
00047
00063 public FailFastPoolConnectionManager(String backendUrl, String backendName,
00064 String login, String password, String driverPath, String driverClassName,
00065
int poolSize)
00066 {
00067 super(backendUrl, backendName, login, password, driverPath,
00068 driverClassName, poolSize);
00069 }
00070
00080 public Connection
getConnection() throws
UnreachableBackendException
00081 {
00082
if (!initialized)
00083 {
00084 logger.
error(
Translate.get(
"connection.request.not.initialized"));
00085
return null;
00086 }
00087
00088
try
00089 {
00090 Connection c = (Connection) freeConnections.pop();
00091 activeConnections.add(c);
00092
return c;
00093 }
00094
catch (EmptyStackException e)
00095 {
00096
synchronized (
this)
00097 {
00098
int missing = poolSize
00099 - (activeConnections.size() + freeConnections.size());
00100
if (missing > 0)
00101 {
00102 logger.
info(
Translate.get(
"connection.reallocate.missing", missing));
00103 Connection connectionToBeReturned = null;
00104
while (missing > 0)
00105 {
00106 Connection c =
getConnectionFromDriver();
00107
if (c == null)
00108 {
00109
if (missing == poolSize)
00110 {
00111 logger.
error(
Translate.get(
"connection.backend.unreachable",
00112 backendName));
00113
throw new UnreachableBackendException();
00114 }
00115 logger.
warn(
Translate
00116 .get(
"connection.reallocate.failed", missing));
00117
break;
00118 }
00119
else
00120 {
00121
if (connectionToBeReturned == null)
00122 connectionToBeReturned = c;
00123
else
00124 freeConnections.add(c);
00125 }
00126 missing--;
00127 }
00128
return connectionToBeReturned;
00129 }
00130 }
00131
if (logger.
isWarnEnabled())
00132 logger.
warn(
Translate.get(
"connection.backend.out.of.connections",
00133
new String[]{backendName, String.valueOf(poolSize)}));
00134
return null;
00135 }
00136 }
00137
00141 public synchronized void releaseConnection(Connection c)
00142 {
00143
if (!initialized)
00144
return;
00145
00146
if (activeConnections.remove(c))
00147 freeConnections.push(c);
00148
else
00149 logger.error(
Translate.get(
"connection.release.failed", c.toString()));
00150 }
00151
00155 public synchronized void deleteConnection(Connection c)
00156 {
00157
if (!initialized)
00158
return;
00159
00160
if (activeConnections.remove(c))
00161 {
00162 Connection newConnection = getConnectionFromDriver();
00163
if (newConnection == null)
00164 {
00165
if (logger.isDebugEnabled())
00166 logger.error(
Translate
00167 .get(
"connection.replaced.failed", c.toString()));
00168 }
00169
else
00170 {
00171 freeConnections.push(newConnection);
00172
if (logger.isDebugEnabled())
00173 logger.debug(
Translate.get(
"connection.replaced.success", c
00174 .toString()));
00175 }
00176 }
00177
else
00178 logger.error(
Translate.get(
"connection.replaced.failed.exception", c
00179 .toString()));
00180 }
00181
00185 public String getXmlImpl()
00186 {
00187 StringBuffer info =
new StringBuffer();
00188 info.append(
"<" +
DatabasesXmlTags.ELT_FailFastPoolConnectionManager +
" "
00189 +
DatabasesXmlTags.ATT_poolSize +
"=\"" + poolSize / 1000 +
"\"/>");
00190
return info.toString();
00191 }
00192
00196 protected Object clone() throws CloneNotSupportedException
00197 {
00198
return new FailFastPoolConnectionManager(backendUrl, backendName, rLogin,
00199 rPassword, driverPath, driverClassName, poolSize);
00200 }
00201 }