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

org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager Class Reference

Inheritance diagram for org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager:

Inheritance graph
[legend]
Collaboration diagram for org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 VariablePoolConnectionManager (String backendUrl, String backendName, String rLogin, String rPassword, String driverPath, String driverClassName, int minPoolSize, int maxPoolSize, int idleTimeout, int waitTimeout)
 VariablePoolConnectionManager (String backendUrl, String backendName, String rLogin, String rPassword, String driverPath, String driverClassName, int initPoolSize, int minPoolSize, int maxPoolSize, int idleTimeout, int waitTimeout)
int getMaxPoolSize ()
int getMinPoolSize ()
int getIdleTimeout ()
int getWaitTimeout ()
synchronized void initializeConnections () throws SQLException
synchronized void finalizeConnections () throws SQLException
Connection getConnection () throws UnreachableBackendException
void releaseConnection (Connection c)
void deleteConnection (Connection c)
String getXmlImpl ()

Static Public Attributes

final int DEFAULT_MAX_POOL_SIZE = 0
final int DEFAULT_IDLE_TIMEOUT = 0
final int DEFAULT_WAIT_TIMEOUT = 0

Protected Member Functions

Object clone () throws CloneNotSupportedException

Detailed Description

This connection manager provides connection pooling with a dynamically adjustable pool size.

If the maximum number of active connections is not reached, the getConnection()method creates a connection. Else, the execution is blocked until a connection is freed or the timeout expires. blocked until a connection is freed or the timeout expires.

Idle connections in the pool are removed after the timeout idleTimeout if the minimum pool size has not been reached.

Author:
Emmanuel Cecchet

Mathieu Peltier

Nicolas Modrzyk

Version:
1.0

Definition at line 54 of file VariablePoolConnectionManager.java.


Constructor & Destructor Documentation

org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.VariablePoolConnectionManager String  backendUrl,
String  backendName,
String  rLogin,
String  rPassword,
String  driverPath,
String  driverClassName,
int  minPoolSize,
int  maxPoolSize,
int  idleTimeout,
int  waitTimeout
 

Creates a new VariablePoolConnectionManager instance with the default minPoolSize(initial pool size to be initialized at startup).

Parameters:
backendUrl URL of the DatabaseBackend owning this connection manager
backendName name of the DatabaseBackend owning this connection manager
rLogin backend connection login to be used by this connection manager
rPassword backend connection password to be used by this connection manager
driverPath path for driver
driverClassName class name for driver
minPoolSize minimum pool size.
maxPoolSize maximum pool size. 0 means no limit.
idleTimeout time a connection can stay idle before begin released (removed from the pool) in seconds. 0 means no timeout: once allocated, connections are never released.
waitTimeout maximum time to wait for a connection in seconds. 0 means no timeout: waits until one connection is freed.

Definition at line 119 of file VariablePoolConnectionManager.java.

Referenced by org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.clone().

00123   {
00124     this(backendUrl, backendName, rLogin, rPassword, driverPath,
00125         driverClassName, minPoolSize, minPoolSize, maxPoolSize, idleTimeout,
00126         waitTimeout);
00127   }

org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.VariablePoolConnectionManager String  backendUrl,
String  backendName,
String  rLogin,
String  rPassword,
String  driverPath,
String  driverClassName,
int  initPoolSize,
int  minPoolSize,
int  maxPoolSize,
int  idleTimeout,
int  waitTimeout
 

Creates a new VariablePoolConnectionManager instance.

Parameters:
backendUrl URL of the DatabaseBackend owning this connection manager
backendName name of the DatabaseBackend owning this connection manager
rLogin backend connection login to be used by this connection manager
rPassword backend connection password to be used by this connection manager
driverPath path for driver
driverClassName class name for driver
initPoolSize initial pool size to be intialized at startup
minPoolSize minimum pool size.
maxPoolSize maximum pool size. 0 means no limit.
idleTimeout time a connection can stay idle before begin released (removed from the pool) in seconds. 0 means no timeout: once allocated, connections are never released.
waitTimeout maximum time to wait for a connection in seconds. 0 means no timeout: waits until one connection is freed.

Definition at line 161 of file VariablePoolConnectionManager.java.

00165   {
00166     super(backendUrl, backendName, rLogin, rPassword, driverPath,
00167         driverClassName, maxPoolSize == 0 ? (initPoolSize > minPoolSize
00168             ? initPoolSize
00169             : minPoolSize) : maxPoolSize);
00170     this.initPoolSize = initPoolSize;
00171     this.minPoolSize = minPoolSize;
00172     this.maxPoolSize = maxPoolSize;
00173     this.idleTimeout = idleTimeout * 1000;
00174     this.waitTimeout = waitTimeout * 1000;
00175   }


Member Function Documentation

Object org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.clone  )  throws CloneNotSupportedException [protected, virtual]
 

See also:
java.lang.Object#clone()

Implements org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.

Definition at line 132 of file VariablePoolConnectionManager.java.

References org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.VariablePoolConnectionManager().

00133   {
00134     return new VariablePoolConnectionManager(backendUrl, backendName, rLogin,
00135         rPassword, driverPath, driverClassName, minPoolSize, maxPoolSize,
00136         idleTimeout, waitTimeout);
00137   }

void org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.deleteConnection Connection  c  )  [virtual]
 

See also:
org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager.deleteConnection(Connection)

Implements org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.

Definition at line 446 of file VariablePoolConnectionManager.java.

References org.objectweb.cjdbc.common.log.Trace.debug(), org.objectweb.cjdbc.common.log.Trace.error(), org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.getConnectionFromDriver(), and org.objectweb.cjdbc.common.log.Trace.isDebugEnabled().

00447   {
00448     if (!initialized)
00449       return; // We probably have been disabled
00450 
00451     synchronized (freeConnections)
00452     {
00453       if (activeConnections.remove(c))
00454       {
00455         poolSize--;
00456         if (poolSize < minPoolSize)
00457         {
00458           Connection newConnection = getConnectionFromDriver();
00459           if (newConnection == null)
00460           {
00461             if (logger.isDebugEnabled())
00462               logger.error("Bad connection " + c
00463                   + " has been removed but cannot be replaced.");
00464           }
00465           else
00466           {
00467             freeConnections.push(newConnection);
00468             freeConnections.notify();
00469             if (logger.isDebugEnabled())
00470               logger.debug("Bad connection " + c
00471                   + " has been replaced by a new connection.");
00472           }
00473         }
00474         else if (logger.isDebugEnabled())
00475           logger.debug("Bad connection " + c + " has been removed.");
00476       }
00477       else
00478         logger.error("Failed to release connection " + c
00479             + " (not found in active pool)");
00480     }
00481   }

synchronized void org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.finalizeConnections  )  throws SQLException [virtual]
 

See also:
org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.finalizeConnections()

Reimplemented from org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager.

Definition at line 260 of file VariablePoolConnectionManager.java.

References org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.RemoveIdleConnectionsThread.isKilled.

00261   {
00262     if (removeIdleConnectionsThread != null)
00263     {
00264       synchronized (removeIdleConnectionsThread)
00265       {
00266         removeIdleConnectionsThread.isKilled = true;
00267         idleTimeout = 0;
00268         removeIdleConnectionsThread.notify();
00269       }
00270       try
00271       {
00272         removeIdleConnectionsThread.join();
00273       }
00274       catch (InterruptedException e)
00275       {
00276       }
00277     }
00278     super.finalizeConnections();
00279   }

Connection org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.getConnection  )  throws UnreachableBackendException [virtual]
 

Gets a connection from the pool.

If the current number of active connections is lower than the maximum pool size, a new connection is created. If the creation fails, this method waits for a connection to be freed.

If the maximum number of active connections is reached, this methods blocks until a connection is freed or the timeout expires.

Returns:
a connection from the pool or null if the timeout has expired.
Exceptions:
UnreachableBackendException if the backend must be disabled
See also:
org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.getConnection()

Implements org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.

Definition at line 296 of file VariablePoolConnectionManager.java.

References org.objectweb.cjdbc.common.log.Trace.error(), org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.getConnectionFromDriver(), org.objectweb.cjdbc.common.log.Trace.isErrorEnabled(), org.objectweb.cjdbc.common.log.Trace.isWarnEnabled(), and org.objectweb.cjdbc.common.log.Trace.warn().

00297   {
00298     if (!initialized)
00299     {
00300       logger
00301           .error("Requesting a connection from a non-initialized connection manager");
00302       return null;
00303     }
00304 
00305     long lTimeout = waitTimeout;
00306     synchronized (freeConnections)
00307     {
00308       if (freeConnections.isEmpty())
00309       {
00310         if ((maxPoolSize == 0) || (activeConnections.size() < maxPoolSize))
00311         {
00312           Connection c = getConnectionFromDriver();
00313           if (c == null)
00314           {
00315             if (activeConnections.size() == 0)
00316             { // No connection active and backend unreachable, the backend
00317               // is probably dead
00318               logger
00319                   .error("Backend " + backendName + " is no more accessible.");
00320               throw new UnreachableBackendException();
00321             }
00322             // If it fails, just wait for a connection to be freed
00323             if (logger.isWarnEnabled())
00324               logger.warn("Failed to create new connection on backend '"
00325                   + backendName + "', waiting for a connection to be freed.");
00326           }
00327           else
00328           {
00329             freeConnections.add(c);
00330             if (idleTimeout != 0)
00331             {
00332               releaseTimes.add(new Long(System.currentTimeMillis()));
00333             }
00334             poolSize++;
00335           }
00336         }
00337 
00338         /*
00339          * We have to do a while loop() because there is a potential race here.
00340          * When freeConnections is notified in releaseConnection, a new thread
00341          * can take the lock on freeConnections before we wake up/reacquire the
00342          * lock on freeConnections. Therefore, we could wake up and have no
00343          * connection to take! We ensure that everything is correct with a while
00344          * statement and recomputing the timeout between 2 wakeup.
00345          */
00346         while (freeConnections.isEmpty())
00347         {
00348           // Wait
00349           try
00350           {
00351             if (lTimeout > 0)
00352             {
00353               long start = System.currentTimeMillis();
00354               // Convert seconds to milliseconds for wait call
00355               freeConnections.wait(waitTimeout);
00356               long end = System.currentTimeMillis();
00357               lTimeout -= end - start;
00358               if (lTimeout <= 0)
00359               {
00360                 if (logger.isWarnEnabled())
00361                   logger.warn("Timeout expired for connection on backend '"
00362                       + backendName
00363                       + "', consider increasing pool size (current size is "
00364                       + poolSize + ") or timeout (current timeout is "
00365                       + (waitTimeout / 1000) + " seconds)");
00366                 return null;
00367               }
00368             }
00369             else
00370             {
00371               freeConnections.wait();
00372             }
00373           }
00374           catch (InterruptedException e)
00375           {
00376             logger
00377                 .error("Wait on freeConnections interrupted in VariablePoolConnectionManager");
00378             return null;
00379           }
00380         }
00381       }
00382 
00383       // Get the connection
00384       try
00385       {
00386         Connection c = (Connection) freeConnections.pop();
00387         if (idleTimeout != 0)
00388         {
00389           releaseTimes.pop();
00390         }
00391         activeConnections.add(c);
00392         return c;
00393       }
00394       catch (EmptyStackException e)
00395       {
00396         if (logger.isErrorEnabled())
00397           logger.error("Failed to get a connection on backend '" + backendName
00398               + "' but an idle connection was expected");
00399         return null;
00400       }
00401     }
00402   }

int org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.getIdleTimeout  ) 
 

Gets the idle timeout.

Returns:
a int value.

Definition at line 202 of file VariablePoolConnectionManager.java.

00203   {
00204     return idleTimeout;
00205   }

int org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.getMaxPoolSize  ) 
 

Gets the max pool size.

Returns:
a int value.

Definition at line 182 of file VariablePoolConnectionManager.java.

00183   {
00184     return maxPoolSize;
00185   }

int org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.getMinPoolSize  ) 
 

Gets the min pool size.

Returns:
a int value.

Definition at line 192 of file VariablePoolConnectionManager.java.

00193   {
00194     return minPoolSize;
00195   }

int org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.getWaitTimeout  ) 
 

Gets the wait timeout.

Returns:
a int value.

Definition at line 212 of file VariablePoolConnectionManager.java.

00213   {
00214     return waitTimeout;
00215   }

String org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.getXmlImpl  )  [virtual]
 

See also:
org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.getXmlImpl()

Implements org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.

Definition at line 486 of file VariablePoolConnectionManager.java.

00487   {
00488     StringBuffer info = new StringBuffer();
00489     info.append("<" + DatabasesXmlTags.ELT_VariablePoolConnectionManager + " "
00490         + DatabasesXmlTags.ATT_initPoolSize + "=\"" + initPoolSize + "\" "
00491         + DatabasesXmlTags.ATT_minPoolSize + "=\"" + minPoolSize + "\" "
00492         + DatabasesXmlTags.ATT_maxPoolSize + "=\"" + maxPoolSize + "\" "
00493         + DatabasesXmlTags.ATT_idleTimeout + "=\"" + idleTimeout / 1000 + "\" "
00494         + DatabasesXmlTags.ATT_waitTimeout + "=\"" + waitTimeout / 1000
00495         + "\"/>");
00496     return info.toString();
00497   }

synchronized void org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.initializeConnections  )  throws SQLException [virtual]
 

See also:
org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager.initializeConnections()

Reimplemented from org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager.

Definition at line 220 of file VariablePoolConnectionManager.java.

00221   {
00222     poolSize = maxPoolSize == 0 ? (initPoolSize > minPoolSize
00223         ? initPoolSize
00224         : minPoolSize) : maxPoolSize;
00225     super.initializeConnections(initPoolSize);
00226 
00227     if (idleTimeout != 0)
00228     {
00229       // Create the thread which manages the free connections
00230       removeIdleConnectionsThread = new RemoveIdleConnectionsThread(
00231           this.backendName);
00232 
00233       // Intialize release time for the initial connections if an idleTimeout
00234       // is set
00235       releaseTimes = new Stack();
00236       Iterator it = freeConnections.iterator();
00237       Long currentTime = new Long(System.currentTimeMillis());
00238       while (it.hasNext())
00239       {
00240         it.next();
00241         releaseTimes.push(currentTime);
00242       }
00243 
00244       // Start the thread
00245       removeIdleConnectionsThread.start();
00246 
00247       synchronized (removeIdleConnectionsThread)
00248       {
00249         if (releaseTimes.size() > 0)
00250         {
00251           removeIdleConnectionsThread.notify();
00252         }
00253       }
00254     }
00255   }

void org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.releaseConnection Connection  c  )  [virtual]
 

See also:
org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.releaseConnection(Connection)

Implements org.objectweb.cjdbc.controller.connection.AbstractConnectionManager.

Definition at line 407 of file VariablePoolConnectionManager.java.

References org.objectweb.cjdbc.common.log.Trace.error().

00408   {
00409     if (!initialized)
00410       return; // We probably have been disabled
00411 
00412     boolean notifyThread = false;
00413     synchronized (freeConnections)
00414     {
00415       if (activeConnections.remove(c))
00416       {
00417         if (idleTimeout != 0)
00418         {
00419           notifyThread = freeConnections.isEmpty()
00420               || (freeConnections.size() == minPoolSize);
00421           freeConnections.push(c);
00422           freeConnections.notify();
00423           releaseTimes.push(new Long(System.currentTimeMillis()));
00424         }
00425         else
00426         {
00427           freeConnections.push(c);
00428           freeConnections.notify();
00429         }
00430       }
00431       else
00432         logger.error("Failed to release connection " + c
00433             + " (not found in active pool)");
00434     }
00435 
00436     if (notifyThread)
00437       synchronized (removeIdleConnectionsThread)
00438       {
00439         removeIdleConnectionsThread.notify();
00440       }
00441   }


Member Data Documentation

final int org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.DEFAULT_IDLE_TIMEOUT = 0 [static]
 

Default idle timeout in milliseconds: default is 0 and means that once allocated, connections are never released.

Definition at line 65 of file VariablePoolConnectionManager.java.

final int org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.DEFAULT_MAX_POOL_SIZE = 0 [static]
 

Default maximum pool size: default is 0 and means no limit.

Definition at line 59 of file VariablePoolConnectionManager.java.

final int org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.DEFAULT_WAIT_TIMEOUT = 0 [static]
 

Default wait timeout in milliseconds: the default is 0 and means no timeout: waits until one connection is freed.

Definition at line 71 of file VariablePoolConnectionManager.java.


The documentation for this class was generated from the following file:
Generated on Mon Apr 11 22:03:42 2005 for C-JDBC by  doxygen 1.3.9.1