pubkey/rxdb · error · RxError

DB8

DB8

Error message

RxDB Error-Code: DB8. Hint: Error messages are not included in RxDB core to reduce build size. To show the full error messages and to ensure that you do not make any mistakes when using RxDB, use the dev-mode plugin when you are in development mode: https://rxdb.info/dev-mode.html?console=error

What it means

DB8 is thrown when you call createRxDatabase() with a name+storage combination that is already in use in the current process. RxDB keeps a global registry (USED_DATABASE_NAMES) of every created database per storage to prevent two RxDatabase instances from writing to the same underlying storage, which would corrupt change detection and replication. The registry entry is only cleared when the database is closed.

Source

Thrown at src/rx-database.ts:707

    > {
        return this as any;
    }

    registerWebMCP(_options?: WebMCPOptions): { error$: Subject<Error>; log$: Subject<WebMCPLogEvent>; } {
        throw pluginMissing('webmcp');
    }
}

/**
 * checks if an instance with same name and storage already exists
 * @throws {RxError} if used
 */
function throwIfDatabaseNameUsed(
    name: string,
    storage: RxStorage<any, any>
) {
    if (USED_DATABASE_NAMES.has(getDatabaseNameKey(name, storage))) {
        throw newRxError('DB8', {
            name,
            storage: storage.name,
            link: 'https://rxdb.info/rx-database.html#ignoreduplicate'
        });
    }
}

/**
 * Polyfill for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
 */
function createPromiseWithResolvers<T>() {
    let resolve!: (value: T | PromiseLike<T>) => void;
    let reject!: (reason?: any) => void;
    const promise = new Promise<T>((res, rej) => {
        resolve = res;
        reject = rej;
    });
    return { promise, resolve, reject };

View on GitHub (pinned to af6fb65f94)

Solutions

  1. Close the existing database before re-creating: await oldDatabase.close() (or call remove() to also delete its data).
  2. Keep the database instance in a module-level singleton and reuse it instead of calling createRxDatabase again.
  3. If you intentionally want to recreate with the same name, pass { ignoreDuplicate: true } to createRxDatabase in dev mode (requires the dev-mode plugin, see DB9).
  4. Fix hot-reload/test setup to await creation and close databases in cleanup hooks.

Example fix

// before
const db = await createRxDatabase({ name: 'mydb', storage: getRxStorageDexie() });
// after
let dbPromise;
export function getDb() {
  if (!dbPromise) {
    dbPromise = createRxDatabase({ name: 'mydb', storage: getRxStorageDexie() });
  }
  return dbPromise;
}
Defensive patterns

Strategy: try-catch

Validate before calling

import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';
// keep a module-level handle so you can check/close before recreating
export let dbInstance;
export async function ensureClosed() {
  if (dbInstance && !dbInstance.closed) {
    await dbInstance.close();
    dbInstance = undefined;
  }
}

Type guard

function canCreate(db) {
  return !dbInstance || dbInstance.closed || dbInstance.name !== db.name;
}

Try / catch

try {
  dbInstance = await createRxDatabase({ name: 'mydb', storage });
} catch (err) {
  if (String(err?.code) === 'DB8') {
    await dbInstance?.close();
    dbInstance = await createRxDatabase({ name: 'mydb', storage });
  } else {
    throw err;
  }
}

Prevention

When it happens

Trigger: Calling createRxDatabase() a second time with the same name and same RxStorage instance, e.g. re-running an init function on hot reload, or after an earlier createRxDatabase() call was not closed before retrying.

Common situations: Hot module reloading in dev servers that re-executes database setup code; test suites that create the same database in beforeEach without closing the previous one; accidental double-invocation of an async init function (e.g. React StrictMode double effects); calling createRxDatabase after a failed creation left the name registered.

Related errors


AI-assisted analysis of pubkey/rxdb@af6fb65f94 (2026-08-31). Data as JSON: /api/errors/a52eacbae5e69d60. Report an issue: GitHub.