Discussion:
What does @transact around a function actually do?
jcurry
2013-10-24 11:05:14 UTC
Permalink
jcurry [http://community.zenoss.org/people/jcurry] created the discussion

"What does @transact around a function actually do?"

To view the discussion, visit: http://community.zenoss.org/message/75052#75052

--------------------------------------------------------------
We are seeing more examples in code and also in event transforms ( see the class transform on /Status/Perf) of functions that are preceded by @transact .

What does this really do?

I am guessing that it locks the Zope database while it runs code that modifies the database?

Examples I have found do not have a commit() - is this not needed if it is a @transact?

If it locks the database, presumably we should make such functions as short / fast as possible?

Any explanation and guidance gratefully received.

Cheers,
Jane
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/75052#75052]

Start a new discussion in zenoss-users at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
Rob Eagle
2013-10-24 12:52:51 UTC
Permalink
Rob Eagle [http://community.zenoss.org/people/reagle] created the discussion

"Re: What does @transact around a function actually do?"

To view the discussion, visit: http://community.zenoss.org/message/75053#75053

--------------------------------------------------------------
Jane,
Looks like the func for this decorator is in /opt/zenoss/lib/python/ZODB/transact.py and definately is trying to commit the transaction -
--Rob

##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Tools to simplify transactions within applications."""

from ZODB.POSException import ReadConflictError, ConflictError
import transaction

def _commit(note):
    t = transaction.get()
    if note:
        t.note(note)
    t.commit()

def transact(f, note=None, retries=5):
    """Returns transactional version of function argument f.

    Higher-order function that converts a regular function into
    a transactional function.  The transactional function will
    retry up to retries time before giving up.  If note, it will
    be added to the transaction metadata when it commits.

    The retries occur on ConflictErrors.  If some other
    TransactionError occurs, the transaction will not be retried.
    """

    # TODO:  deal with ZEO disconnected errors?

    def g(*args, **kwargs):
        n = retries
        while n:
            n -= 1
            try:
                r = f(*args, **kwargs)
            except ReadConflictError, msg:
                transaction.abort()
                if not n:
                    raise
                continue
            try:
                _commit(note)
            except ConflictError, msg:
                transaction.abort()
                if not n:
                    raise
                continue
            return r
        raise RuntimeError("couldn't commit transaction")
    return g
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/75053#75053]

Start a new discussion in zenoss-users at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
Loading...