88 RequestParameter ,
99 VulcanExtensionId ,
1010} from '@vulcan-sql/core' ;
11- import { Pool , PoolConfig , QueryResult } from 'pg' ;
11+ import { Pool , PoolClient , PoolConfig , QueryResult } from 'pg' ;
1212import * as Cursor from 'pg-cursor' ;
1313import { Readable } from 'stream' ;
1414import { buildSQL } from './sqlBuilder' ;
@@ -119,8 +119,9 @@ export class CannerDataSource extends DataSource<any, PGOptions> {
119119 const auth = headers ?. [ 'authorization' ] ;
120120 const password = auth ?. trim ( ) . split ( ' ' ) [ 1 ] ;
121121 const pool = this . getPool ( profileName , password ) ;
122- const client = await pool . connect ( ) ;
122+ let client : PoolClient | undefined ;
123123 try {
124+ client = await pool . connect ( ) ;
124125 const builtSQL = buildSQL ( sql , operations ) ;
125126 const cursor = client . query (
126127 new Cursor ( builtSQL , Array . from ( bindParams . values ( ) ) )
@@ -131,15 +132,15 @@ export class CannerDataSource extends DataSource<any, PGOptions> {
131132 ) ;
132133 // It is important to close the cursor before releasing connection, or the connection might not able to handle next request.
133134 await cursor . close ( ) ;
134- client . release ( ) ;
135+ if ( client ) client . release ( ) ;
135136 } ) ;
136137 // All promises MUST fulfilled in this function or we are not able to release the connection when error occurred
137138 return await this . getResultFromCursor ( cursor , options ) ;
138139 } catch ( e : any ) {
139140 this . logger . debug (
140141 `Errors occurred, release connection from ${ profileName } `
141142 ) ;
142- client . release ( ) ;
143+ if ( client ) client . release ( ) ;
143144 throw e ;
144145 }
145146 }
0 commit comments