Skip to content

Commit

Permalink
fix: Disconnect db service on shutdown (#327)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrice Bender <patrice.bender@sap.com>
  • Loading branch information
BobdenOs and patricebender authored Nov 15, 2023
1 parent bbd3b76 commit 8471bda
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions db-service/lib/common/DatabaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ 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
*/
pools = { _factory: this.factory }
pools = Object.setPrototypeOf({}, { _factory: this.factory })

/**
* Return a pool factory + options property as expected by
Expand Down Expand Up @@ -111,11 +117,18 @@ class DatabaseService extends cds.Service {
* @param {string} tenant
*/
async disconnect(tenant) {
const pool = this.pools[tenant]
if (!pool) return
await pool.drain()
await pool.clear()
delete this.pools[tenant]
const _disconnect = async tenant => {
const pool = this.pools[tenant]
if (!pool) {
return
}
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)
}

/**
Expand Down

0 comments on commit 8471bda

Please sign in to comment.