/**
   * Opens this database. The database should be opened after construction. or reopened by the
   * close(int closemode) method during a "shutdown compact". Closes the log if there is an error.
   */
  void reopen() {

    boolean isNew = false;

    setState(DATABASE_OPENING);

    try {
      nameManager = new HsqlNameManager(this);
      granteeManager = new GranteeManager(this);
      userManager = new UserManager(this);
      schemaManager = new SchemaManager(this);
      persistentStoreCollection = new PersistentStoreCollectionDatabase(this);
      isReferentialIntegrity = true;
      sessionManager = new SessionManager(this);
      collation = collation.newDatabaseInstance();
      dbInfo = DatabaseInformation.newDatabaseInformation(this);
      txManager = new TransactionManager2PL(this);

      lobManager.createSchema();
      sessionManager.getSysLobSession().setSchema(SqlInvariants.LOBS_SCHEMA);
      schemaManager.setSchemaChangeTimestamp();
      schemaManager.createSystemTables();

      // completed metadata
      logger.open();

      isNew = logger.isNewDatabase;

      if (isNew) {
        String username = urlProperties.getProperty("user", "SA");
        String password = urlProperties.getProperty("password", "");

        userManager.createFirstUser(username, password);
        schemaManager.createPublicSchema();
        logger.checkpoint(false);
      }

      lobManager.open();
      dbInfo.setWithContent(true);

      checkpointRunner = new CheckpointRunner();
      timeoutRunner = new TimeoutRunner();
    } catch (Throwable e) {
      logger.close(Database.CLOSEMODE_IMMEDIATELY);
      logger.releaseLock();
      setState(DATABASE_SHUTDOWN);
      clearStructures();
      DatabaseManager.removeDatabase(this);

      if (!(e instanceof HsqlException)) {
        e = Error.error(ErrorCode.GENERAL_ERROR, e);
      }

      logger.logSevereEvent("could not reopen database", e);

      throw (HsqlException) e;
    }

    setState(DATABASE_ONLINE);
  }
  /**
   * Constructs a new Database object.
   *
   * @param type is the type of the database: "mem", "file", "res"
   * @param path is the fiven path to the database files
   * @param name is the combination of type and canonical path
   * @param props property overrides placed on the connect URL
   * @exception HsqlException if the specified name and path combination is illegal or unavailable,
   *     or the database files the name and path resolves to are in use by another process
   */
  Database(String type, String path, String name, HsqlProperties props) throws HsqlException {

    urlProperties = props;

    setState(Database.DATABASE_SHUTDOWN);

    sName = name;
    sType = type;
    sPath = path;

    if (sType == DatabaseURL.S_RES) {
      filesInJar = true;
      filesReadOnly = true;
    }

    // does not need to be done more than once
    try {
      classLoader = getClass().getClassLoader();
    } catch (Exception e) {

      // strict security policy:  just use the system/boot loader
      classLoader = null;
    }

    // [email protected] - changed to file access api
    String fileaccess_class_name = (String) urlProperties.getProperty("fileaccess_class_name");

    if (fileaccess_class_name != null) {
      String storagekey = urlProperties.getProperty("storage_key");

      try {
        Class fileAccessClass = null;
        try {
          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
          fileAccessClass = classLoader.loadClass(fileaccess_class_name);
        } catch (ClassNotFoundException e) {
          fileAccessClass = Class.forName(fileaccess_class_name);
        }
        Constructor constructor = fileAccessClass.getConstructor(new Class[] {Object.class});

        fileaccess = (FileAccess) constructor.newInstance(new Object[] {storagekey});
        isStoredFileAccess = true;
      } catch (Exception e) {
        throw Trace.error(Trace.INVALID_FILE_ACCESS_CLASS, new Object[] {e.toString()});
      }
    } else {
      fileaccess = FileUtil.getDefaultInstance();
    }

    shutdownOnNoConnection = urlProperties.getProperty("shutdown", "false").equals("true");
    logger = new Logger();
    compiledStatementManager = new CompiledStatementManager(this);
  }
Beispiel #3
0
  public static void main(String[] arg) {

    boolean webserver;
    String driver = "org.hsqldb.jdbcDriver";
    String url;
    String user;
    String password;
    int port;
    String defaulturl;
    String shutdownarg;

    if (arg.length > 0) {
      String p = arg[0];

      if ((p != null) && p.startsWith("-?")) {
        printHelp();

        return;
      }
    }

    HsqlProperties props = HsqlProperties.argArrayToProps(arg, "server");

    webserver = props.isPropertyTrue("server.webserver", false);
    defaulturl = webserver ? "jdbc:hsqldb:http://localhost" : "jdbc:hsqldb:hsql://localhost";

    int defaultport = webserver ? 80 : 9001;

    port = props.getIntegerProperty("server.port", defaultport);
    url = props.getProperty("server.url", defaulturl + ":" + port);
    user = props.getProperty("server.user", "sa");
    password = props.getProperty("server.password", "");
    shutdownarg = props.getProperty("server.shutdownarg", "");

    try {
      Class.forName(driver); // Load the driver

      Connection connection = DriverManager.getConnection(url, user, password);
      Statement statement = connection.createStatement();

      // can use SHUTDOWN COMPACT or SHUTDOWN IMMEDIATELY
      statement.execute("SHUTDOWN " + shutdownarg);
    } catch (ClassNotFoundException cnfe) {
      System.err.println(cnfe); // Driver not found
    } catch (SQLException sqle) {
      System.err.println(sqle); // error connection to database
    }
  }
  /**
   * Translates the legacy default database form: database=... to the 1.7.2 form: database.0=...
   *
   * @param p The properties object upon which to perform the translation
   */
  public static void translateDefaultDatabaseProperty(HsqlProperties p) {

    if (p == null) {
      return;
    }

    String defaultdb = p.getProperty(SC_KEY_DATABASE);

    if (defaultdb != null) {
      p.setProperty(SC_KEY_DATABASE + ".0", defaultdb);
    }
  }
  /**
   * Translates null or zero length value for address key to the special value
   * ServerConstants.SC_DEFAULT_ADDRESS which causes ServerSockets to be constructed without
   * specifying an InetAddress.
   *
   * @param p The properties object upon which to perform the translation
   */
  public static void translateAddressProperty(HsqlProperties p) {

    if (p == null) {
      return;
    }

    String address = p.getProperty(SC_KEY_ADDRESS);

    if (StringUtil.isEmpty(address)) {
      p.setProperty(SC_KEY_ADDRESS, SC_DEFAULT_ADDRESS);
    }
  }
  public void testHsqldbUrls() {

    HsqlProperties props;

    DatabaseURL.parseURL(
        "JDBC:hsqldb:hsql://myhost:1777/mydb;filepath=c:/myfile/database/db", true, false);
    DatabaseURL.parseURL("JDBC:hsqldb:../data/mydb.db", true, false);
    DatabaseURL.parseURL("JDBC:hsqldb:../data/mydb.db;ifexists=true", true, false);
    DatabaseURL.parseURL("JDBC:hsqldb:HSQL://localhost:9000/mydb", true, false);
    DatabaseURL.parseURL(
        "JDBC:hsqldb:Http://localhost:8080/servlet/org.hsqldb.Servlet/mydb;get_column_names=true",
        true,
        false);
    DatabaseURL.parseURL("JDBC:hsqldb:Http://localhost/servlet/org.hsqldb.Servlet/", true, false);
    DatabaseURL.parseURL("JDBC:hsqldb:hsql://myhost", true, false);

    props =
        DatabaseURL.parseURL(
            "jdbc:hsqldb:res://com.anorg.APath;hsqldb.crypt_provider=org.crypt.Provider",
            true,
            false);

    assertEquals(props.getProperty("hsqldb.crypt_provider"), "org.crypt.Provider");
    assertEquals(props.getProperty("database"), "//com.anorg.APath");

    System.setProperty("mypath", "/opt/mydir");
    props = DatabaseURL.parseURL("jdbc:hsqldb:file:${mypath}/mydata", true, false);

    assertEquals("/opt/mydir/mydata", props.getProperty("database"));

    props =
        DatabaseURL.parseURL(
            "JDBC:hsqldb:hsql://localhost:9000/mydb;file:data/hsqldb/XYZTEST;hsqldb.default_table_type=cached",
            true,
            false);
  }