pyramid_tm API


Set up an implicit ‘tween’ to do transaction management using the transaction package. The tween will be slotted between the Pyramid request ingress and the Pyramid exception view handler.

For every request it handles, the tween will begin a transaction by calling, and will then call the downstream handler (usually the main Pyramid application request handler) to obtain a response. When attempting to call the downstream handler:

  • If an exception is raised by downstream handler while attempting to obtain a response, the transaction will be rolled back ( will be called).
  • If no exception is raised by the downstream handler, but the transaction is doomed ( has been called), the transaction will be rolled back.
  • If the deployment configuration specifies a tm.commit_veto setting, and the transaction management tween receives a response from the downstream handler, the commit veto hook will be called. If it returns True, the transaction will be rolled back. If it returns False, the transaction will be committed.
  • If none of the above conditions are true, the transaction will be committed (via

Return True if the request is currently being managed by the pyramid_tm tween. If False then it may be necessary to manage transactions yourself.


This does not indicate that there is a current transaction. For example, may raise a NoTransaction error even though is_tm_active returns True. This would be caused by user code that manually completed a transaction and did not begin a new one.

pyramid_tm.default_commit_veto(request, response)

When used as a commit veto, the logic in this function will cause the transaction to be aborted if:

  • An X-Tm response header with the value abort (or any value other than commit) exists.
  • The response status code starts with 4 or 5.

Otherwise the transaction will be allowed to commit.

pyramid_tm.tm_tween_factory(handler, registry)

Create a new transaction.TransactionManager in explicit mode.

This is recommended transaction manager and will help to weed out errors caused by code that tweaks the transaction before it has begun or after it has ended.

class pyramid_tm.TMActivePredicate(val, config)

A view predicate registered as tm_active. Can be used to determine if an exception view should execute based on whether it’s the last retry attempt before aborting the request.