From d2c7e4193fb917596a8721345132b9c2d3a7de17 Mon Sep 17 00:00:00 2001 From: Bob den Os <bob.den.os@sap.com> Date: Mon, 30 Oct 2023 10:31:02 +0100 Subject: [PATCH 1/4] Disconnect db service on shutdown --- db-service/lib/common/DatabaseService.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/db-service/lib/common/DatabaseService.js b/db-service/lib/common/DatabaseService.js index 94abf8935..6c210dd6a 100644 --- a/db-service/lib/common/DatabaseService.js +++ b/db-service/lib/common/DatabaseService.js @@ -6,6 +6,12 @@ const cds = require('@sap/cds/lib') /** @typedef {unknown} DatabaseDriver */ class DatabaseService extends cds.Service { + + init() { + cds.on('shutdown', () => this.disconnect()) + return super.init() + } + /** * Dictionary of connection pools per tenant */ From 9a370c5e38e09b8d4ca51f770efb23d908c45b34 Mon Sep 17 00:00:00 2001 From: Bob den Os <bob.den.os@sap.com> Date: Tue, 7 Nov 2023 10:57:18 +0100 Subject: [PATCH 2/4] Disconnect all tenants if not specified --- db-service/lib/common/DatabaseService.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/db-service/lib/common/DatabaseService.js b/db-service/lib/common/DatabaseService.js index 6c210dd6a..cfdadc493 100644 --- a/db-service/lib/common/DatabaseService.js +++ b/db-service/lib/common/DatabaseService.js @@ -118,7 +118,11 @@ class DatabaseService extends cds.Service { */ async disconnect(tenant) { const pool = this.pools[tenant] - if (!pool) return + if (!pool) { + if (tenant == null) + return Promise.all(Object.keys(this.pools).map(t => this.disconnect(t))) + return + } await pool.drain() await pool.clear() delete this.pools[tenant] From 25d29a443e2041f06e5ca926a3ad1e4923c0159f Mon Sep 17 00:00:00 2001 From: Bob den Os <bob.den.os@sap.com> Date: Tue, 7 Nov 2023 13:32:22 +0100 Subject: [PATCH 3/4] Re arrange disconnect --- db-service/lib/common/DatabaseService.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/db-service/lib/common/DatabaseService.js b/db-service/lib/common/DatabaseService.js index cfdadc493..50d5dfcc4 100644 --- a/db-service/lib/common/DatabaseService.js +++ b/db-service/lib/common/DatabaseService.js @@ -117,15 +117,18 @@ class DatabaseService extends cds.Service { * @param {string} tenant */ async disconnect(tenant) { - const pool = this.pools[tenant] - if (!pool) { - if (tenant == null) - return Promise.all(Object.keys(this.pools).map(t => this.disconnect(t))) - return + const _disconnect = async tenant => { + const pool = this.pools[tenant] + if (!pool) { + return + } + await pool.drain() + await pool.clear() + delete this.pools[tenant] } - await pool.drain() - await pool.clear() - delete this.pools[tenant] + if (tenant == null) + return Promise.all(Object.keys(this.pools).map(_disconnect)) + return _disconnect(tenant) } /** From f3a4dfff21584791e2d0f98ebcdbcfe45ad3a587 Mon Sep 17 00:00:00 2001 From: Bob den Os <bob.den.os@sap.com> Date: Tue, 7 Nov 2023 13:42:30 +0100 Subject: [PATCH 4/4] Hide _factory to simplify disconnect --- db-service/lib/common/DatabaseService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db-service/lib/common/DatabaseService.js b/db-service/lib/common/DatabaseService.js index 50d5dfcc4..96e32bff2 100644 --- a/db-service/lib/common/DatabaseService.js +++ b/db-service/lib/common/DatabaseService.js @@ -15,7 +15,7 @@ class DatabaseService extends cds.Service { /** * Dictionary of connection pools per tenant */ - pools = { _factory: this.factory } + pools = Object.setPrototypeOf({}, { _factory: this.factory }) /** * Return a pool factory + options property as expected by