@@ -95,6 +95,7 @@ export interface EndSessionOptions {
9595 */
9696 error ?: AnyError ;
9797 force ?: boolean ;
98+ forceClear ?: boolean ;
9899}
99100
100101/**
@@ -225,7 +226,7 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
225226 }
226227
227228 /** @internal */
228- unpin ( options ?: { force ?: boolean ; error ?: AnyError } ) : void {
229+ unpin ( options ?: { force ?: boolean ; forceClear ?: boolean ; error ?: AnyError } ) : void {
229230 if ( this . loadBalanced ) {
230231 return maybeClearPinnedConnection ( this , options ) ;
231232 }
@@ -479,16 +480,28 @@ export function maybeClearPinnedConnection(
479480
480481 // NOTE: the spec talks about what to do on a network error only, but the tests seem to
481482 // to validate that we don't unpin on _all_ errors?
482- if ( conn && ( options ?. error == null || options ?. force ) ) {
483+ if ( conn ) {
483484 const servers = Array . from ( session . topology . s . servers . values ( ) ) ;
484485 const loadBalancer = servers [ 0 ] ;
485- loadBalancer . s . pool . checkIn ( conn ) ;
486- conn . emit (
487- Connection . UNPINNED ,
488- session . transaction . state !== TxnState . NO_TRANSACTION
489- ? ConnectionPoolMetrics . TXN
490- : ConnectionPoolMetrics . CURSOR
491- ) ;
486+
487+ if ( options ?. error == null || options ?. force ) {
488+ loadBalancer . s . pool . checkIn ( conn ) ;
489+ conn . emit (
490+ Connection . UNPINNED ,
491+ session . transaction . state !== TxnState . NO_TRANSACTION
492+ ? ConnectionPoolMetrics . TXN
493+ : ConnectionPoolMetrics . CURSOR
494+ ) ;
495+
496+ if ( options ?. forceClear ) {
497+ loadBalancer . s . pool . clear ( conn . serviceId ) ;
498+ // NOTE: This is not truly necessary, but in the test suite it is. Clearing
499+ // the connection pool will force the connection to get destroyed on the next check
500+ // out, but in the test suite we may not check out a connection again after
501+ // the assertions are finished.
502+ conn . destroy ( ) ;
503+ }
504+ }
492505
493506 session [ kPinnedConnection ] = undefined ;
494507 }
0 commit comments