@@ -689,49 +689,55 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
689689 opts : this . _observabilityOptions ,
690690 dbName : this . database . formattedName_ ,
691691 } ;
692- return startTrace ( 'SessionPool.createSessions' , traceConfig , async span => {
693- span . addEvent ( `Requesting ${ amount } sessions` ) ;
694-
695- // while we can request as many sessions be created as we want, the backend
696- // will return at most 100 at a time, hence the need for a while loop.
697- while ( amount > 0 ) {
698- let sessions : Session [ ] | null = null ;
699-
700- span . addEvent ( `Creating ${ amount } sessions` ) ;
701-
702- try {
703- [ sessions ] = await this . database . batchCreateSessions ( {
704- count : amount ,
705- labels : labels ,
706- databaseRole : databaseRole ,
707- } ) ;
708-
709- amount -= sessions . length ;
710- nReturned += sessions . length ;
711- } catch ( e ) {
712- this . _pending -= amount ;
713- this . emit ( 'createError' , e ) ;
692+ return context . with ( ROOT_CONTEXT , ( ) => {
693+ return startTrace (
694+ 'SessionPool.createSessions' ,
695+ traceConfig ,
696+ async span => {
697+ span . addEvent ( `Requesting ${ amount } sessions` ) ;
698+
699+ // while we can request as many sessions be created as we want, the backend
700+ // will return at most 100 at a time, hence the need for a while loop.
701+ while ( amount > 0 ) {
702+ let sessions : Session [ ] | null = null ;
703+
704+ span . addEvent ( `Creating ${ amount } sessions` ) ;
705+
706+ try {
707+ [ sessions ] = await this . database . batchCreateSessions ( {
708+ count : amount ,
709+ labels : labels ,
710+ databaseRole : databaseRole ,
711+ } ) ;
712+
713+ amount -= sessions . length ;
714+ nReturned += sessions . length ;
715+ } catch ( e ) {
716+ this . _pending -= amount ;
717+ this . emit ( 'createError' , e ) ;
718+ span . addEvent (
719+ `Requested for ${ nRequested } sessions returned ${ nReturned } ` ,
720+ ) ;
721+ setSpanErrorAndException ( span , e as Error ) ;
722+ span . end ( ) ;
723+ throw e ;
724+ }
725+
726+ sessions . forEach ( ( session : Session ) => {
727+ setImmediate ( ( ) => {
728+ this . _inventory . borrowed . add ( session ) ;
729+ this . _pending -= 1 ;
730+ this . release ( session ) ;
731+ } ) ;
732+ } ) ;
733+ }
734+
714735 span . addEvent (
715736 `Requested for ${ nRequested } sessions returned ${ nReturned } ` ,
716737 ) ;
717- setSpanErrorAndException ( span , e as Error ) ;
718738 span . end ( ) ;
719- throw e ;
720- }
721-
722- sessions . forEach ( ( session : Session ) => {
723- setImmediate ( ( ) => {
724- this . _inventory . borrowed . add ( session ) ;
725- this . _pending -= 1 ;
726- this . release ( session ) ;
727- } ) ;
728- } ) ;
729- }
730-
731- span . addEvent (
732- `Requested for ${ nRequested } sessions returned ${ nReturned } ` ,
739+ } ,
733740 ) ;
734- span . end ( ) ;
735741 } ) ;
736742 }
737743
0 commit comments