{"id":"a33808611b64b7f9","repo":"typeorm/typeorm","slug":"connection-with-sap-database-is-not-established-c","errorCode":null,"errorMessage":"Connection with sap database is not established. Check connection configuration.","messagePattern":"Connection with sap database is not established\\. Check connection configuration\\.","errorType":"exception","errorClass":"ConnectionIsNotSetError","httpStatus":null,"severity":"error","filePath":"src/driver/sap/SapDriver.ts","lineNumber":365,"sourceCode":"        this.schema ??= await queryRunner.getCurrentSchema()\n\n        await queryRunner.release()\n    }\n\n    /**\n     * Makes any action after connection (e.g. create extensions in Postgres driver).\n     */\n    afterConnect(): Promise<void> {\n        return Promise.resolve()\n    }\n\n    /**\n     * Closes connection with the database.\n     */\n    async disconnect(): Promise<void> {\n        const pool = this.master\n        if (!pool) {\n            throw new ConnectionIsNotSetError(\"sap\")\n        }\n\n        this.master = undefined\n        try {\n            await promisify(pool.clear).call(pool)\n        } catch (error) {\n            this.poolErrorHandler(error)\n            throw error\n        }\n    }\n\n    /**\n     * Obtains a new database connection to a master server.\n     * Used for replication.\n     * If replication is not setup then returns default connection's database connection.\n     */\n    async obtainMasterConnection(): Promise<any> {\n        const pool = this.master","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/typeorm/typeorm/blob/04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96/src/driver/sap/SapDriver.ts#L347-L383","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Guard disconnect: only call dataSource.close() if dataSource.isInitialized is true.","Avoid double-close by tracking connection state in your app lifecycle.","Ensure connect() actually succeeded before issuing disconnect in cleanup.","Wrap close() in try/catch in shutdown code so a missing connection doesn't crash teardown."],"exampleFix":"// before\nawait dataSource.close() // may run twice in shutdown\nawait dataSource.close()\n\n// after\nif (dataSource.isInitialized) {\n  await dataSource.close()\n}","handlingStrategy":"validation","validationCode":"if (dataSource.isInitialized && dataSource.driver.master) {\n  await dataSource.close()\n}","typeGuard":null,"tryCatchPattern":"try {\n  await dataSource.close()\n} catch (e) {\n  if (e instanceof ConnectionIsNotSetError) return // already disconnected\n  throw e\n}","preventionTips":["Only call close() when isInitialized is true.","Track connection lifecycle to prevent double-close in shutdown hooks.","Ensure initialize() succeeded before teardown expects a connection."],"tags":["sap","driver","connection","lifecycle"],"analyzedSha":"04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96","analyzedAt":"2026-08-03T18:27:32.281Z","schemaVersion":2}