Ejemplo n.º 1
0
 /** 初始化连接池 */
 public void initConnectionPool() {
   try {
     connectionPool = JdbcConnectionPool.create(url, username, password);
     connectionPool.setLoginTimeout(H2_LOGIN_TIMEOUT);
     connectionPool.setMaxConnections(H2_MAX_CONNECTIONS);
   } catch (Exception e) {
     log.error("init H2 JdbcConnectionPool error!", e);
   }
 }
  @Override
  public Object getObject() throws Exception {
    if (connectionPool == null) {

      // locate the repository directory
      String repositoryDirectoryPath =
          properties.getProperty(NiFiProperties.REPOSITORY_DATABASE_DIRECTORY);

      // ensure the repository directory is specified
      if (repositoryDirectoryPath == null) {
        throw new NullPointerException("Database directory must be specified.");
      }

      // create a handle to the repository directory
      File repositoryDirectory = new File(repositoryDirectoryPath);

      // create a handle to the database directory and file
      File databaseFile = new File(repositoryDirectory, AUDIT_DATABASE_FILE_NAME);
      String databaseUrl = getDatabaseUrl(databaseFile);

      // create the pool
      connectionPool =
          JdbcConnectionPool.create(databaseUrl, NF_USERNAME_PASSWORD, NF_USERNAME_PASSWORD);
      connectionPool.setMaxConnections(MAX_CONNECTIONS);

      Connection connection = null;
      ResultSet rs = null;
      Statement statement = null;
      try {
        // get a connection
        connection = connectionPool.getConnection();
        connection.setAutoCommit(false);

        // create a statement for creating/updating the database
        statement = connection.createStatement();

        // determine if the tables need to be created
        rs = connection.getMetaData().getTables(null, null, "USER", null);
        if (!rs.next()) {
          logger.info("Database not built for repository: " + databaseUrl + ".  Building now...");

          // create the tables
          statement.execute(CREATE_USER_TABLE);
          statement.execute(CREATE_AUTHORITY_TABLE);

          // seed the anonymous user
          statement.execute(INSERT_ANONYMOUS_USER);
          statement.execute(INSERT_ANONYMOUS_MONITOR_AUTHORITY);
          statement.execute(INSERT_ANONYMOUS_DFM_AUTHORITY);
          statement.execute(INSERT_ANONYMOUS_ADMIN_AUTHORITY);
          statement.execute(INSERT_ANONYMOUS_NIFI_AUTHORITY);
        } else {
          logger.info("Existing database found and connected to at: " + databaseUrl);
        }

        // close the previous result set
        RepositoryUtils.closeQuietly(rs);

        // merge in the provenance role to handle existing databases
        rs = statement.executeQuery(SELECT_ANONYMOUS_PROVENANCE_AUTHORITY);
        if (!rs.next()) {
          statement.execute(INSERT_ANONYMOUS_PROVENANCE_AUTHORITY);
        }

        // commit any changes
        connection.commit();
      } catch (SQLException sqle) {
        RepositoryUtils.rollback(connection, logger);
        throw sqle;
      } finally {
        RepositoryUtils.closeQuietly(rs);
        RepositoryUtils.closeQuietly(statement);
        RepositoryUtils.closeQuietly(connection);
      }
    }

    return connectionPool;
  }