/* package */
  DbSequence openSequence(final Db db, final DbTxn txn, final DatabaseEntry key)
      throws DatabaseException {

    final DbSequence seq = createSequence(db);
    // The DB_THREAD flag is inherited from the database
    boolean threaded = ((db.get_open_flags() & DbConstants.DB_THREAD) != 0);

    int openFlags = 0;
    openFlags |= allowCreate ? DbConstants.DB_CREATE : 0;
    openFlags |= exclusiveCreate ? DbConstants.DB_EXCL : 0;
    openFlags |= threaded ? DbConstants.DB_THREAD : 0;

    if (db.get_transactional() && txn == null) openFlags |= DbConstants.DB_AUTO_COMMIT;

    configureSequence(seq, DEFAULT);
    boolean succeeded = false;
    try {
      seq.open(txn, key, openFlags);
      succeeded = true;
      return seq;
    } finally {
      if (!succeeded)
        try {
          seq.close(0);
        } catch (Throwable t) {
          // Ignore it -- an exception is already in flight.
        }
    }
  }
  /* package */
  SecondaryConfig(final Db db) throws DatabaseException {

    super(db);

    // XXX: There is no way to find out what flags were passed to associate.
    allowPopulate = false;
    immutableSecondaryKey = false;
    keyCreator = db.get_seckey_create();
    multiKeyCreator = db.get_secmultikey_create();
  }
  /* package */
  Db openSecondaryDatabase(
      final DbEnv dbenv,
      final DbTxn txn,
      final String fileName,
      final String databaseName,
      final Db primary)
      throws DatabaseException, java.io.FileNotFoundException {
    int associateFlags = 0;
    int foreignFlags = 0;
    associateFlags |= allowPopulate ? DbConstants.DB_CREATE : 0;
    if (getTransactional() && txn == null) associateFlags |= DbConstants.DB_AUTO_COMMIT;
    if (immutableSecondaryKey) associateFlags |= DbConstants.DB_IMMUTABLE_KEY;

    final Db db = super.openDatabase(dbenv, txn, fileName, databaseName);
    boolean succeeded = false;
    try {
      /*
       * The multi-key creator must be set before the call to associate
       * so that we can work out whether the C API callback should be
       * set or not.
       */
      db.set_secmultikey_create(multiKeyCreator);
      primary.associate(txn, db, keyCreator, associateFlags);
      if (foreign != null) {
        db.set_foreignmultikey_nullifier(multiKeyNullifier);
        foreign.associate_foreign(db, keyNullifier, foreignFlags | fkDelAction.getId());
      }
      succeeded = true;
      return db;
    } finally {
      if (!succeeded)
        try {
          db.close(0);
        } catch (Throwable t) {
          // Ignore it -- there is already an exception in flight.
        }
    }
  }