1 65 66 67 package org.hsqldb; 68 69 import java.lang.reflect.Constructor ; 70 71 import org.hsqldb.lib.FileAccess; 72 import org.hsqldb.lib.FileUtil; 73 import org.hsqldb.lib.HashMap; 74 import org.hsqldb.persist.HsqlDatabaseProperties; 75 import org.hsqldb.persist.HsqlProperties; 76 import org.hsqldb.persist.Logger; 77 78 107 119 public class Database { 120 121 int databaseID; 122 String sType; 123 String sName; 124 125 private HsqlProperties urlProperties; 127 private String sPath; 128 DatabaseInformation dbInfo; 129 ClassLoader classLoader; 130 131 132 private int dbState; 133 public Logger logger; 134 135 136 boolean databaseReadOnly; 137 138 142 private boolean filesReadOnly; 143 144 145 private boolean filesInJar; 146 public boolean sqlEnforceStrictSize; 147 public int firstIdentity; 148 private boolean bIgnoreCase; 149 private boolean bReferentialIntegrity; 150 private HsqlDatabaseProperties databaseProperties; 151 private boolean shutdownOnNoConnection; 152 153 private HashMap hAlias; 155 public UserManager userManager; 156 public GranteeManager granteeManager; 157 public HsqlNameManager nameManager; 158 159 public SessionManager sessionManager; 161 public TransactionManager txManager; 162 CompiledStatementManager compiledStatementManager; 163 164 public SchemaManager schemaManager; 166 public Collation collation; 167 168 public static final int DATABASE_ONLINE = 1; 170 public static final int DATABASE_OPENING = 4; 171 public static final int DATABASE_CLOSING = 8; 172 public static final int DATABASE_SHUTDOWN = 16; 173 public static final int CLOSEMODE_IMMEDIATELY = -1; 174 public static final int CLOSEMODE_NORMAL = 0; 175 public static final int CLOSEMODE_COMPACT = 1; 176 public static final int CLOSEMODE_SCRIPT = 2; 177 178 189 Database(String type, String path, String name, 190 HsqlProperties props) throws HsqlException { 191 192 urlProperties = props; 193 194 setState(Database.DATABASE_SHUTDOWN); 195 196 sName = name; 197 sType = type; 198 sPath = path; 199 200 if (sType == DatabaseURL.S_RES) { 201 filesInJar = true; 202 filesReadOnly = true; 203 } 204 205 try { 207 classLoader = getClass().getClassLoader(); 208 } catch (Exception e) { 209 210 classLoader = null; 212 } 213 214 String fileaccess_class_name = 216 (String ) urlProperties.getProperty("fileaccess_class_name"); 217 218 if (fileaccess_class_name != null) { 219 String storagekey = urlProperties.getProperty("storage_key"); 220 221 try { 222 Class zclass = Class.forName(fileaccess_class_name); 223 Constructor constructor = zclass.getConstructor(new Class []{ 224 Object .class }); 225 226 fileaccess = 227 (FileAccess) constructor.newInstance(new Object []{ 228 storagekey }); 229 isStoredFileAccess = true; 230 } catch (java.lang.ClassNotFoundException e) { 231 System.out.println("ClassNotFoundException"); 232 } catch (java.lang.InstantiationException e) { 233 System.out.println("InstantiationException"); 234 } catch (java.lang.IllegalAccessException e) { 235 System.out.println("IllegalAccessException"); 236 } catch (Exception e) { 237 System.out.println("Exception"); 238 } 239 } else { 240 fileaccess = FileUtil.getDefaultInstance(); 241 } 242 243 shutdownOnNoConnection = urlProperties.getProperty("shutdown", 244 "false").equals("true"); 245 logger = new Logger(); 246 compiledStatementManager = new CompiledStatementManager(this); 247 } 248 249 252 synchronized void open() throws HsqlException { 253 254 if (!isShutdown()) { 255 return; 256 } 257 258 reopen(); 259 } 260 261 266 void reopen() throws HsqlException { 267 268 boolean isNew; 269 270 setState(DATABASE_OPENING); 271 272 try { 273 databaseProperties = new HsqlDatabaseProperties(this); 274 isNew = !DatabaseURL.isFileBasedDatabaseType(sType) 275 ||!databaseProperties.checkFileExists(); 276 277 if (isNew && urlProperties.isPropertyTrue("ifexists")) { 278 throw Trace.error(Trace.DATABASE_NOT_EXISTS, sName); 279 } 280 281 databaseProperties.load(); 282 databaseProperties.setURLProperties(urlProperties); 283 compiledStatementManager.reset(); 284 285 nameManager = new HsqlNameManager(); 286 granteeManager = new GranteeManager(this); 287 userManager = new UserManager(this); 288 hAlias = Library.getAliasMap(); 289 schemaManager = new SchemaManager(this); 290 bReferentialIntegrity = true; 291 sessionManager = new SessionManager(this); 292 txManager = new TransactionManager(this); 293 collation = new Collation(); 294 dbInfo = DatabaseInformation.newDatabaseInformation(this); 295 296 databaseProperties.setDatabaseVariables(); 297 298 if (DatabaseURL.isFileBasedDatabaseType(sType)) { 299 logger.openLog(this); 300 } 301 302 if (isNew) { 303 sessionManager.getSysSession().sqlExecuteDirectNoPreChecks( 304 "CREATE USER SA PASSWORD \"\" ADMIN"); 305 logger.synchLogForce(); 306 } 307 308 dbInfo.setWithContent(true); 309 } catch (Throwable e) { 310 logger.closeLog(Database.CLOSEMODE_IMMEDIATELY); 311 logger.releaseLock(); 312 setState(DATABASE_SHUTDOWN); 313 clearStructures(); 314 DatabaseManager.removeDatabase(this); 315 316 if (!(e instanceof HsqlException)) { 317 e = Trace.error(Trace.GENERAL_ERROR, e.toString()); 318 } 319 320 throw (HsqlException) e; 321 } 322 323 setState(DATABASE_ONLINE); 324 } 325 326 329 void clearStructures() { 330 331 if (schemaManager != null) { 332 schemaManager.clearStructures(); 333 } 334 335 granteeManager = null; 336 userManager = null; 337 hAlias = null; 338 nameManager = null; 339 schemaManager = null; 340 sessionManager = null; 341 dbInfo = null; 342 } 343 344 347 public String getType() { 348 return sType; 349 } 350 351 354 public String getPath() { 355 return sPath; 356 } 357 358 361 public HsqlDatabaseProperties getProperties() { 362 return databaseProperties; 363 } 364 365 368 public SessionManager getSessionManager() { 369 return sessionManager; 370 } 371 372 375 synchronized boolean isShutdown() { 376 return dbState == DATABASE_SHUTDOWN; 377 } 378 379 388 synchronized Session connect(String username, 389 String password) throws HsqlException { 390 391 User user = userManager.getUser(username, password); 392 Session session = sessionManager.newSession(this, user, 393 databaseReadOnly, false); 394 395 logger.logConnectUser(session); 396 397 return session; 398 } 399 400 406 public void setReadOnly() { 407 databaseReadOnly = true; 408 filesReadOnly = true; 409 } 410 411 417 public void setFilesReadOnly() { 418 filesReadOnly = true; 419 } 420 421 424 public boolean isFilesReadOnly() { 425 return filesReadOnly; 426 } 427 428 431 public boolean isFilesInJar() { 432 return filesInJar; 433 } 434 435 438 UserManager getUserManager() { 439 return userManager; 440 } 441 442 445 GranteeManager getGranteeManager() { 446 return granteeManager; 447 } 448 449 452 public void setReferentialIntegrity(boolean ref) { 453 bReferentialIntegrity = ref; 454 } 455 456 459 boolean isReferentialIntegrity() { 460 return bReferentialIntegrity; 461 } 462 463 467 HashMap getAliasMap() { 468 return hAlias; 469 } 470 471 476 String getJavaName(String s) { 477 478 String alias = (String ) hAlias.get(s); 479 480 return (alias == null) ? s 481 : alias; 482 } 483 484 488 void setIgnoreCase(boolean b) { 489 bIgnoreCase = b; 490 } 491 492 496 boolean isIgnoreCase() { 497 return bIgnoreCase; 498 } 499 500 503 int getDefaultTableType() { 504 505 String dttName = getProperties().getProperty( 506 HsqlDatabaseProperties.hsqldb_default_table_type); 507 508 return Token.T_CACHED.equalsIgnoreCase(dttName) ? Table.CACHED_TABLE 509 : Table.MEMORY_TABLE; 510 } 511 512 516 protected void finalize() { 517 518 if (getState() != DATABASE_ONLINE) { 519 return; 520 } 521 522 try { 523 close(CLOSEMODE_IMMEDIATELY); 524 } catch (HsqlException e) { } 526 } 527 528 void closeIfLast() { 529 530 if (shutdownOnNoConnection && sessionManager.isEmpty() 531 && dbState == this.DATABASE_ONLINE) { 532 try { 533 close(CLOSEMODE_NORMAL); 534 } catch (HsqlException e) {} 535 } 536 } 537 538 555 void close(int closemode) throws HsqlException { 556 557 HsqlException he = null; 558 559 setState(DATABASE_CLOSING); 560 sessionManager.closeAllSessions(); 561 sessionManager.clearAll(); 562 563 if (filesReadOnly) { 564 closemode = CLOSEMODE_IMMEDIATELY; 565 } 566 567 logger.closeLog(closemode); 570 571 try { 572 if (closemode == CLOSEMODE_COMPACT) { 573 clearStructures(); 574 reopen(); 575 setState(DATABASE_CLOSING); 576 logger.closeLog(CLOSEMODE_NORMAL); 577 } 578 } catch (Throwable t) { 579 if (t instanceof HsqlException) { 580 he = (HsqlException) t; 581 } else { 582 he = Trace.error(Trace.GENERAL_ERROR, t.toString()); 583 } 584 } 585 586 classLoader = null; 587 588 logger.releaseLock(); 589 setState(DATABASE_SHUTDOWN); 590 clearStructures(); 591 592 DatabaseManager.removeDatabase(this); 596 597 if (he != null) { 598 throw he; 599 } 600 } 601 602 618 public void setMetaDirty(boolean resetPrepared) { 619 620 if (dbInfo != null) { 621 dbInfo.setDirty(); 622 } 623 624 if (resetPrepared) { 625 compiledStatementManager.resetStatements(); 626 } 627 } 628 629 private synchronized void setState(int state) { 630 dbState = state; 631 } 632 633 synchronized int getState() { 634 return dbState; 635 } 636 637 String getStateString() { 638 639 int state = getState(); 640 641 switch (state) { 642 643 case DATABASE_CLOSING : 644 return "DATABASE_CLOSING"; 645 646 case DATABASE_ONLINE : 647 return "DATABASE_ONLINE"; 648 649 case DATABASE_OPENING : 650 return "DATABASE_OPENING"; 651 652 case DATABASE_SHUTDOWN : 653 return "DATABASE_SHUTDOWN"; 654 655 default : 656 return "UNKNOWN"; 657 } 658 } 659 660 663 668 public String getURI() { 669 return sName; 670 } 671 672 public HsqlProperties getURLProperties() { 674 return urlProperties; 675 } 676 677 private FileAccess fileaccess; 678 private boolean isStoredFileAccess; 679 680 public synchronized FileAccess getFileAccess() { 681 return fileaccess; 682 } 683 684 public synchronized boolean isStoredFileAccess() { 685 return isStoredFileAccess; 686 } 687 } 688 | Popular Tags |