interfaces.py 3.95 KB
Newer Older
Jim Fulton's avatar
Jim Fulton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
from zope.interface import Interface

class TransactionError(StandardError):
    """An error occured due to normal transaction processing."""

class ConflictError(TransactionError):
    """Two transactions tried to modify the same object at once

    This transaction should be resubmitted.
    """

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
class IllegalStateError(TransactionError):
    """An operation was invoked that wasn't valid in the current
    transaction state.
    """

    def __init__(self, verb, state):
        self._verb = verb
        self._state = state

    def __str__(self):
        return "Can't %s transaction in %s state" % (self._verb,
                                                     self._state)

class AbortError(TransactionError):
    """Transaction commit failed and the application must abort."""

    def __init__(self, datamgr):
        self.datamgr = datamgr

    def __str__(self):
        str = self.__class__.__doc__ + " Failed data manager: %s"
        return str % self.datamgr

Jim Fulton's avatar
Jim Fulton committed
48 49 50 51 52 53 54 55 56 57 58 59
class RollbackError(TransactionError):
    """An error occurred rolling back a savepoint."""

class IDataManager(Interface):
    """Data management interface for storing objects transactionally

    This is currently implemented by ZODB database connections.
    """

    def prepare(transaction):
        """Begin two-phase commit of a transaction.

60 61
        The data manager must raise an exception if it is not prepared
        to commit the transaction after executing prepare().
Jim Fulton's avatar
Jim Fulton committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        """

    def abort(transaction):
        """Abort changes made by transaction."""

    def commit(transaction):
        """Commit changes made by transaction."""

    def savepoint(transaction):
        """Do tentative commit of changes to this point.

        Should return an object implementing IRollback
        """

class IRollback(Interface):

    def rollback():
        """Rollback changes since savepoint."""

class ITransaction(Interface):
    """Transaction objects

    Application code typically gets these by calling
    get_transaction().
    """

    def abort():
        """Abort the current transaction."""

    def begin():
        """Begin a transaction."""

    def commit():
        """Commit a transaction."""

    def join(resource):
        """Join a resource manager to the current transaction."""

    def status():
        """Return status of the current transaction."""
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    def suspend():
        """Suspend the current transaction.

        If a transaction is suspended, the transaction manager no
        longer treats it as active.  The resume() method must be
        called before the transaction can be used.
        """

    def resume():
        """Resume the current transaction.

        If another transaction is active, it must be suspended before
        resume() is called.
        """

class ITransactionManager(Interface):
    """Coordinates application use of transactional resources."""

    def get():
        """Return the curren transaction.

        Calls new() to start a new transaction if one does not exist.
        """

    def begin():
        """Return a new transaction.

        If a transaction is currently active for the calling thread,
        it is aborted.
        """

    def commit(txn):
        """Commit txn."""

    def abort(txn):
        """Abort txn."""

    def savepoint(txn):
        """Return rollback object that can restore txn to current state."""