KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > driver > ConnectionClosingThread


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 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.driver;
26
27 import java.util.ArrayList JavaDoc;
28
29 import org.objectweb.cjdbc.driver.protocol.Commands;
30
31 /**
32  * The <code>ConnectionClosingThread</code> wakes up every 5 seconds when
33  * close() has been called on a connection and it frees the connection if it has
34  * not been reused.
35  *
36  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
37  * @version 1.0
38  */

39 public class ConnectionClosingThread extends Thread JavaDoc
40 {
41   /* wait time before closing a connection in ms */
42   private static final int WAIT_TIME = 5000;
43
44   private Driver driver;
45   private ArrayList JavaDoc pendingConnectionClosing;
46
47   /**
48    * Builds a new ConnectionClosingThread
49    *
50    * @param driver The driver that created us
51    */

52   public ConnectionClosingThread(Driver driver)
53   {
54     super("ConnectionClosingThread");
55     this.driver = driver;
56     this.pendingConnectionClosing = driver.pendingConnectionClosing;
57     driver.connectionClosingThreadisAlive = true;
58   }
59
60   /**
61    * The connection closing thread wakes up every WAIT_TIME seconds when close()
62    * has been called on a connection and it frees the connection if it has not
63    * been reused.
64    */

65   public void run()
66   {
67     try
68     {
69       Connection firstConnectionToClose = null;
70       Connection lastConnectionToClose = null;
71       int pendingConnectionSize;
72       ArrayList JavaDoc closingList = new ArrayList JavaDoc();
73       boolean killed = false;
74
75       while (!killed)
76       {
77         synchronized (pendingConnectionClosing)
78         {
79           pendingConnectionSize = pendingConnectionClosing.size();
80           if (pendingConnectionSize == 0)
81             break;
82
83           try
84           {
85             // Look at the connections in the queue before sleeping
86
firstConnectionToClose = (Connection) pendingConnectionClosing
87                 .get(0);
88             lastConnectionToClose = (Connection) pendingConnectionClosing
89                 .get(pendingConnectionSize - 1);
90
91             // Sleep
92
pendingConnectionClosing.wait(WAIT_TIME);
93           }
94           catch (InterruptedException JavaDoc ignore)
95           {
96           }
97
98           pendingConnectionSize = pendingConnectionClosing.size();
99           // Exit, no more connections
100
if (pendingConnectionSize == 0)
101             break;
102
103           // Compare the queue now with its state when we got to sleep
104
if (firstConnectionToClose == pendingConnectionClosing.get(0))
105           { // Ok, the connection has not been reused, let's close it
106
if (lastConnectionToClose == (Connection) pendingConnectionClosing
107                 .get(pendingConnectionSize - 1))
108             { // No connection has been reused, remove them all
109
closingList.addAll(pendingConnectionClosing);
110               pendingConnectionClosing.clear();
111               killed = true; // Let's die, there are no more connections
112
}
113             else
114               // Close only the first connection
115
closingList.add(pendingConnectionClosing.remove(0));
116           }
117         }
118
119         // Effectively close the connections outside the synchronized block
120
while (!closingList.isEmpty())
121           closeConnection((Connection) closingList.remove(0));
122       }
123     }
124     catch (RuntimeException JavaDoc e)
125     {
126       e.printStackTrace();
127     }
128     finally
129     {
130       synchronized (pendingConnectionClosing)
131       {
132         driver.connectionClosingThreadisAlive = false;
133       }
134     }
135   }
136
137   /**
138    * Closes a connection. This cleanup should belong to the underlying class.
139    *
140    * @param c the connection to close
141    */

142   private void closeConnection(Connection c)
143   {
144     try
145     {
146       // Unlink those objects so that this connection can be garbage collected
147
c.driver = null;
148       if (c.socketOutput != null)
149       {
150         c.socketOutput.writeInt(Commands.Close);
151         c.socketOutput.flush();
152         if (c.socketInput != null)
153         { // Wait for the controller to receive the connection and close the
154
// stream. If we do not wait for the controller ack, the connection is
155
// closed on the controller before the closing is handled which
156
// results in an ugly warning message on the controller side. We are
157
// not in a hurry when closing the connection so let do the things
158
// nicely!
159
c.socketInput.readBoolean();
160           c.socketInput.close();
161         }
162         c.socketOutput.close();
163       }
164
165       if (c.socket != null)
166         c.socket.close();
167     }
168     catch (Exception JavaDoc ignore)
169     {
170     }
171   }
172
173 }
Popular Tags