{"id":6433,"library":"pyramid-tm","title":"Pyramid Transaction Manager","description":"pyramid_tm is a package that integrates the Python `transaction` package with the Pyramid web framework, allowing Pyramid requests to join the active transaction. It provides centralized transaction management for Pyramid applications without relying on external WSGI middleware. Maintained by the Pylons Project, it has a generally active release cadence, with version 2.6 released in November 2024.","status":"active","version":"2.6","language":"en","source_language":"en","source_url":"https://github.com/Pylons/pyramid_tm","tags":["pyramid","transaction","wsgi","pylons","transaction-manager"],"install":[{"cmd":"pip install pyramid-tm","lang":"bash","label":"Install `pyramid-tm`"}],"dependencies":[{"reason":"Core web framework integration.","package":"pyramid"},{"reason":"Provides the underlying transaction management system.","package":"transaction"},{"reason":"Enhances retryable exception handling for transactions (optional).","package":"pyramid_retry","optional":true},{"reason":"Integration with SQLAlchemy sessions for transaction management (optional).","package":"zope.sqlalchemy","optional":true}],"imports":[{"note":"Include the package in your Pyramid application's configuration.","symbol":"pyramid_tm","correct":"config.include('pyramid_tm')"}],"quickstart":{"code":"from wsgiref.simple_server import make_server\nfrom pyramid.config import Configurator\nfrom pyramid.response import Response\n\ndef hello_world(request):\n    # Any database operations here would automatically join the active transaction\n    return Response('Hello World from pyramid_tm!')\n\nif __name__ == '__main__':\n    with Configurator() as config:\n        config.include('pyramid_tm') # Enable pyramid_tm\n        config.add_route('hello', '/')\n        config.add_view(hello_world, route_name='hello')\n        app = config.make_wsgi_app()\n    server = make_server('0.0.0.0', 6543, app)\n    print(\"Pyramid server running on http://0.0.0.0:6543\")\n    server.serve_forever()","lang":"python","description":"This minimal Pyramid application demonstrates how to include `pyramid_tm`. Once included, any operations involving data managers (like ZODB or SQLAlchemy with `zope.sqlalchemy`) will automatically join the transaction associated with the request."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or later, or use an older `pyramid-tm` version compatible with your Python runtime.","message":"Version 2.6 dropped support for Python 3.7 and 3.8. Ensure your environment uses Python 3.9 or newer.","severity":"breaking","affected_versions":">=2.6"},{"fix":"Review and update any custom `tm.commit_veto` implementations to account for the new behavior regarding squashed exceptions.","message":"The behavior of the `tm.commit_veto` hook changed in version 2.2. It will now be consulted for squashed exceptions (exceptions handled by an exception view) instead of always aborting. It inspects `request.exception` and the response to determine whether to commit or abort.","severity":"breaking","affected_versions":">=2.2"},{"fix":"Ensure `request.tm` is only accessed within the context where the `pyramid_tm` tween is active. For testing, set `tm.active` in the WSGI environ to `False` to disable it, or `tm.manager` to provide your own transaction manager.","message":"Accessing `request.tm` outside the `pyramid_tm` tween or during a request where `pyramid_tm` was explicitly disabled (e.g., via an activate hook or `tm.active` in WSGI environ for testing) will raise an `AttributeError`.","severity":"gotcha","affected_versions":"All"},{"fix":"After explicit `abort()` or `commit()`, call `request.tm.begin()` if further transaction-managed operations are required within the same request cycle.","message":"If you manually complete a transaction using `request.tm.abort()` or `request.tm.commit()`, subsequent uses of the transaction manager within the same request will fail unless `request.tm.begin()` is explicitly invoked again to start a new transaction.","severity":"gotcha","affected_versions":"All"},{"fix":"Remove `repoze.tm` or `repoze.tm2` from your WSGI pipeline if you intend to use `pyramid_tm` for transaction management.","message":"If `repoze.tm` or `repoze.tm2` middleware is present in the WSGI pipeline, `pyramid_tm` becomes inactive. These middlewares handle transaction management themselves and conflict with `pyramid_tm`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z"}