@@ -2641,4 +2641,50 @@ t('query during copy error', async() => {
26412641 ]
26422642} )
26432643
2644+ t ( 'idle_in_transaction_session_timeout causes CONNECTION_CLOSED' , { timeout : 5 } , async ( ) => {
2645+ const sql = postgres ( options )
2646+ const error = await sql . begin ( async ( txSql ) => {
2647+ await txSql `SET LOCAL idle_in_transaction_session_timeout = '1s'`
2648+ await new Promise ( r => setTimeout ( r , 2000 ) )
2649+ await txSql `SELECT 1`
2650+ } ) . catch ( e => e )
2651+ await sql . end ( )
2652+ return [ true , error . code === 'CONNECTION_CLOSED' || error . code === 'CONNECTION_DESTROYED' ]
2653+ } )
2654+
2655+ t ( 'txSql fails after idle_in_transaction_session_timeout even if callback continues' , { timeout : 5 } , async ( ) => {
2656+ const sql = postgres ( options )
2657+ let failed = false
2658+ await sql . begin ( async ( txSql ) => {
2659+ await txSql `SET LOCAL idle_in_transaction_session_timeout = '1s'`
2660+ await new Promise ( r => setTimeout ( r , 2000 ) )
2661+ try { await txSql `SELECT 1` } catch ( e ) {
2662+ failed = e . code === 'CONNECTION_CLOSED' || e . code === 'CONNECTION_DESTROYED'
2663+ }
2664+ } ) . catch ( ( ) => { /* ignore */ } )
2665+ await new Promise ( r => setTimeout ( r , 1000 ) )
2666+ await sql . end ( )
2667+ return [ true , failed ]
2668+ } )
2669+
2670+ t ( 'txSql fails even when pool connection reconnects after idle timeout' , { timeout : 10 } , async ( ) => {
2671+ const sql = postgres ( { ...options , max : 1 } )
2672+ // Queue pool queries to trigger reconnect after connection closes
2673+ for ( let i = 0 ; i < 3 ; i ++ )
2674+ setTimeout ( ( ) => sql `SELECT ${ i } ` . catch ( ( ) => { /* ignore */ } ) , 1000 + i * 100 )
2675+
2676+ let txSqlSucceeded = false
2677+ await sql . begin ( async ( txSql ) => {
2678+ await txSql `SET LOCAL idle_in_transaction_session_timeout = '1s'`
2679+ await new Promise ( r => setTimeout ( r , 2000 ) )
2680+ try {
2681+ await txSql `SELECT 'should fail'`
2682+ txSqlSucceeded = true
2683+ } catch { /* ignore */ }
2684+ } ) . catch ( ( ) => { /* ignore */ } )
2685+ await new Promise ( r => setTimeout ( r , 1000 ) )
2686+ await sql . end ( )
2687+ return [ false , txSqlSucceeded ]
2688+ } )
2689+
26442690; globalThis . addEventListener ( "unload" , ( ) => Deno . exit ( process . exitCode ) )
0 commit comments