1 24 25 package org.objectweb.cjdbc.driver; 26 27 import java.util.ArrayList ; 28 29 import org.objectweb.cjdbc.driver.protocol.Commands; 30 31 39 public class ConnectionClosingThread extends Thread 40 { 41 42 private static final int WAIT_TIME = 5000; 43 44 private Driver driver; 45 private ArrayList pendingConnectionClosing; 46 47 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 65 public void run() 66 { 67 try 68 { 69 Connection firstConnectionToClose = null; 70 Connection lastConnectionToClose = null; 71 int pendingConnectionSize; 72 ArrayList closingList = new ArrayList (); 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 firstConnectionToClose = (Connection) pendingConnectionClosing 87 .get(0); 88 lastConnectionToClose = (Connection) pendingConnectionClosing 89 .get(pendingConnectionSize - 1); 90 91 pendingConnectionClosing.wait(WAIT_TIME); 93 } 94 catch (InterruptedException ignore) 95 { 96 } 97 98 pendingConnectionSize = pendingConnectionClosing.size(); 99 if (pendingConnectionSize == 0) 101 break; 102 103 if (firstConnectionToClose == pendingConnectionClosing.get(0)) 105 { if (lastConnectionToClose == (Connection) pendingConnectionClosing 107 .get(pendingConnectionSize - 1)) 108 { closingList.addAll(pendingConnectionClosing); 110 pendingConnectionClosing.clear(); 111 killed = true; } 113 else 114 closingList.add(pendingConnectionClosing.remove(0)); 116 } 117 } 118 119 while (!closingList.isEmpty()) 121 closeConnection((Connection) closingList.remove(0)); 122 } 123 } 124 catch (RuntimeException e) 125 { 126 e.printStackTrace(); 127 } 128 finally 129 { 130 synchronized (pendingConnectionClosing) 131 { 132 driver.connectionClosingThreadisAlive = false; 133 } 134 } 135 } 136 137 142 private void closeConnection(Connection c) 143 { 144 try 145 { 146 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 { 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 ignore) 169 { 170 } 171 } 172 173 } | Popular Tags |