pyramid_tm API


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

For every request it handles, the tween will begin a transaction by calling transaction.begin(), 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 (transaction.abort() will be called).

  • If no exception is raised by the downstream handler, but the transaction is doomed (transaction.doom() 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 transaction.commit()).

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)