1 24 25 package com.mckoi.database; 26 27 import com.mckoi.util.Stats; 28 import com.mckoi.debug.*; 29 import com.mckoi.database.control.DBConfig; 30 import java.util.List ; 32 import java.util.ArrayList ; 33 import java.util.Properties ; 36 37 44 45 public final class DatabaseSystem extends TransactionSystem { 46 47 50 private StatementCache statement_cache = null; 51 52 56 private boolean query_logging; 57 58 62 private WorkerPool worker_pool; 63 64 68 private ArrayList database_list; 69 70 73 private boolean shutdown = false; 74 75 79 private UserManager user_manager; 80 81 84 private ShutdownThread shutdown_thread; 85 86 89 public DatabaseSystem() { 90 super(); 91 } 92 93 97 public void init(DBConfig config) { 98 super.init(config); 99 100 database_list = new ArrayList (); 101 102 user_manager = new UserManager(); 104 105 if (config != null) { 106 107 boolean status; 108 109 status = getConfigBoolean("statement_cache", true); 111 if (status) { 112 statement_cache = new StatementCache(this, 127, 140, 20); 113 } 114 Debug().write(Lvl.MESSAGE, DatabaseSystem.class, 115 "statement_cache = " + status); 116 117 int max_worker_threads = getConfigInt("maximum_worker_threads", 4); 119 if (max_worker_threads <= 0) { 120 max_worker_threads = 1; 121 } 122 Debug().write(Lvl.MESSAGE, DatabaseSystem.class, 123 "Max worker threads set to: " + max_worker_threads); 124 worker_pool = new WorkerPool(this, max_worker_threads); 125 126 query_logging = getConfigBoolean("query_logging", false); 128 129 } 130 else { 131 throw new Error ("Config bundle already set."); 132 } 133 134 shutdown = false; 135 136 } 137 138 139 140 141 143 147 public boolean logQueries() { 148 return query_logging; 149 } 150 151 152 153 154 156 160 public void dispose() { 161 super.dispose(); 162 worker_pool = null; 163 database_list = null; 164 user_manager = null; 165 } 166 167 169 176 public StatementCache getStatementCache() { 177 return statement_cache; 178 } 179 180 181 182 183 185 192 public Transaction.CheckExpression prepareTransactionCheckConstraint( 193 DataTableDef table_def, Transaction.CheckExpression check) { 194 195 return super.prepareTransactionCheckConstraint(table_def, check); 196 197 } 198 199 200 202 208 UserManager getUserManager() { 209 return user_manager; 210 } 211 212 214 223 void waitUntilAllWorkersQuiet() { 224 worker_pool.waitUntilAllWorkersQuiet(); 225 } 226 227 233 void setIsExecutingCommands(boolean status) { 234 worker_pool.setIsExecutingCommands(status); 235 } 236 237 245 void execute(User user, DatabaseConnection database, 246 Runnable runner) { 247 worker_pool.execute(user, database, runner); 248 } 249 250 252 private final ArrayList shut_down_delegates = new ArrayList (); 253 254 260 void registerShutDownDelegate(Runnable delegate) { 261 shut_down_delegates.add(delegate); 262 } 263 264 267 private class ShutdownThread extends Thread { 268 269 private boolean finished = false; 270 271 synchronized void waitTillFinished() { 272 while (finished == false) { 273 try { 274 wait(); 275 } 276 catch (InterruptedException e) {} 277 } 278 } 279 280 public void run() { 281 synchronized (this) { 282 if (finished) { 283 return; 284 } 285 } 286 287 try { 290 Thread.sleep(1500); 291 } 292 catch (InterruptedException e) {} 293 setIsExecutingCommands(false); 295 waitUntilAllWorkersQuiet(); 297 298 worker_pool.shutdown(); 300 301 int sz = shut_down_delegates.size(); 302 if (sz == 0) { 303 Debug().write(Lvl.WARNING, this, "No shut down delegates registered!"); 304 } 305 else { 306 for (int i = 0; i < sz; ++i) { 307 Runnable shut_down_delegate = (Runnable ) shut_down_delegates.get(i); 308 shut_down_delegate.run(); 310 } 311 shut_down_delegates.clear(); 312 } 313 314 synchronized (this) { 315 dispose(); 317 318 finished = true; 319 notifyAll(); 320 } 321 } 322 }; 323 324 330 void startShutDownThread() { 331 if (!shutdown) { 332 shutdown = true; 333 shutdown_thread = new ShutdownThread(); 334 shutdown_thread.start(); 335 } 336 } 337 338 341 boolean hasShutDown() { 342 return shutdown; 343 } 344 345 349 void waitUntilShutdown() { 350 shutdown_thread.waitTillFinished(); 351 } 352 353 } 354 | Popular Tags |