typeorm/typeorm · error · ConnectionIsNotSetError

Connection with sap database is not established. Check conne

Error message

Connection with sap database is not established. Check connection configuration.

What it means

Thrown as ConnectionIsNotSetError('sap') by SapDriver.disconnect when this.master (the connection pool) is undefined, meaning the driver was never connected or has already been disconnected. The error indicates an operation that requires an established connection was attempted without one.

Source

Thrown at src/driver/sap/SapDriver.ts:365

        this.schema ??= await queryRunner.getCurrentSchema()

        await queryRunner.release()
    }

    /**
     * Makes any action after connection (e.g. create extensions in Postgres driver).
     */
    afterConnect(): Promise<void> {
        return Promise.resolve()
    }

    /**
     * Closes connection with the database.
     */
    async disconnect(): Promise<void> {
        const pool = this.master
        if (!pool) {
            throw new ConnectionIsNotSetError("sap")
        }

        this.master = undefined
        try {
            await promisify(pool.clear).call(pool)
        } catch (error) {
            this.poolErrorHandler(error)
            throw error
        }
    }

    /**
     * Obtains a new database connection to a master server.
     * Used for replication.
     * If replication is not setup then returns default connection's database connection.
     */
    async obtainMasterConnection(): Promise<any> {
        const pool = this.master

View on GitHub (pinned to 04ff4daedc)

Solutions

  1. Guard disconnect: only call dataSource.close() if dataSource.isInitialized is true.
  2. Avoid double-close by tracking connection state in your app lifecycle.
  3. Ensure connect() actually succeeded before issuing disconnect in cleanup.
  4. Wrap close() in try/catch in shutdown code so a missing connection doesn't crash teardown.

Example fix

// before
await dataSource.close() // may run twice in shutdown
await dataSource.close()

// after
if (dataSource.isInitialized) {
  await dataSource.close()
}
Defensive patterns

Strategy: validation

Validate before calling

if (dataSource.isInitialized && dataSource.driver.master) {
  await dataSource.close()
}

Try / catch

try {
  await dataSource.close()
} catch (e) {
  if (e instanceof ConnectionIsNotSetError) return // already disconnected
  throw e
}

Prevention

When it happens

Trigger: Calling await dataSource.close() / driver.disconnect() twice; calling disconnect before connect succeeded; calling disconnect after a prior disconnect or a failed connect that left master undefined.

Common situations: Double-close in shutdown hooks; app calling close() in both a signal handler and normal flow; connect() threw during init but close() still runs in cleanup; tests tearing down a DataSource that never started.

Related errors


AI-assisted analysis of typeorm/typeorm@04ff4daedc (2026-08-03). Data as JSON: /data/errors/a33808611b64b7f9.json. Report an issue: GitHub.