* Frees any client-side resources held by the current session. If a session is in a transaction, * the transaction is aborted. * * Does not end the session on the server. * * @param options - Optional settings. Currently reserved for future use
(options?: EndSessionOptions)
| 265 | * @param options - Optional settings. Currently reserved for future use |
| 266 | */ |
| 267 | async endSession(options?: EndSessionOptions): Promise<void> { |
| 268 | try { |
| 269 | if (this.inTransaction()) { |
| 270 | await this.abortTransaction({ ...options, throwTimeout: true }); |
| 271 | } |
| 272 | } catch (error) { |
| 273 | // spec indicates that we should ignore all errors for `endSessions` |
| 274 | if (error.name === 'MongoOperationTimeoutError') throw error; |
| 275 | squashError(error); |
| 276 | } finally { |
| 277 | if (!this.hasEnded) { |
| 278 | const serverSession = this.serverSession; |
| 279 | if (serverSession != null) { |
| 280 | // release the server session back to the pool |
| 281 | this.sessionPool.release(serverSession); |
| 282 | // Store a clone of the server session for reference (debugging) |
| 283 | this._serverSession = new ServerSession(serverSession); |
| 284 | } |
| 285 | // mark the session as ended, and emit a signal |
| 286 | this.hasEnded = true; |
| 287 | this.emit('ended', this); |
| 288 | } |
| 289 | maybeClearPinnedConnection(this, { force: true, ...options }); |
| 290 | } |
| 291 | } |
| 292 | /** |
| 293 | * An alias for {@link ClientSession.endSession|ClientSession.endSession()}. |
| 294 | */ |