1 24 25 package com.mckoi.database.jdbcserver; 26 27 import com.mckoi.database.User; 28 import com.mckoi.database.Database; 29 import com.mckoi.database.DatabaseSystem; 30 import com.mckoi.debug.*; 31 import java.io.IOException ; 32 import java.util.ArrayList ; 33 import java.util.LinkedList ; 34 import java.util.ResourceBundle ; 35 36 43 44 final class MultiThreadedConnectionPoolServer implements ConnectionPoolServer { 45 46 50 private static final boolean DISPLAY_STATS = false; 51 52 55 private Database database; 56 57 61 private ArrayList client_threads; 62 63 64 67 MultiThreadedConnectionPoolServer(Database database) { 68 this.database = database; 69 client_threads = new ArrayList (); 70 } 71 72 75 public final DebugLogger Debug() { 76 return database.Debug(); 77 } 78 79 85 public void addConnection(ServerConnection connection) { 86 ClientThread client_thread = new ClientThread(connection); 87 synchronized(client_threads) { 88 client_threads.add(client_thread); 89 } 90 client_thread.start(); 91 } 92 93 96 public void close() { 97 synchronized (client_threads) { 98 int size = client_threads.size(); 99 for (int i = 0; i < size; ++i) { 100 ((ClientThread) client_threads.get(i)).close(); 101 } 102 } 103 } 104 105 107 111 private class ClientThread extends Thread { 112 113 116 private ServerConnection server_connection; 117 118 121 private boolean client_closed; 122 123 126 private boolean processing_command; 127 128 131 public ClientThread(ServerConnection connection) { 132 super(); 133 setName("Mckoi - Client Connection"); 135 136 this.server_connection = connection; 137 client_closed = false; 138 processing_command = false; 139 } 140 141 147 private void checkCurrentConnection() throws InterruptedException { 148 try { 149 150 synchronized(this) { 152 while (processing_command) { 153 if (client_closed) { 154 return; 155 } 156 wait(120000); 158 } 159 } 160 161 server_connection.blockForRequest(); 163 164 processing_command = true; 165 166 database.execute(null, null, new Runnable () { 167 public void run() { 168 169 try { 170 server_connection.processRequest(); 172 } 173 catch (IOException e) { 174 Debug().writeException(Lvl.INFORMATION, e); 175 } 176 finally { 177 processing_command = false; 179 synchronized (ClientThread.this) { 180 ClientThread.this.notifyAll(); 181 } 182 } 183 184 } 185 }); 186 187 } 188 catch (IOException e) { 189 close(); 192 193 Debug().write(Lvl.INFORMATION, this, 195 "IOException generated while checking connections, " + 196 "removing provider."); 197 Debug().writeException(Lvl.INFORMATION, e); 198 } 199 200 } 201 202 205 public synchronized void close() { 206 client_closed = true; 207 try { 208 server_connection.close(); 209 } 210 catch (Throwable e) { 211 } 213 notifyAll(); 214 } 215 216 219 public void run() { 220 while (true) { 221 try { 222 223 boolean closed = false; 224 synchronized (this) { 225 closed = client_closed; 226 } 227 if (closed == true) { 229 synchronized(client_threads) { 231 client_threads.remove(this); 232 } 233 return; 234 } 235 236 checkCurrentConnection(); 237 238 } 239 catch (Throwable e) { 240 Debug().write(Lvl.ERROR, this, "Connection Pool Farmer Error"); 241 Debug().writeException(e); 242 } 243 } 244 } 245 246 }; 247 248 } 249 | Popular Tags |