Pony ORM 0.4.7 is released.
Starting with this release Pony requires that all database interactions are done within an explicitly created session. In order to set session boundaries one needs to use the @db_session decorator:
@db_session def handle_request(params): ...
Another option is to use db_session as a context manager:
with db_session: ...
When a decorated function or the context manager exits Pony clears the transaction cache and returns the database connection to the connection pool. If data was changed within the session then Pony will commit transaction to the database. If the function raises an exception then Pony performs a rollback of the transaction.
In previous versions, Pony would start a session implicitly and sometimes it would result in an UnrepeatableReadError because an obsolete transaction cache wouldn’t be cleared in a timely manner. Now Pony eliminates this problem and requires that all database interactions be done within db_session, even if all of the operations performed are read-only. If you omit the db_session decorator or context manager, Pony will raise the TransactionError exception with the following text: ‘db_session is required when working with the database’. You can omit the db_session only if you work with Python interactive shell.
Essentially @db_session replaces the @with_transaction decorator and now @with_transaction is deprecated. You can find a couple of examples here
This release also includes a couple of other updates:
– Pickling/unpickling objects is now supported (e.g. for storing in memcached)
– Query can be built step-by-step using the .filter() method: q = select(…); q.filter(…); q.filter(…)
– Lazy collections support: Set(…, lazy=true). When user requests items from a collection one by one, Pony loads all collection items after a threshold is reached. This behavior can be switched off by setting lazy to true, so Pony will not do prefetching and make a request to the database each time a new item is needed. This feature can be useful for large collections when only small and unpredictable portions of collection are required.
– MySQL fractional seconds fix. This bug appeared as an UnrepeatableReadError while working with MySQL DATETIME columns
– Several bug fixes