То есть, закрытие соединения на уровне кода..
Connections can be used as context managers. Note that a context wraps a transaction: if the context exits with success the transaction is committed, if it exits with an exception the transaction is rolled back. Note that the connection is not closed by the context and it can be used for several contexts
SET SESSION idle_in_transaction_session_timeout = '2min';
EXE
, как архив (то есть можно найти ресурсы):