src/org/objectweb/cjdbc/driver/ConnectionClosingThread.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.driver; 00026 00027 import java.util.ArrayList; 00028 00029 import org.objectweb.cjdbc.driver.protocol.Commands; 00030 00039 public class ConnectionClosingThread extends Thread 00040 { 00041 /* wait time before closing a connection in ms */ 00042 private static final int WAIT_TIME = 5000; 00043 00044 private Driver driver; 00045 private ArrayList pendingConnectionClosing; 00046 00052 public ConnectionClosingThread(Driver driver) 00053 { 00054 super("ConnectionClosingThread"); 00055 this.driver = driver; 00056 this.pendingConnectionClosing = driver.pendingConnectionClosing; 00057 driver.connectionClosingThreadisAlive = true; 00058 } 00059 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 // 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 } 00136 00142 private void closeConnection(Connection c) 00143 { 00144 try 00145 { 00146 // Unlink those objects so that this connection can be garbage collected 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 { // Wait for the controller to receive the connection and close the 00154 // stream. If we do not wait for the controller ack, the connection is 00155 // closed on the controller before the closing is handled which 00156 // results in an ugly warning message on the controller side. We are 00157 // not in a hurry when closing the connection so let do the things 00158 // nicely! 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 }

CJDBCversion1.0.4に対してTue Oct 12 15:16:05 2004に生成されました。 doxygen 1.3.8