Exemple #1
0
  public static synchronized void init(String host, int port) {
    hostAndPort = host + ":" + port;

    StorageEngineManager.initStorageEngines();
    SQLEngineManager.initSQLEngines();
    SystemDatabase.init();

    for (String dbName : SystemDatabase.findAll())
      DATABASES.put(dbName, new Database(INSTANCE, true));
  }
Exemple #2
0
  private Session createSession(ConnectionInfo ci, boolean ifExists) {
    String name = ci.getDatabaseName();
    name = Database.parseDatabaseShortName(ci.getDbSettings(), name);
    Database database;
    boolean openNew = ci.getProperty("OPEN_NEW", false);
    if (openNew) {
      database = null;
    } else {
      database = DATABASES.get(name);
    }
    User user = null;
    boolean opened = false;
    if (database == null) {
      if (ifExists && !SystemDatabase.exists(name)) {
        throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, name);
      }
      database = createDatabase(ci.isPersistent());
      database.init(ci, name);
      opened = true;
      if (database.getAllUsers().isEmpty()) {
        // users is the last thing we add, so if no user is around,
        // the database is new (or not initialized correctly)
        user = new User(database, database.allocateObjectId(), ci.getUserName(), false);
        user.setAdmin(true);
        user.setUserPasswordHash(ci.getUserPasswordHash());
        database.setMasterUser(user);
      }
      DATABASES.put(name, database);

      if (database.isPersistent())
        SystemDatabase.addDatabase(database.getShortName(), database.getStorageEngineName());
    } else {
      if (!database.isInitialized()) database.init(ci, name);
    }

    synchronized (database) {
      if (opened) {
        // start the thread when already synchronizing on the database
        // otherwise a deadlock can occur when the writer thread
        // opens a new database (as in recovery testing)
        database.opened();
      }
      if (database.isClosing()) {
        return null;
      }
      if (user == null) {
        if (database.validateFilePasswordHash(
            ci.getProperty("CIPHER", null), ci.getFilePasswordHash())) {
          user = database.findUser(ci.getUserName());
          if (user != null) {
            if (!user.validateUserPasswordHash(ci.getUserPasswordHash())) {
              user = null;
            }
          }
        }
        if (opened && (user == null || !user.isAdmin())) {
          // reset - because the user is not an admin, and has no
          // right to listen to exceptions
          database.setEventListener(null);
        }
      }
      if (user == null) {
        database.removeSession(null);
        throw DbException.get(ErrorCode.WRONG_USER_OR_PASSWORD);
      }
      Session session = database.createSession(user);
      session.setConnectionInfo(ci);
      return session;
    }
  }