1 24 25 package com.mckoi.database.control; 26 27 import com.mckoi.database.Database; 28 import com.mckoi.database.DatabaseException; 29 import com.mckoi.database.jdbc.MConnection; 30 import com.mckoi.database.jdbc.DatabaseInterface; 31 import com.mckoi.database.jdbcserver.JDBCDatabaseInterface; 32 import com.mckoi.debug.*; 33 34 import java.sql.Connection ; 35 import java.sql.SQLException ; 36 37 46 47 public final class DBSystem { 48 49 52 private DBController controller; 53 54 58 private DBConfig config; 59 60 64 private Database database; 65 66 69 private int internal_counter; 70 71 72 73 76 DBSystem(DBController controller, DBConfig config, Database database) { 77 this.controller = controller; 78 this.config = config; 79 this.database = database; 80 this.internal_counter = 0; 81 82 database.registerShutDownDelegate(new Runnable () { 84 public void run() { 85 internalDispose(); 86 } 87 }); 88 89 database.setIsExecutingCommands(true); 91 92 } 93 94 97 public DBConfig getConfig() { 98 return config; 99 } 100 101 103 116 public Database getDatabase() { 117 return database; 118 } 119 120 143 public Connection getConnection(String schema, 144 String username, String password) throws SQLException { 145 146 StringBuffer buf = new StringBuffer (); 148 buf.append("Internal/"); 149 buf.append(hashCode()); 150 buf.append('/'); 151 synchronized (this) { 152 buf.append(internal_counter); 153 ++internal_counter; 154 } 155 String host_string = new String (buf); 156 157 DatabaseInterface db_interface = 159 new JDBCDatabaseInterface(getDatabase(), host_string); 160 MConnection connection = new MConnection("", db_interface, 8, 4092000); 163 connection.login(schema, username, password); 165 166 return connection; 168 } 169 170 192 public Connection getConnection(String username, String password) 193 throws SQLException { 194 return getConnection(null, username, password); 195 } 196 197 199 211 public final void setDeleteOnClose(boolean status) { 212 database.setDeleteOnShutdown(status); 213 } 214 215 229 public void close() { 230 if (database != null) { 231 database.startShutDownThread(); 232 database.waitUntilShutdown(); 233 } 234 } 235 236 238 243 private void internalDispose() { 244 if (database != null && database.isInitialized()) { 245 246 database.setIsExecutingCommands(false); 248 249 try { 250 database.shutdown(); 251 } 252 catch (DatabaseException e) { 253 database.Debug().write(Lvl.ERROR, this, 254 "Unable to shutdown database because of exception"); 255 database.Debug().writeException(Lvl.ERROR, e); 256 } 257 } 258 controller = null; 259 config = null; 260 database = null; 261 } 262 263 } 264 | Popular Tags |