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

org.objectweb.cjdbc.driver.ConnectionClosingThread Class Reference

Collaboration diagram for org.objectweb.cjdbc.driver.ConnectionClosingThread:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ConnectionClosingThread (Driver driver)
void run ()

Detailed Description

The ConnectionClosingThread wakes up every 5 seconds when close() has been called on a connection and it frees the connection if it has not been reused.

Author:
Emmanuel Cecchet
Version:
1.0

Definition at line 39 of file ConnectionClosingThread.java.


Constructor & Destructor Documentation

org.objectweb.cjdbc.driver.ConnectionClosingThread.ConnectionClosingThread Driver  driver  ) 
 

Builds a new ConnectionClosingThread

Parameters:
driver The driver that created us

Definition at line 52 of file ConnectionClosingThread.java.

References org.objectweb.cjdbc.driver.Driver.connectionClosingThreadisAlive, and org.objectweb.cjdbc.driver.Driver.pendingConnectionClosing.

00053   {
00054     super("ConnectionClosingThread");
00055     this.driver = driver;
00056     this.pendingConnectionClosing = driver.pendingConnectionClosing;
00057     driver.connectionClosingThreadisAlive = true;
00058   }


Member Function Documentation

void org.objectweb.cjdbc.driver.ConnectionClosingThread.run  ) 
 

The connection closing thread wakes up every WAIT_TIME seconds when close() has been called on a connection and it frees the connection if it has not been reused.

Definition at line 65 of file ConnectionClosingThread.java.

References org.objectweb.cjdbc.driver.Driver.connectionClosingThreadisAlive.

00066   {
00067     try
00068     {
00069       Connection firstConnectionToClose = null;
00070       Connection lastConnectionToClose = null;
00071       int pendingConnectionSize;
00072       ArrayList closingList = new ArrayList();
00073       boolean killed = false;
00074 
00075       while (!killed)
00076       {
00077         synchronized (pendingConnectionClosing)
00078         {
00079           pendingConnectionSize = pendingConnectionClosing.size();
00080           if (pendingConnectionSize == 0)
00081             break;
00082 
00083           try
00084           {
00085             // Look at the connections in the queue before sleeping
00086             firstConnectionToClose = (Connection) pendingConnectionClosing
00087                 .get(0);
00088             lastConnectionToClose = (Connection) pendingConnectionClosing
00089                 .get(pendingConnectionSize - 1);
00090 
00091             // Sleep
00092             pendingConnectionClosing.wait(WAIT_TIME);
00093           }
00094           catch (InterruptedException ignore)
00095           {
00096           }
00097 
00098           pendingConnectionSize = pendingConnectionClosing.size();
00099           // Exit, no more connections
00100           if (pendingConnectionSize == 0)
00101             break;
00102 
00103           // Compare the queue now with its state when we got to sleep
00104           if (firstConnectionToClose == pendingConnectionClosing.get(0))
00105           { // Ok, the connection has not been reused, let's close it
00106             if (lastConnectionToClose == (Connection) pendingConnectionClosing
00107                 .get(pendingConnectionSize - 1))
00108             { // No connection has been reused, remove them all
00109               closingList.addAll(pendingConnectionClosing);
00110               pendingConnectionClosing.clear();
00111               killed = true; // Let's die, there are no more connections
00112             }
00113             else
00114               // Close only the first connection
00115               closingList.add(pendingConnectionClosing.remove(0));
00116           }
00117         }
00118 
00119         // Effectively close the connections outside the synchronized block
00120         while (!closingList.isEmpty())
00121           closeConnection((Connection) closingList.remove(0));
00122       }
00123     }
00124     catch (RuntimeException e)
00125     {
00126       e.printStackTrace();
00127     }
00128     finally
00129     {
00130       synchronized (pendingConnectionClosing)
00131       {
00132         driver.connectionClosingThreadisAlive = false;
00133       }
00134     }
00135   }


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