Edgewall Software

Changes between Version 33 and Version 34 of TracDev/ApiChanges/0.12


Ignore:
Timestamp:
May 15, 2010, 11:50:12 AM (14 years ago)
Author:
Remy Blank
Comment:

Added documentation for with_transaction().

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/ApiChanges/0.12

    v33 v34  
    4444
    4545
    46 === Other Changes to the 0.12 API ===
     46=== Other Changes ===
    4747
    4848==== `prevnext_nav` support for i18n ^[source:trunk/trac/web/chrome.py@8597:158-164#L158 (0.12)] [source:branches/0.11-stable/trac/web/chrome.py@8550:139-144#L139 (0.11)]^ ==== #prevnext_nav
     
    234234Plugins can implement additional providers, for example to automatically include all repositories located below a given directory (similar to the `SVNParentPath` directive of [http://svnbook.red-bean.com/en/1.5/svn.ref.mod_dav_svn.conf.html mod_dav_svn]).
    235235
     236=== Other news ===
     237==== `trac.db.util.with_transaction()` ^[source:trunk/trac/db/util.py@9701:23-78#L22 (0.12)]^ ==== #with_transaction
     238
     239The `@with_transaction(env)` decorator replaces the current practice of getting a database connection with `env.get_db_cnx()` and issuing an explicit commit or rollback. Applied to a local function, it calls that function with a database connection as an argument, and either issues a commit if the function terminates normally, or a rollback if the function raises an exception. In the latter case, the exception is re-raised.
     240
     241This makes transactions much more robust, as we can guarantee that any mutating operations on the database are either committed or rolled back. This should avoid issues like #8443, where transactions were kept open in IDLE state.
     242
     243This mechanism also handles transaction nesting automatically, by only committing or rolling back in the outermost transaction block. This avoids having to pass a `db` argument around, like was typically done in model object methods like [source:trunk/trac/wiki/model.py@9701:120#L119 WikiPage.save()]. The `db` argument is still supported for backward compatibility in methods that were present in 0.11, but is deprecated and should not be used in new code.
     244
     245To avoid having to import `with_transaction` from `trac.db.util` in every module using transactions, it can also be called conveniently on the environment as `@env.with_transaction()`.
     246
     247Implementing this mechanism as a function decorator is an intermediate solution until the `with` statement and context managers become available (once we drop support for Python 2.4).
     248
     249See #8751 for details.