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.driver;
00026
00027 import java.util.ArrayList;
00028
00029 import org.objectweb.cjdbc.driver.protocol.Commands;
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 public class ConnectionClosingThread extends Thread
00040 {
00041
00042 private static final int WAIT_TIME = 5000;
00043
00044 private Driver driver;
00045 private ArrayList pendingConnectionClosing;
00046
00047
00048
00049
00050
00051
00052 public ConnectionClosingThread(Driver driver)
00053 {
00054 super("ConnectionClosingThread");
00055 this.driver = driver;
00056 this.pendingConnectionClosing = driver.pendingConnectionClosing;
00057 driver.connectionClosingThreadisAlive = true;
00058 }
00059
00060
00061
00062
00063
00064
00065 public void run()
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
00086 firstConnectionToClose = (Connection) pendingConnectionClosing
00087 .get(0);
00088 lastConnectionToClose = (Connection) pendingConnectionClosing
00089 .get(pendingConnectionSize - 1);
00090
00091
00092 pendingConnectionClosing.wait(WAIT_TIME);
00093 }
00094 catch (InterruptedException ignore)
00095 {
00096 }
00097
00098 pendingConnectionSize = pendingConnectionClosing.size();
00099
00100 if (pendingConnectionSize == 0)
00101 break;
00102
00103
00104 if (firstConnectionToClose == pendingConnectionClosing.get(0))
00105 {
00106 if (lastConnectionToClose == (Connection) pendingConnectionClosing
00107 .get(pendingConnectionSize - 1))
00108 {
00109 closingList.addAll(pendingConnectionClosing);
00110 pendingConnectionClosing.clear();
00111 killed = true;
00112 }
00113 else
00114
00115 closingList.add(pendingConnectionClosing.remove(0));
00116 }
00117 }
00118
00119
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 }
00136
00137
00138
00139
00140
00141
00142 private void closeConnection(Connection c)
00143 {
00144 try
00145 {
00146
00147 c.driver = null;
00148 if (c.socketOutput != null)
00149 {
00150 c.socketOutput.writeInt(Commands.Close);
00151 c.socketOutput.flush();
00152 if (c.socketInput != null)
00153 {
00154
00155
00156
00157
00158
00159 c.socketInput.readObject();
00160 c.socketInput.close();
00161 }
00162 c.socketOutput.close();
00163 }
00164
00165 if (c.socket != null)
00166 c.socket.close();
00167 }
00168 catch (Exception ignore)
00169 {
00170 }
00171 }
00172
00173 }