in pyqldb/session/qldb_session.py [0:0]
def _execute_lambda(self, query_lambda):
"""
Implicitly start a transaction, execute the lambda function, and commit the transaction.
:type query_lambda: function
:param query_lambda: The lambda function to execute. A lambda function cannot have any side effects as
it may be invoked multiple times, and the result cannot be trusted until the transaction is
committed.
:rtype: :py:class:`pyqldb.cursor.buffered_cursor.BufferedCursor`/object
:return: The return value of the lambda function which could be a
:py:class:`pyqldb.cursor.buffered_cursor.BufferedCursor` on the result set of a statement within the
lambda.
:raises ExecuteError: Error containing the context of a failure during execute.
"""
transaction = None
transaction_id = None
try:
transaction = self._start_transaction()
result = query_lambda(Executor(transaction))
if isinstance(result, StreamCursor):
# If someone accidentally returned a StreamCursor object which would become invalidated by the
# commit, automatically buffer it to allow them to use the result anyway.
result = BufferedCursor(result)
transaction._commit()
return result
except Exception as e:
is_retryable = is_retriable_exception(e)
is_session_invalid = is_invalid_session_exception(e)
if is_session_invalid and not is_transaction_expired_exception(e):
# Underlying session is dead on InvalidSessionException except for transaction expiry.
self._is_alive = False
elif not is_occ_conflict_exception(e):
# OCC does not need session state reset as the transaction is implicitly closed.
self._no_throw_abort()
if transaction is not None:
transaction_id = transaction.transaction_id
raise ExecuteError(e, is_retryable, is_session_invalid, transaction_id)
finally:
if transaction is not None:
transaction._close_child_cursors()