[sqlalchemy-20] Use session.transaction information to decide if active

The "session.transaction" member "_connections" is a dictionary with the
active database connections of the current transaction. Neutron will use
it to determine if the session is active or not. This parameter will
provide more accurate information than "new", "dirty" or "deleted". A
read (SELECT) operation won't be included on those lists while the
transaction is currently active.

This patch is patch is a direct transcription of [1].

[1]https://review.opendev.org/c/openstack/neutron/+/843256

Related-Bug: #1975542

Change-Id: Ibc04a1940c3833d3e10dbfc011598ac11c58ba13
This commit is contained in:
Rodolfo Alonso Hernandez
2022-07-22 03:44:21 +02:00
parent 89dc650220
commit 5b1dab0e8f

View File

@@ -473,14 +473,14 @@ def is_session_active(session):
session transaction will not end at the end of a reader/writer context.
In this case, a session could have an active transaction even when it is
not inside a reader/writer context. In order to mimic the previous
behaviour, this method checks the pending new, deleted and dirty elements
to be flushed.
behaviour, this method checks if there is a transaction created and if
the transaction has any active connection against the database server.
"""
if getattr(session, 'autocommit', None):
# old behaviour, to be removed with sqlalchemy 2.0
return session.is_active
if not session.transaction:
return False
if not (session.dirty or session.deleted or session.new):
if not session.transaction._connections:
return False
return True